slackSend (channel: "#app-build-state", color: '#FF0000', message: "FAILED: Git Clone")
pipeline {
agent {
label 'Slave01'
}
environment {
AWS_REGION = credentials('AWS_REGION')
AWS_ACCOUNT_ID_ECR = credentials('AWS_ACCOUNT_ID_ECR')
githubCredential = 'github-user-account'
gitEmail = 'gugucone999@gmail.com'
gitName = 'ONE-OF-WORLD'
IMAGE_NAME = "app:${env.BUILD_NUMBER}"
}
stages {
stage('Git Clone') {
steps {
checkout scm
}
post {
failure {
echo 'Repository clone failure'
slackSend (channel: "#app-build-state", color: '#FF0000', message: "FAILED: Git Clone")
}
success {
echo 'Repository clone success'
slackSend (channel: "#app-build-state", color: '#0AC9FF', message: "SUCCESS: Git Clone")
}
}
}
stage('Docker Image Build') {
steps {
sh "docker build -t ${AWS_ACCOUNT_ID_ECR}.dkr.ecr.${AWS_REGION}.amazonaws.com/${IMAGE_NAME} --file Dockerfile ."
}
post {
failure {
echo 'Docker Image Build failure'
slackSend (channel: "#app-build-state", color: '#FF0000', message: "FAILED: Docker Image Build ${IMAGE_NAME}")
}
success {
echo 'Docker Image Build success'
slackSend (channel: "#app-build-state", color: '#0AC9FF', message: "SUCCESS: Docker Image Build ${IMAGE_NAME}")
}
}
}
stage('ECR Login') {
steps {
sh "aws ecr get-login-password --region ${AWS_REGION} | docker login --username AWS --password-stdin ${AWS_ACCOUNT_ID_ECR}.dkr.ecr.${AWS_REGION}.amazonaws.com"
}
post {
failure {
echo 'ECR login failure'
slackSend (channel: "#app-build-state", color: '#FF0000', message: "FAILED: ECR Login")
}
success {
echo 'ECR login success'
slackSend (channel: "#app-build-state", color: '#0AC9FF', message: "SUCCESS: ECR Login")
}
}
}
stage('ECR Image Push') {
steps {
sh "docker push ${AWS_ACCOUNT_ID_ECR}.dkr.ecr.${AWS_REGION}.amazonaws.com/${IMAGE_NAME}"
}
post {
failure {
echo 'ECR Push failure'
slackSend (channel: "#app-build-state", color: '#FF0000', message: "FAILED: ECR Push ${IMAGE_NAME}")
}
success {
echo 'ECR Push success'
slackSend (channel: "#app-build-state", color: '#0AC9FF', message: "SUCCESS: ECR Push ${IMAGE_NAME}")
}
}
}
stage('K8S Manifest Update') {
steps {
git credentialsId: githubCredential,
url: 'https://github.com/ONE-OF-WORLD/Deploy.git',
branch: 'main'
sh "git config --global user.email ${gitEmail}"
sh "git config --global user.name ${gitName}"
sh "sed -i -r 's|(${AWS_ACCOUNT_ID_ECR}.dkr.ecr.${AWS_REGION}.amazonaws.com/app:)[0-9]+|\\1${BUILD_NUMBER}|g' app/create-back-pod.yaml"
sh "git add ."
sh "git commit -m 'fix: Update ${IMAGE_NAME} image version'"
sh "git branch -M main"
sh "git remote remove origin"
sh "git remote add origin git@github.com:ONE-OF-WORLD/Deploy.git"
sh "git push -u origin main"
}
post {
failure {
echo 'K8S Manifest failure'
slackSend (channel: "#app-build-state", color: '#FF0000', message: "FAILED: K8S Manifest Update ${IMAGE_NAME}")
}
success {
echo 'K8S Manifest success'
slackSend (channel: "#app-build-state", color: '#0AC9FF', message: "SUCCESS: K8S Manifest Update ${IMAGE_NAME}")
}
}
}
}
}
agent {
label 'Slave02'
}
environment {
AWS_REGION = credentials('AWS_REGION')
AWS_ACCOUNT_ID_ECR = credentials('AWS_ACCOUNT_ID_ECR')
githubCredential = 'github-user-account'
gitEmail = 'gugucone999@gmail.com'
gitName = 'ONE-OF-WORLD'
IMAGE_NAME = "app:${env.BUILD_NUMBER}"
}
checkout scm
으로 해결할 수 있다. stages {
stage('Git Clone') {
steps {
checkout scm
}
post {
failure {
echo 'Repository clone failure'
slackSend (channel: "#app-build-state", color: '#FF0000', message: "FAILED: Git Clone")
}
success {
echo 'Repository clone success'
slackSend (channel: "#app-build-state", color: '#0AC9FF', message: "SUCCESS: Git Clone")
}
}
}
${IMAGE_NAME}
으로 지정된 Docker 이미지를 빌드하고, ${AWS_ACCOUNT_ID_ECR}.dkr.ecr.${AWS_REGION}.amazonaws.com
과 함께 태그를 지정하겠다!stage('Docker Image Build') {
steps {
sh "docker build -t ${AWS_ACCOUNT_ID_ECR}.dkr.ecr.${AWS_REGION}.amazonaws.com/${IMAGE_NAME} --file Dockerfile ."
}
post {
failure {
echo 'Docker Image Build failure'
slackSend (channel: "#app-build-state", color: '#FF0000', message: "FAILED: Docker Image Build ${IMAGE_NAME}")
}
success {
echo 'Docker Image Build success'
slackSend (channel: "#app-build-state", color: '#0AC9FF', message: "SUCCESS: Docker Image Build ${IMAGE_NAME}")
}
}
}
sudo chmod 666 /var/run/docker.sock
stage('ECR Login') {
steps {
sh "aws ecr get-login-password --region ${AWS_REGION} | docker login --username AWS --password-stdin ${AWS_ACCOUNT_ID_ECR}.dkr.ecr.${AWS_REGION}.amazonaws.com"
}
post {
failure {
echo 'ECR login failure'
slackSend (channel: "#app-build-state", color: '#FF0000', message: "FAILED: ECR Login")
}
success {
echo 'ECR login success'
slackSend (channel: "#app-build-state", color: '#0AC9FF', message: "SUCCESS: ECR Login")
}
}
}
sudo apt install awscli -y
aws configure
AWS Access Key ID
AWS Secret Access Key
Default region name
stage('ECR Image Push') {
steps {
sh "docker push ${AWS_ACCOUNT_ID_ECR}.dkr.ecr.${AWS_REGION}.amazonaws.com/${IMAGE_NAME}"
}
post {
failure {
echo 'ECR Push failure'
slackSend (channel: "#app-build-state", color: '#FF0000', message: "FAILED: ECR Push ${IMAGE_NAME}")
}
success {
echo 'ECR Push success'
slackSend (channel: "#app-build-state", color: '#0AC9FF', message: "SUCCESS: ECR Push ${IMAGE_NAME}")
}
}
}
image:tag
를 수정해서 올려주도록 하자! stage('K8S Manifest Update') {
steps {
git credentialsId: githubCredential,
url: 'https://github.com/ONE-OF-WORLD/Deploy.git',
branch: 'main'
sh "git config --global user.email ${gitEmail}"
sh "git config --global user.name ${gitName}"
sh "sed -i -r 's|(${AWS_ACCOUNT_ID_ECR}.dkr.ecr.${AWS_REGION}.amazonaws.com/app:)[0-9]+|\\1${BUILD_NUMBER}|g' app/create-back-pod.yaml"
sh "git add ."
sh "git commit -m 'fix: Update ${IMAGE_NAME} image version'"
sh "git branch -M main"
sh "git remote remove origin"
sh "git remote add origin git@github.com:ONE-OF-WORLD/Deploy.git"
sh "git push -u origin main"
}
post {
failure {
echo 'K8S Manifest failure'
slackSend (channel: "#app-build-state", color: '#FF0000', message: "FAILED: K8S Manifest Update ${IMAGE_NAME}")
}
success {
echo 'K8S Manifest success'
slackSend (channel: "#app-build-state", color: '#0AC9FF', message: "SUCCESS: K8S Manifest Update ${IMAGE_NAME}")
}
}
}
app/create-back-pod.yaml
이렇게 구성을 해줬다.ssh-keygen
cat .ssh/id_rsa.pub
ssh -T git@github.com
현재는 app 만 만들었는데 web도 동일한 방법으로 진행해 주면 된다!
우리가 만든 repo에 배포하고자하는 파일들을 넣어준 다음에 Argo가 바라보게 한다면 구르굿! 다음장에 설명하겠다!