GitLab
Deskripsi Umum
Dokumen ini membahas cara mengintegrasikan GitLab dengan Jenkins menggunakan webhook. Dengan setup ini, setiap kali ada push ke branch tertentu di GitLab, Jenkins akan otomatis menjalankan pipeline sesuai dengan environment yang ditentukan.
Setup Webhook GitLab untuk Trigger Jenkins
Webhook adalah mekanisme yang memungkinkan GitLab untuk mengirim notifikasi ke Jenkins setiap kali ada event tertentu (seperti push code). Berikut langkah-langkah konfigurasinya.
Persiapan
Sebelum setup webhook di GitLab, pastikan:
- Jenkins sudah terpasang plugin GitLab
- Pipeline Jenkins sudah dibuat dan dikonfigurasi dengan GitLab trigger
- Dapatkan Webhook URL dari Jenkins (biasanya ada di konfigurasi pipeline)
- Catat Secret Token jika Jenkins menggunakan autentikasi token
Langkah Setup Webhook
1. Akses Menu Webhook
- Login ke GitLab dan buka repository project yang ingin diintegrasikan
- Masuk ke menu:
Settings → Webhooks
2. Konfigurasi URL
Pada bagian URL, masukkan endpoint webhook dari Jenkins. Format URL biasanya:
https://src-jen.tsgitdev.com/project/<folder-name>/<pipeline-name>
Contoh:
https://src-jen.tsgitdev.com/project/myapp-service/build-and-deploy
Catatan: Pastikan URL bisa diakses dari server GitLab. Jika Jenkins berada di jaringan internal, GitLab juga harus bisa reach ke network tersebut.
3. Setup Secret Token
Secret token digunakan untuk autentikasi agar tidak sembarang request bisa trigger Jenkins.
- Buka konfigurasi pipeline di Jenkins
- Di bagian Build Triggers, cari GitLab section
- Generate atau copy Secret token yang ada
- Paste token tersebut ke field Secret token di GitLab webhook
Jika belum ada token di Jenkins:
- Klik Advanced di bagian GitLab trigger
- Klik Generate untuk membuat token baru
- Copy token tersebut
4. Konfigurasi Trigger Events
Pilih event yang akan men-trigger webhook. Untuk integrasi dengan Jenkins, biasanya menggunakan:
Push Events
Centang Push events untuk trigger otomatis setiap ada push ke repository.
Di bagian ini ada beberapa pilihan filter branch:
a. All branches
Pilih ini jika ingin semua branch men-trigger Jenkins pipeline. Berguna untuk:
- Testing di semua branch
- Development environment yang tidak strict
- Project kecil dengan policy yang simple
Contoh use case:
Feature branch: feature/login → trigger Jenkins
Bugfix branch: bugfix/header → trigger Jenkins
Main branch: main → trigger Jenkins
b. Wildcard pattern
Gunakan wildcard pattern untuk men-trigger branch dengan pola tertentu. Format menggunakan shell wildcard (*, ?).
Contoh penggunaan:
release/*
Akan trigger semua branch yang dimulai dengan release/:
release/v1.0✅release/v2.1✅hotfix/bug-fix❌
feature/*
Akan trigger semua feature branch:
feature/login✅feature/payment✅develop❌
staging
Hanya trigger branch staging saja.
c. Regular expression
Untuk filtering yang lebih kompleks, gunakan regex pattern. Harus mengikuti format regex yang valid.
Contoh penggunaan:
^(main|develop)$
Hanya trigger branch main dan develop.
^release/v[0-9]+\.[0-9]+$
Trigger branch release dengan format versi:
release/v1.0✅release/v2.5✅release/beta❌
^(feature|bugfix)/.*
Trigger semua branch yang dimulai dengan feature/ atau bugfix/:
feature/authentication✅bugfix/header-issue✅develop❌
Merge Request Events (Opsional)
Centang Merge request events jika ingin trigger pipeline saat ada:
- Merge request dibuat
- Merge request di-update
- Merge request di-merge
Berguna untuk automated testing sebelum code di-merge ke branch utama.
5. Konfigurasi SSL Verification
Jika Jenkins menggunakan HTTPS dengan self-signed certificate:
- Uncheck Enable SSL verification
Jika menggunakan certificate valid:
- Biarkan checked untuk keamanan lebih baik
Strategy Branch untuk Environment
Dalam praktik DevOps, biasanya kita memisahkan webhook trigger berdasarkan environment. Berikut rekomendasi setup untuk masing-masing environment.
Development Environment
Karakteristik:
- Trigger otomatis untuk semua perubahan
- Deployment cepat tanpa approval
- Testing awal dan debugging
Konfigurasi Webhook:
| Setting | Value |
|---|---|
| URL | https://src-jen.tsgitdev.com/project/myapp-dev/deploy-dev |
| Secret Token | Token khusus untuk dev pipeline |
| Push Events | ✅ Enabled |
| Branch Filter | Wildcard pattern: develop atau dev/* |
| Merge Request | ❌ Disabled (opsional) |
Branch Strategy:
develop → Auto deploy ke dev server
dev/feature-login → Auto deploy ke dev server
dev/bugfix-header → Auto deploy ke dev server
Jenkinsfile Example:
pipeline {
agent { label 'docker-dev' }
when {
branch 'develop'
}
stages {
stage('Deploy to Development') {
steps {
sh 'docker-compose -f docker-compose.dev.yml up -d'
}
}
}
}
Staging Environment
Karakteristik:
- Testing lebih comprehensive
- Mirror dari production
- QA testing dilakukan di sini
- Perlu approval sebelum deploy (opsional)
Konfigurasi Webhook:
| Setting | Value |
|---|---|
| URL | https://src-jen.tsgitdev.com/project/myapp-staging/deploy-staging |
| Secret Token | Token khusus untuk staging pipeline |
| Push Events | ✅ Enabled |
| Branch Filter | Wildcard pattern: staging atau Regex: `^(staging |
| Merge Request | ✅ Enabled (untuk pre-merge testing) |
Branch Strategy:
staging → Auto deploy ke staging server
release/v1.0 → Auto deploy ke staging server
release/v2.1 → Auto deploy ke staging server
Jenkinsfile Example:
pipeline {
agent { label 'docker-staging' }
when {
anyOf {
branch 'staging'
branch pattern: 'release/.*', comparator: 'REGEXP'
}
}
stages {
stage('Deploy to Staging') {
steps {
sh 'docker-compose -f docker-compose.staging.yml up -d'
}
}
stage('Run Integration Tests') {
steps {
sh 'npm run test:integration'
}
}
}
}
Production Environment
Karakteristik:
- Deploy paling strict
- Hanya dari branch production atau tag
- Wajib ada approval
- Rollback strategy harus jelas
Konfigurasi Webhook:
| Setting | Value |
|---|---|
| URL | https://src-jen.tsgitdev.com/project/myapp-prod/deploy-production |
| Secret Token | Token khusus untuk production pipeline |
| Push Events | ✅ Enabled |
| Branch Filter | Regex: `^(main |
| Tag Events | ✅ Enabled (untuk versioning) |
| Merge Request | ❌ Disabled |
Branch Strategy:
main → Deploy ke production (dengan approval)
production → Deploy ke production (dengan approval)
v1.0.0 (tag) → Deploy ke production (dengan approval)
Jenkinsfile Example:
pipeline {
agent { label 'docker-prod' }
when {
anyOf {
branch 'main'
branch 'production'
tag pattern: 'v[0-9]+\\.[0-9]+\\.[0-9]+', comparator: 'REGEXP'
}
}
stages {
stage('Approval') {
steps {
input message: 'Deploy to Production?', ok: 'Deploy'
}
}
stage('Backup Current Version') {
steps {
sh './scripts/backup-production.sh'
}
}
stage('Deploy to Production') {
steps {
sh 'docker-compose -f docker-compose.prod.yml up -d'
}
}
stage('Health Check') {
steps {
sh './scripts/health-check.sh'
}
}
}
post {
failure {
sh './scripts/rollback-production.sh'
}
}
}
Multiple Webhook untuk Multiple Environment
Jika dalam satu repository ada beberapa environment, kita bisa setup multiple webhook dengan trigger yang berbeda.
Setup Multiple Webhook
Di GitLab, kita bisa menambahkan lebih dari satu webhook dalam satu repository:
-
Webhook 1 - Development
- URL:
https://src-jen.tsgitdev.com/project/myapp-dev/deploy-dev - Branch:
developataudev/*
- URL:
-
Webhook 2 - Staging
- URL:
https://src-jen.tsgitdev.com/project/myapp-staging/deploy-staging - Branch:
stagingataurelease/*
- URL:
-
Webhook 3 - Production
- URL:
https://src-jen.tsgitdev.com/project/myapp-prod/deploy-production - Branch:
mainatauproduction
- URL:
Keuntungan Multiple Webhook
- Isolation: Setiap environment punya pipeline terpisah
- Flexibility: Bisa set token berbeda untuk setiap environment
- Security: Production pipeline lebih secure dengan token yang berbeda
- Monitoring: Lebih mudah tracking deployment per environment
Testing Webhook
Setelah webhook dibuat, penting untuk melakukan testing:
1. Test dari GitLab UI
- Scroll ke bawah di halaman webhook
- Cari webhook yang baru dibuat
- Klik Test → pilih Push events
- GitLab akan mengirim test request ke Jenkins
- Cek response:
- HTTP 200: Webhook berhasil
- HTTP 403: Token salah atau autentikasi gagal
- HTTP 404: URL Jenkins tidak ditemukan
- Timeout: Jenkins tidak bisa diakses dari GitLab
2. Test dengan Push Code
Cara paling reliable adalah dengan push code ke branch yang sudah dikonfigurasi:
# Pastikan di branch yang benar
git checkout develop
# Buat perubahan kecil
echo "test webhook" >> test.txt
git add test.txt
git commit -m "test: webhook integration"
# Push ke GitLab
git push origin develop
Cek di Jenkins:
- Pipeline harus otomatis ter-trigger
- Bisa dilihat di Build History
- Cek console output untuk memastikan tidak ada error
3. Monitoring Webhook Logs
Di GitLab, setiap webhook request dicatat. Untuk melihat log:
- Buka Settings → Webhooks
- Klik Edit pada webhook yang ingin dicek
- Scroll ke bawah ke bagian Recent events
- Lihat status dan response dari setiap request
Status yang mungkin muncul:
- ✅ 200 OK: Request berhasil dikirim dan Jenkins merespon
- ❌ Failed: Request gagal (cek detail error)
- ⏱️ Timeout: Jenkins tidak merespon dalam waktu yang ditentukan
Troubleshooting
Webhook Tidak Trigger Jenkins
Kemungkinan penyebab:
- URL Jenkins salah atau tidak bisa diakses dari GitLab
- Secret token tidak match
- Branch filter tidak sesuai
- GitLab plugin di Jenkins tidak aktif
Solusi:
- Verify URL bisa diakses:
curl -X POST <webhook-url> - Cek token di kedua sisi (GitLab dan Jenkins)
- Test dengan "All branches" dulu untuk memastikan webhook jalan
- Pastikan plugin GitLab di Jenkins sudah terinstall dan aktif
Jenkins Trigger Tapi Build Gagal
Kemungkinan penyebab:
- Jenkinsfile error
- Node/agent tidak available
- Credential untuk clone repository salah
Solusi:
- Cek console output di Jenkins untuk error detail
- Pastikan agent dengan label yang sesuai online
- Verify SSH key atau credential untuk akses GitLab repository
Webhook Timeout
Kemungkinan penyebab:
- Jenkins sedang overload
- Network issue antara GitLab dan Jenkins
- Firewall blocking request
Solusi:
- Cek status Jenkins server
- Pastikan port Jenkins (biasanya 8080) terbuka
- Cek firewall rules di server Jenkins
Multiple Trigger untuk Satu Push
Kemungkinan penyebab:
- Ada multiple webhook dengan filter yang overlap
- Push ke multiple branch sekaligus
Solusi:
- Review semua webhook yang aktif
- Pastikan branch filter tidak overlap
- Gunakan regex yang lebih spesifik
Best Practices
Security
- Selalu gunakan Secret Token untuk webhook production
- Gunakan HTTPS jika memungkinkan untuk koneksi yang aman
- Token berbeda untuk setiap environment
- Restrict network access - hanya GitLab server yang bisa akses Jenkins webhook endpoint
Branch Strategy
- Pisahkan branch per environment untuk menghindari accidental deployment
- Gunakan tag untuk production release yang ter-versioning
- Protected branch untuk main/production branch
- Require approval untuk merge ke production branch
Monitoring
- Setup notification di Jenkins untuk build status (email, Slack, etc)
- Monitor webhook logs secara berkala di GitLab
- Set up alerting untuk failed deployment
- Keep build history untuk audit trail
Performance
- Jangan trigger untuk semua branch jika tidak perlu - gunakan filter
- Cleanup old builds secara berkala untuk save storage
- Use lightweight checkout jika memungkinkan untuk speed up clone
- Parallel execution untuk multiple environment jika infrastruktur support
Kesimpulan
Setup webhook GitLab ke Jenkins adalah langkah penting dalam implementasi CI/CD. Dengan konfigurasi yang tepat, kita bisa otomasi deployment untuk berbagai environment dengan aman dan efisien.
Kunci suksesnya ada di:
- Branch strategy yang jelas
- Filter webhook yang tepat
- Security dengan token dan SSL
- Testing yang menyeluruh sebelum production
Mulai dengan setup development environment terlebih dahulu, test sampai benar-benar stabil, baru kemudian terapkan ke staging dan production.
Terakhir diperbarui: 22 Oktober 2025
Dibuat oleh: Tim DevOps Internal