Bu örnekte, GitHub'ın Jenkins pipeline aşamalarını yalnızca GitHub deposundaki bir feature branchına kod gönderdiğimizde çalıştırımasına izin vereceğiz. 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 "Just the push event" seçeneğini seçili bırakın. Not: Bu normalde yeterli olacaktır ancak isteğe bağlı olarak "Let me select individual events." bölümündeki "Pushes" ve "Pull requests" seçeneklerini işaretleyip kullanabilirsiniz.

  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-builder" 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 **/feature/* (veya feature/*) olarak değiştirin çünkü biz git push komutunun sadece "feature" branchlarında ektili olmasını istiyoruz.

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

  10. Kaydedip çıkın.

Git push komutuyla test


$ git status
modified: Readme.md
$ git add .
$ git commit -m 'Update readme file'
$ git push origin feature/test
To git@github.com:inanzzz/game.git
139991f..25f345e feature/test -> feature/test


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-builder
Feb 16, 2019 9:22:49 PM com.cloudbees.jenkins.GitHubPushTrigger$1 run
INFO: SCM changes detected in game-pr-builder. Triggering #15
Feb 16, 2019 9:23:12 PM org.jenkinsci.plugins.workflow.job.WorkflowRun finish
INFO: game-pr-builder #15 completed: SUCCESS

Dosyalar


ci/pipeline/branch/feature/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 'cd docker && cd ci && make build'
}
}
stage('Composer Install') {
steps {
echo '> Building the application within the container ...'
sh 'cd docker && cd ci && make composer'
}
}
stage('Test') {
steps {
echo '> Running the application tests ...'
sh 'cd docker && cd ci && make test'
}
}
}
}

docker/ci/Makefile


CONTAINER := game_php

build:
@docker-compose up -d

composer:
@docker exec -i $(CONTAINER) composer install

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

down:
@docker-compose down --volumes
@make -s clean

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