Skip to main content

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

  1. Login ke GitLab dan buka repository project yang ingin diintegrasikan
  2. 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.

  1. Buka konfigurasi pipeline di Jenkins
  2. Di bagian Build Triggers, cari GitLab section
  3. Generate atau copy Secret token yang ada
  4. 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:

SettingValue
URLhttps://src-jen.tsgitdev.com/project/myapp-dev/deploy-dev
Secret TokenToken khusus untuk dev pipeline
Push Events✅ Enabled
Branch FilterWildcard 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:

SettingValue
URLhttps://src-jen.tsgitdev.com/project/myapp-staging/deploy-staging
Secret TokenToken khusus untuk staging pipeline
Push Events✅ Enabled
Branch FilterWildcard 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:

SettingValue
URLhttps://src-jen.tsgitdev.com/project/myapp-prod/deploy-production
Secret TokenToken khusus untuk production pipeline
Push Events✅ Enabled
Branch FilterRegex: `^(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:

  1. Webhook 1 - Development

    • URL: https://src-jen.tsgitdev.com/project/myapp-dev/deploy-dev
    • Branch: develop atau dev/*
  2. Webhook 2 - Staging

    • URL: https://src-jen.tsgitdev.com/project/myapp-staging/deploy-staging
    • Branch: staging atau release/*
  3. Webhook 3 - Production

    • URL: https://src-jen.tsgitdev.com/project/myapp-prod/deploy-production
    • Branch: main atau production

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

  1. Scroll ke bawah di halaman webhook
  2. Cari webhook yang baru dibuat
  3. Klik Test → pilih Push events
  4. GitLab akan mengirim test request ke Jenkins
  5. 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:

  1. Buka Settings → Webhooks
  2. Klik Edit pada webhook yang ingin dicek
  3. Scroll ke bawah ke bagian Recent events
  4. 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:

  1. URL Jenkins salah atau tidak bisa diakses dari GitLab
  2. Secret token tidak match
  3. Branch filter tidak sesuai
  4. 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:

  1. Jenkinsfile error
  2. Node/agent tidak available
  3. 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:

  1. Jenkins sedang overload
  2. Network issue antara GitLab dan Jenkins
  3. 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:

  1. Ada multiple webhook dengan filter yang overlap
  2. Push ke multiple branch sekaligus

Solusi:

  • Review semua webhook yang aktif
  • Pastikan branch filter tidak overlap
  • Gunakan regex yang lebih spesifik

Best Practices

Security

  1. Selalu gunakan Secret Token untuk webhook production
  2. Gunakan HTTPS jika memungkinkan untuk koneksi yang aman
  3. Token berbeda untuk setiap environment
  4. Restrict network access - hanya GitLab server yang bisa akses Jenkins webhook endpoint

Branch Strategy

  1. Pisahkan branch per environment untuk menghindari accidental deployment
  2. Gunakan tag untuk production release yang ter-versioning
  3. Protected branch untuk main/production branch
  4. Require approval untuk merge ke production branch

Monitoring

  1. Setup notification di Jenkins untuk build status (email, Slack, etc)
  2. Monitor webhook logs secara berkala di GitLab
  3. Set up alerting untuk failed deployment
  4. Keep build history untuk audit trail

Performance

  1. Jangan trigger untuk semua branch jika tidak perlu - gunakan filter
  2. Cleanup old builds secara berkala untuk save storage
  3. Use lightweight checkout jika memungkinkan untuk speed up clone
  4. 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