목차
Jenkin 를 이용하여 CI 를 자동화해보자!
이번 포스팅에서는 Jenkins 를 이용하여 CI 를 자동화 하는 방법을 알아보도록 하겠습니다.
이 구성은 Github 의 Actions 와 비슷하게 구성 할 수 있습니다.
Pipeline 시나리오
- Gitlab 의 특정 Project 에서
Push
orMerge
가 발생하면 Jenkins 에 CI 를 발생하도록 webhook 을 발생한다. - Jenkins 는 설정된 Pipeline 에 맞게 CI 를 진행한다.
- CI 는 Clone source > docker build > docker run > docker rm 으로 진행 됩니다.
CI 설정
테스트 소스
CI 테스트용으로 사용될 소스는 아래와 같습니다.
Dockerfile
과index.html
로 구성된 소스입니다.
FROM httpd
COPY index.html /usr/local/apache2/htdocs
Dockerfile
내용입니다.httpd
image 에index.html
를 추가하는Dockerfile
입니다.
Jenkins Token 생성 및 등록
Pipeline 생성
Pipeline Script 추가
node {
stage ('clone') {
git branch: 'master', credentialsId: 'chhanz', url: 'git@gitlab.example.com:chhanz/cicd-httpd-source.git'
}
stage ('docker build') {
sh ' docker build --tag sample-ci-httpd .'
}
stage ('run docker') {
sh ' docker run -d -ti --name jenkins-ci -p 33333:80 sample-ci-httpd'
}
stage ('check httpd') {
sh 'curl -s http://127.0.0.1:33333'
}
stage ('rm docker') {
sh ' docker rm -f jenkins-ci'
}
}
위와 같이 Pipeline Script 를 구성하였습니다.
- 상세 Pipeline 순서
- Clone Source
- Build Image
- Run Container
- Healthy check
- Delete Container
Build 결과
Gitlab 과 Jenkins 연동
지금까진 수동으로 Build 진행 되는 Pipeline 을 생성하였습니다.
이제는 Gitlab Source 의 Push
or Merge
가 발생되면 Jenkins 에서 Build 가 자동으로 진행 되도록 구성하여 CI 자동화를 완성하도록 하겠습니다.
CI 자동화 테스트
실제로 소스를 변경하여 CI 가 자동화 되는지 확인하겠습니다.
[root@fastvm-centos-7-7-91 cicd-httpd-source]# git add .
[root@fastvm-centos-7-7-91 cicd-httpd-source]# git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: index.html
#
[root@fastvm-centos-7-7-91 cicd-httpd-source]# git commit -m "update index.html"
[master ad71674] update index.html
1 file changed, 1 insertion(+), 1 deletion(-)
[root@fastvm-centos-7-7-91 cicd-httpd-source]# git push
Counting objects: 5, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 279 bytes | 0 bytes/s, done.
Total 3 (delta 2), reused 0 (delta 0)
To git@gitlab.example.com:chhanz/cicd-httpd-source.git
c633ff8..ad71674 master -> master