github 에서 git push 가 일어났을 때, Webhook 을 통해 jenkins pipeline이 trigger 될 수 있도록 설정
1. jenkins 프로젝트 설정에서 GitHub project 추가
2. Build trigger 추가
3. github 프로젝트 설정 중, Webhook 에 들어가서 jenkins 서버 정보 설정
payload URL 의 경우, ip 기반은 안되며, DNS 로 구성해야 하며, 뒤에 /github-webhook/ 경로 추가.
(이를 위해 EC2 앞에 ALB 를 추가하였습니다)
(ex.http://albservice.amazonaws.com/github-webhook/)
4. github Credential 생성
5. jenkins Global Credential 생성
Kind : Username with password
Username : github 계정명
Password : github credential 토큰 (ghp_~~~)
ID : github_access_token (상관은 없으나, jenkins pipeline script 에 들어가는 token 명)
6. pipeline script 중 git clone 시에 credential 추가
pipeline {
agent any
stages {
stage('Git Clone') {
steps {
script {
try {
git url: "https://github.com/abc/jenkins-nodejs.git", branch: "main", credentialsId: 'github_access_token'
env.cloneResult=true
} catch (error) {
print(error)
env.cloneResult=false
currentBuild.result = 'FAILURE'
}
}
}
}
stage('ECR Upload') {
steps{
script{
try {
sh 'aws ecr get-login-password --region ap-northeast-2 | docker login --username AWS --password-stdin 11233.dkr.ecr.ap-northeast-2.amazonaws.com'
sh 'docker build -t nodejs-jenkins .'
sh 'docker tag nodejs-jenkins:latest 11233.dkr.ecr.ap-northeast-2.amazonaws.com/nodejs-jenkins:latest'
sh 'docker push 11233.dkr.ecr.ap-northeast-2.amazonaws.com/nodejs-jenkins:latest'
} catch (error) {
print(error)
echo 'Remove Deploy Files'
sh "rm -rf /var/lib/jenkins/workspace/nodejs-pipeline/*"
currentBuild.result = 'FAILURE'
}
}
}
post {
success {
echo "The ECR Upload stage successfully."
}
failure {
echo "The ECR Upload stage failed."
}
}
}
stage('Deploy'){
steps {
script{
try {
sh"""
aws ecs update-service --region ap-northeast-2 --cluster jenkins-test-cluster --service nodejs-jenkins-service --force-new-deployment
"""
} catch (error) {
print(error)
echo 'Remove Deploy Files'
sh "rm -rf /var/lib/jenkins/workspace/nodejs-pipeline/*"
currentBuild.result = 'FAILURE'
}
}
}
post {
success {
echo "The deploy stage successfully."
}
failure {
echo "The deploy stage failed."
}
}
}
}
}
7. git push 시, trigger 발동 및 jenkins pipeline 실행
반응형
'CICD' 카테고리의 다른 글
[Jenkins] 슬레이브 노드 (agent) 생성 및 연동 with SSH (1) | 2022.12.31 |
---|---|
[Jenkins] 슬레이브 노드 (agent) 생성 및 연동 with JNLP (0) | 2022.12.30 |
[jenkins] Amazon Linux 2 에 Docker 설치 (0) | 2022.12.30 |
[jenkins] Amazon Linux 2 에 git 설치 (1) | 2022.12.30 |
[jenkins] Amazon Linux 2 에 jdk 설치 (0) | 2022.12.30 |