이전 ECS 배포 잡을 카피해서 만들어 줍시다.
변수는 아래와 같이 수정해줍니다.
그리고 스크립트를 아래와 같이 넣어주세요
pipeline {
agent any
stages {
stage('Git Clone') {
steps {
script {
try {
git url: "https://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/cb-test-api", branch: "master", credentialsId: "$GIT_CREDENTIALS_ID"
sh "sudo rm -rf ./.git"
env.cloneResult=true
} catch (error) {
print(error)
env.cloneResult=false
currentBuild.result = 'FAILURE'
}
}
}
}
stage('Build JAR') {
when {
expression {
return env.cloneResult ==~ /(?i)(Y|YES|T|TRUE|ON|RUN)/
}
}
steps {
script{
try {
sh """
rm -rf deploy
mkdir deploy
"""
sh "sudo sed -i \"s/module_name=.*/module_name=${env.JOB_NAME}\\:${env.BUILD_NUMBER}/g\" /var/lib/jenkins/workspace/${env.JOB_NAME}/src/main/resources/application.properties"
sh "cat /var/lib/jenkins/workspace/${env.JOB_NAME}/src/main/resources/application.properties"
sh 'mvn -Dmaven.test.failure.ignore=true clean install'
sh """
cd deploy
cp /var/lib/jenkins/workspace/${env.JOB_NAME}/target/*.jar ./${env.JOB_NAME}.jar
"""
env.mavenBuildResult=true
} catch (error) {
print(error)
echo 'Remove Deploy Files'
sh "sudo rm -rf /var/lib/jenkins/workspace/${env.JOB_NAME}/*"
env.mavenBuildResult=false
currentBuild.result = 'FAILURE'
}
}
}
post {
success {
slackSend channel: '#pipeline-deploy', color: 'good', message: "The pipeline ${currentBuild.fullDisplayName} stage Build JAR successfully."
}
failure {
slackSend channel: '#pipeline-deploy', color: 'danger', message: "The pipeline ${currentBuild.fullDisplayName} stage Build JAR failed."
}
}
}
stage('Docker Build'){
when {
expression {
return env.mavenBuildResult ==~ /(?i)(Y|YES|T|TRUE|ON|RUN)/
}
}
steps {
script{
try {
sh"""
#!/bin/bash
cd ./deploy
cat>Dockerfile<<-EOF
FROM ${ECR_BASE_URL}:latest
ADD ${env.JOB_NAME}.jar /home/${env.JOB_NAME}.jar
CMD nohup java -jar /home/${env.JOB_NAME}.jar 1> /dev/null 2>&1
EXPOSE 9000
EOF"""
sh"""
cd ./deploy
\$(aws ecr get-login --no-include-email --region ap-northeast-2)
docker build -t ${SERVICE_NAME.toLowerCase()} .
docker tag ${SERVICE_NAME.toLowerCase()}:latest ${ECR_TASK_URL}:ver${env.BUILD_NUMBER}
docker push ${ECR_TASK_URL}:ver${env.BUILD_NUMBER}
"""
echo 'Remove Deploy Files'
sh "sudo rm -rf /var/lib/jenkins/workspace/${env.JOB_NAME}/*"
env.dockerBuildResult=true
} catch (error) {
print(error)
echo 'Remove Deploy Files'
sh "sudo rm -rf /var/lib/jenkins/workspace/${env.JOB_NAME}/*"
env.dockerBuildResult=false
currentBuild.result = 'FAILURE'
}
}
}
post {
success {
slackSend channel: '#jenkins', color: 'good', message: "The pipeline ${currentBuild.fullDisplayName} stage Docker Build successfully."
}
failure {
slackSend channel: '#jenkins', color: 'danger', message: "The pipeline ${currentBuild.fullDisplayName} stage Docker Build failed."
}
}
}
stage('Deploy'){
when {
expression {
return env.dockerBuildResult ==~ /(?i)(Y|YES|T|TRUE|ON|RUN)/
}
}
steps {
script{
try {
withAWS(credentials:"$AWS_CREDENTIALS") {
sh "aws ecs describe-task-definition --task-definition ${TASK_DEFINITION} --region ap-northeast-2 --query \"taskDefinition.{\"family\": family, \"networkMode\": networkMode,\"containerDefinitions\": containerDefinitions, \"executionRoleArn\": executionRoleArn,\"requiresCompatibilities\": requiresCompatibilities,\"cpu\":cpu, \"memory\":memory}\" --output json > task-definition.json"
def task_repository_name = sh(
script:"""
echo \"${ECR_TASK_URL}\" | awk \'{ split(\$0, arr, \"/\"); print arr[2] }\'
""",
returnStdout: true
).trim()
sh "sudo sed -i \"9s/${task_repository_name}.*/${task_repository_name}:ver${env.BUILD_NUMBER}\\\",/g\" task-definition.json"
sh "aws ecs register-task-definition --cli-input-json file://task-definition.json --region ap-northeast-2"
sh "aws ecs update-service --cluster ${CLUSTER_NAME} --service ${SERVICE_NAME} --task-definition ${TASK_DEFINITION} --region ap-northeast-2"
}
} catch (error) {
print(error)
echo 'Remove Deploy Files'
sh "sudo rm -rf /var/lib/jenkins/workspace/${env.JOB_NAME}/*"
currentBuild.result = 'FAILURE'
}
}
}
post {
success {
slackSend channel: '#jenkins', color: 'good', message: "The pipeline ${currentBuild.fullDisplayName} successfully."
}
failure {
slackSend channel: '#jenkins', color: 'danger', message: "The pipeline ${currentBuild.fullDisplayName} failed."
}
}
}
}
}
배포는 뭐 크게 문제가 없습니다
잘 수행된 것을 볼 수 있습니다.
배포가 수행되고, CLI를 통해 서비스가 새롭게 뜬 것을 볼 수 있습니다.
새롭게 타겟그룹에도 파게이트 컨테이너가 띄워지고,
러닝까지 잘 수행된 다음 헬스체크도 잘 되는것을 볼 수 있습니다.
URL로 접속 시에도 문제가 없는 것을 볼 수 있습니다.
다음 포스트에서는 Blue Green 배포에 대해 알아보도록 하겠습니다.
'CICD > ECS' 카테고리의 다른 글
ECS CICD Automation - 8. Jenkins Job (ECS Fargate Blue Green) (0) | 2020.01.31 |
---|---|
ECS CICD Automation - 8. ECS Fargate Blue/Green 생성 (0) | 2020.01.31 |
ECS CICD Automation - 6. ECS Fargate 생성 (0) | 2020.01.31 |
ECS CICD Automation - 5. Jenkins Job (Blue/Green) (0) | 2020.01.30 |
ECS CICD Automation - 4. Jenkins Job (Rolling) (0) | 2020.01.30 |