Jenkins Plugin 설치#
- Jenkins 에서 제공하는 기본 플러그인 외에 추가로 필요한 플러그인 설치
- Jenkins > 플러그인 관리 > 설치 가능 탭에서 필요한 플러그인 검색
- 설치가 완료되면 Jenkins 재시작
Jenkins Kubernetes Credential 설정#
- Jenkins > 관리 > Credentials > Global credentials (unrestricted) > Add Credentials
- Kind: Secret file
- File:
~/.kube/config - ID:
kube-config - Description:
Kubernetes config file
Jenkins Kubernetes Cloud 설정#
- Jenkins > 관리 > Cloud > Kubernetes Cloud Details
- Kubernetes URL:
https://kubernetes.default - Kubernetes Namespace:
jenkins - Credentials:
kube-config
Jenkins Gitlab Credential 설정#
- Jenkins > 관리 > Credentials > Global credentials (unrestricted) > Add Credentials
- Kind: Username with password
- Username:
gitlab username (gitlab 에서 사용하는 username) - Password:
gitlab password (gitlab 에서 사용하는 password) - ID:
gitlab-credential - Description:
Gitlab credential
Jenkinsfile 작성#
// Jenkins Pipeline 예시
// Test app 을 Docker build 하고 Git push 하는 파이프라인
pipeline {
agent {
// kubernetes 에서 실행
kubernetes {
yaml '''
apiVersion: v1
kind: Pod
spec:
containers:
- name: python
image: python:3.10
command:
- cat
tty: true
- name: kaniko
image: gcr.io/kaniko-project/executor:debug
command:
- cat
tty: true
volumeMounts:
- name: registry-credentials
mountPath: /kaniko/.docker
volumes:
- name: registry-credentials
secret:
secretName: regcred
items:
- key: .dockerconfigjson
path: config.json'''
}
}
stages {
// Unit Test (테스트를 위해 python 컨테이너에서 실행)
// Test 결과가 성공이면 Docker Build 수행
// 테스트를 위해 적용한 스테이지로 필수로 포함할 필요는 없음
stage('Test') {
steps {
container('python') {
sh 'pip install -r requirements.txt'
sh 'python test_app.py'
}
}
}
// Docker Build
// kaniko 컨테이너에서 Dockerfile 을 통해 이미지 빌드
// 컨테이너 내부에서 Docker 빌드를 위해 [kaniko](https://github.com/GoogleContainerTools/kaniko) 를 사용
// Docker build 후 Docker Hub 에 Push
stage('Docker Build') {
steps {
container('kaniko') {
sh "executor --dockerfile=Dockerfile \
--context=dir://${env.WORKSPACE} \
--destination=<docker-id>/test-app:${currentBuild.number}"
}
}
}
// docker image 를 deployment.yaml 에 적용
// deployment.yaml 의 image version 을 현재 빌드 번호로 변경
// 변경된 deployment.yaml 을 git 에 push
stage('Modified deployment.yaml') {
steps {
sh "sed -i 's/test-app:.*\$/test-app:${currentBuild.number}/g' deployment.yaml"
withCredentials([gitUsernamePassword(credentialsId: 'gitlab-credential',
gitToolName: 'git-tool')]) {
sh 'git checkout main'
sh 'git fetch --all'
sh 'git add deployment.yaml'
sh "git commit -m 'Update image version test-app:${currentBuild.number} in deployment.yaml'"
sh 'git push -u origin main'
}
}
post {
success {
echo 'Deploy Success'
}
failure {
echo 'Deploy Failure'
}
}
}
}
}
Jenkins Pipeline 실행#
- Jenkins > 새로운 Item > Enter an item name:
test-app - Pipeline 선택 > OK
- Pipeline > Definition:
Pipeline script from SCM - SCM:
Git- Repository URL:
대상 gitlab repository 주소 - Credentials:
Gitlab credential - Branches to build:
main
- Script Path:
Jenkinsfile - 저장 후 빌드 실행
Jenkins Pipeline 결과 확인#
- Jenkins Pipeline 실행 결과 확인
- 오류 발생 시 Console Output 확인
- 정상적으로 빌드가 완료되면 Gitlab 에서 변경된 deployment.yaml 확인
- ArgoCD 에서 변경된 deployment.yaml 을 통해 배포 확인