Bu örnekte, GitHub'ın Jenkins pipeline aşamalarını yalnızca GitHub deposundaki bir develop branchına PR merge yaptığımızda çalıştırımasına izin vereceğiz. Bu eylem genellikle bir "feature/hotfix/release" dalı "develop" ile birleştirildiğinde ortaya çıkar. Hepsi bu kadar!


Ön şartlar


Öncelikle Jenkins ve GitHub entegrasyonu yapıp Docker uygulamasının pipeline aşamalarını çalıştırmak sayfasında belirtilen "Jenkins sunucusu GitHub SSH entegrasyonu" ve "Jenkins kullanıcısının docker komutlarını çalıştırması" işlemlerini yapın.


Ngrok kurulumu


Bilgisayarınızın masaüstüne ngrok programını indirdikten sonra terminalde $ ./ngrok http -host-header=rewrite 192.168.99.40:8080 komutunu çalıştırın. Belirtilen http://192.168.99.40:8080 adresi sizin Jenkins GUI adresini temsil ediyor. Çalıştırdığınız komut size Jenkins GUI'ye internetten ulaşabilmeniz için yeni bir adres verecek. Daha sonra bu adresi GitHub entegrasyonu için kullanacağız. Mesela şu an için http://fdb1ce55.ngrok.io adresini verdiğini varsayalım.


GitHub Integration Plugin


Jenkins GUI içindeki "Manage Plugins" sayfasından "GitHub Integration Plugin" ekini bulup kurun.


GitHub webhook URL


Jenkins GUI içindeyken aşağıdakileri yapın.


  1. "Configure System" sayfasına gidin.

  2. "GitHub" bölümünden "Advanced" butonuna tıklayın.

  3. "Specify another hook url for GitHub configuration" seçeneğini tikleyin, http://192.168.99.40:8080/github-webhook/ adresini bir yere not edin ve seçenekten tiki kaldırın.

  4. Sayfadan kayıt yapmadan çıkın.

Github repository ayarı


GitHub repository adresi olan https://github.com/inanzzz/game linkine gidip aşağıdakileri yapın.


  1. "Settings" menüsünden "Webhooks" sayfasına gidin.

  2. http://fdb1ce55.ngrok.io/github-webhook/ linkini "Payload URL" kutusuna yapıştırın. Gördüğümüz gibi yerel IP adresini kullanmıyoruz.

  3. "Which events would you like to trigger this webhook?" bölümündeki "Let me select individual events." seçeneğini seçtikten sonra "Pull requests" alt seçeneğini seçili bırakın.

  4. "Add webhook" butonuna tıklayın.

Eğer /var/log/jenkins/jenkins.log dosyasındaki Jenkins loglarına bakarsanız, aşağıdaki bilgileri en altta göreceksiniz.


Feb 16, 2019 5:59:32 PM org.jenkinsci.plugins.github.webhook.subscriber.PingGHEventSubscriber onEvent
INFO: PING webhook received from repo !

Ayrıca, GitHub sayfasındaki webhook ayrıntılarını kontrol ederseniz, GitHub ve Jenkins arasındaki iletişim bilgilerini görürsünüz. Eklenen webhook içindeki "Recent Deliveries" bölümünde bulunabilir.


Jenkins projesi yaratma


Jenkins GUI'ye gidin ve yeni bir proje eklemek için aşağıdakileri yapın.


  1. Projeyi "game-pr-merger" adlandırın, "Pipeline" seçeneğini seçin ve işlemi kaydedin.

  2. "General" tab altındaki "GitHub project" seçeneğini tikleyin ve https://github.com/inanzzz/game adresini kutuya yapıştırın.

  3. "Build Triggers" tab altındaki, "GitHub hook trigger for GITScm polling" seçeneğini tikleyin.

  4. "Pipeline" bölümünden "Pipeline script from SCM" seçeneğini seçin.

  5. SCM olarak "Git" seçin.

  6. "Repository URL" için https://github.com/inanzzz/game linkini kullanın.

  7. "Credentials" bölümünden GitHub-inanzzz seçeneğini seçin.

  8. */master girdisini develop olarak değiştirin çünkü biz git merge komutunun sadece "develop" brancında ektili olmasını istiyoruz.

  9. "Script path" bölümüne ci/pipeline/branch/develop/Jenkinsfile yazın.

  10. Kaydedip çıkın.

Git push komutuyla test


GitHub deposunda bir PR oluşturun ve "Merge pull request" butonuna tıklatarak birleştirin.



Jenkins logları aşağıdakine benzemelidir.


Feb 16, 2019 9:22:47 PM org.jenkinsci.plugins.github.webhook.subscriber.DefaultPushGHEventSubscriber onEvent
INFO: Received PushEvent for https://github.com/inanzzz/game from 192.30.252.37 ⇒ http://192.168.99.40:8080/github-webhook/
Feb 16, 2019 9:22:47 PM org.jenkinsci.plugins.github.webhook.subscriber.DefaultPushGHEventSubscriber$1 run
INFO: Poked game-pr-merger
Feb 16, 2019 9:22:49 PM com.cloudbees.jenkins.GitHubPushTrigger$1 run
INFO: SCM changes detected in game-pr-merger. Triggering #15
Feb 16, 2019 9:23:12 PM org.jenkinsci.plugins.workflow.job.WorkflowRun finish
INFO: game-pr-merger #15 completed: SUCCESS

Dosyalar


ci/pipeline/branch/develop/Jenkinsfile


pipeline {
agent any

options {
skipDefaultCheckout(true)
}

stages {
stage('Checkout SCM') {
steps {
echo '> Checking out the source control ...'
checkout scm
}
}
stage('Docker Up') {
steps {
echo '> Building the docker containers ...'
sh 'make -sC docker/ci/ build'
}
}
stage('Composer Install') {
steps {
echo '> Building the application within the container ...'
sh 'make -sC docker/ci/ composer'
}
}
stage('Test') {
steps {
echo '> Running the application tests ...'
sh 'make -sC docker/ci/ test'
}
}
stage('Cleanup') {
steps {
echo '> Cleaning the docker artifacts ...'
sh 'make -sC docker/ci/ clean'
}
}
stage('Docker Image Build') {
steps {
echo '> Building the docker image ...'
}
}
stage('Docker Image Push') {
steps {
echo '> Pushing the docker image ...'
}
}
stage('Deploy') {
steps {
echo '> Deploying the application to staging ...'
}
}
}
}

docker/ci/Makefile


PHP_SERVICE := game_php

build:
@docker-compose up -d

composer:
@docker-compose exec -T $(PHP_SERVICE) composer install

test:
@docker-compose exec -T $(PHP_SERVICE) vendor/bin/php-cs-fixer fix src --rules=@PSR2 --using-cache=no --dry-run --verbose --diff
@docker-compose exec -T $(PHP_SERVICE) vendor/bin/phpunit tests
@docker-compose exec -T $(PHP_SERVICE) vendor/bin/phpstan analyse src tests --no-progress --level=max

clean:
@docker-compose down --volumes
@docker system prune --volumes --force