Bu örneğimizde her git push komutundan sonra dockerleştirilmiş uygulamanın Jenkins ile tekrardan kurulumunu göreceğiz. Jenkins sunucumuz Ubuntu 16.04 üzerine kurulu ve gerekli bilgileri aşağıdaki gibidir. Sisteminizin kurulu ve çalışıyor vaziyette olduğunu varsayıyorum.


Kurulum bilgileri


Server:
IP: 192.168.99.20
Jenkins GUI: http://192.168.99.20:8080/
Jenkins GUI login: admin:admin
Jenkisn Root: /var/lib/jenkins

Configuration:
Jenkins: version 2.89.2
Docker: version 17.09.1-ce, build 19e2cf6
Docker Compose: version 1.18.0, build 8dd22a9

Ön bilgi


Öncelikler sudo usermod -aG docker jenkins komutunu çalıştırarak jenkins kullanıcısına docker komutunu çalıştırma hakkını veriyoruz. Bu işlemden sonra sudo service jenkins restart komutu ile Jenkins'i yeniden başlatmalısınız.


Yapı


ubuntu@linux:~/hello-world$ tree -a
.
├── docker
│ ├── docker-compose.yml
│ └── .env
├── index.html
└── logs
└── nginx
├── access.log
└── error.log

3 directories, 6 files

Dosyalar


index.html


This is my web page!

.env


COMPOSE_PROJECT_NAME=helloworld

docker-compose.yml


version: '3'

services:
nginx_img:
container_name: ${COMPOSE_PROJECT_NAME}_nginx_con
image: nginx:1.13.8
volumes:
- ..:/usr/share/nginx/html
- ../logs/nginx:/var/log/nginx
ports:
- 1000:80
networks:
public_net:
ipv4_address: 192.168.0.11

networks:
public_net:
driver: bridge
ipam:
driver: default
config:
- subnet: 192.168.0.0/24

Eğer bilmek isterseniz, Nginx'in konfigürasyon dosyalarının yolu aşağıdaki gibidir.


Config file: /etc/nginx/nginx.conf
Vhost file: /etc/nginx/conf.d/default.conf

Kurulum


ubuntu@linux:~/hello-world/docker$ docker-compose up -d --build

Nginx containerın ulaşılabilirlik testini yapalım.


ubuntu@linux:~$ curl 192.168.0.11
This is my web page!

ubuntu@linux:~$ curl localhost:1000
This is my web page!

Jenkins kurulumu


GitHub SSH iletişimi


ubuntu@linux:~$ sudo su -l jenkins
jenkins@linux:~$

jenkins@linux:~$ ssh-keygen -t rsa -b 4096 -C "email@address.com"
jenkins@linux:~$ eval "$(ssh-agent -s)"
jenkins@linux:~$ ssh-add ~/.ssh/id_rsa
jenkins@linux:~$ cat ~/.ssh/id_rsa.pub # Add this to GitHub
jenkins@linux:~$ ssh -T git@github.com

Jenkins ve GitHub bağlantısı


Bu konuyla ilgili "Jenkins" bölümü altında yazılarım mevcut. Konu uzun olduğundan burada tekrardan detaylara girmeyeceğim ama siz uygulamanız lazım.


Build komutunun eklenmesi


Jenkins'e login olun ve aşağıdakileri yapın.



Test


Eğer "Build now" seçeneğini tıklarsanız aşağıdaki çıktıyı alacaksınız.


Started by user admin
Building in workspace /var/lib/jenkins/workspace/Hello-World
> git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
> git config remote.origin.url https://github.com/inanzzz/helloworld.git # timeout=10
Fetching upstream changes from https://github.com/inanzzz/helloworld.git
> git --version # timeout=10
using GIT_ASKPASS to set credentials
> git fetch --tags --progress https://github.com/inanzzz/helloworld.git +refs/heads/*:refs/remotes/origin/*
Seen branch in repository origin/master
Seen 1 remote branch
> git show-ref --tags -d # timeout=10
Checking out Revision 11c315d020cf6b1791b3294d381357dfc3ce9620 (origin/master)
> git config core.sparsecheckout # timeout=10
> git checkout -f 11c315d020cf6b1791b3294d381357dfc3ce9620
Commit message: "Bring back volumes"
> git rev-list --no-walk 11c315d020cf6b1791b3294d381357dfc3ce9620 # timeout=10
[Hello-World] $ /bin/sh -xe /tmp/jenkins8895909563541010771.sh
+ cd docker
+ docker-compose up -d
helloworld_nginx_con is up-to-date
+ curl localhost:1000
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed

0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
100 21 100 21 0 0 3718 0 --:--:-- --:--:-- --:--:-- 4200
This is my web page!
Finished: SUCCESS

Testten sonraki sunucu bilgileri


ubuntu@linux:~$ sudo su -l jenkins

jenkins@linux:~/workspace/Hello-World$ ls -l
total 12
drwxr-xr-x 2 jenkins jenkins 4096 Dec 24 23:22 docker
-rw-r--r-- 1 jenkins jenkins 21 Dec 24 20:47 index.html
drwxr-xr-x 3 root root 4096 Dec 24 21:02 logs

jenkins@linux:~/workspace/Hello-World$ ls -l logs/
total 4
drwxr-xr-x 2 root root 4096 Dec 24 21:02 nginx

jenkins@linux:~/workspace/Hello-World$ ls -l logs/nginx/
total 4
-rw-r--r-- 1 root root 455 Dec 24 23:22 access.log
-rw-r--r-- 1 root root 0 Dec 24 21:02 error.log

Docker imaj


jenkins@linux:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx 1.13.8 3f8a4339aadd Less than a second ago 108MB
...

Docker container


jenkins@linux:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
041fde7a1975 nginx:1.13.8 "nginx -g 'daemon ..." 8 minutes ago Up 8 minutes 0.0.0.0:1000->80/tcp helloworld_nginx_con

Docker ağ


jenkins@linux:~$ docker network ls
NETWORK ID NAME DRIVER SCOPE
9f0596a9b6dc helloworld_public_net bridge local