1. ์ ํจ์ค๋ ๋ฌด์์ธ๊ฐ?
๋จผ์ ์ ํจ์ค๋ฅผ ์์๋ณด๊ธฐ ์ , CI/CD์ ๋ํ ์ดํด๋ฅผ ํด์ผ ํ๋ค.
- CI (Continuous Integration)
- ์ง์์ ์ธ ํตํฉ
- ์ฌ๋ฌ ๊ฐ๋ฐ์ ๋ค์ด ์ง์์ ์ผ๋ก ์ฝ๋๋ฅผ ํตํฉํ๋ ๊ฒ
- CD (Continuous Deployment / Continuous Delivery)
- ์ง์์ ์ธ ๋ฐฐํฌ
- ๋ณ๊ฒฝ ์ฌํญ๋ค์ ํ ์คํธ์ ๋น๋๋ฅผ ๊ฑฐ์น๊ณ , ์ฌ์ฉ์์๊ฒ ์๋์ผ๋ก ๋ฐฐํฌ
CI/CD ๊ณผ์ ์ธ ์์ ๊ทธ๋ฆผ๊ณผ ๊ฐ๋ค.
1. ๋ก์ปฌ์์ ๊ฐ๋ฐ ํ ์ปค๋ฐ
2. Github WebHook ์ ์ด์ฉํด ์ ํจ์ค์๊ฒ ์ ๋ฌ
3. ๋น๋๊ฐ ์ฑ๊ณตํ๊ฒ ๋๋ฉด ๋ฐฐํฌ ์๋ฒ๋ก ์ ์ก
4. ๋น๋๊ฐ ์คํจํ ๊ฒฝ์ฐ ๋ก๊ทธ๋ฅผ ๋จ๊ธฐ๊ณ ์๋ฆผ
๋ค์์ ํ๋ก์ ํธ ์งํ ์ ํ์ดํ๋ผ์ธ์ ๊ตฌ์ฑํ ์์์ด๋ค. ์ ํจ์ค + ๋์ปค๋ฅผ ์ด์ฉํด ์ ํจ์ค ์๋ฒ๋ฅผ ๋ง๋ค์๊ณ ๊ทธ ์์์ ๋์ปค๋ฅผ ์ฌ์ฉํ์๋ค. ๋์ปค์ธ๋์ปค ๋ฐฉ์์ผ๋ก ์ฒ์์ ๊ตฌ์ถ์ ํ๋ค๊ฐ ํ๋์ ์ธ์คํด์ค์์ ์ด์์๋ฒ์ ๊ฐ๋ฐ์๋ฒ๋ฅผ ๋๋์ด์ผ ํด์ docker daemon์ ์ด์ฉํด ๋์ปค์์์ค๋ธ๋์ปค๋ก ๋ณ๊ฒฝํ์๋ค.
ํ์ดํ ๋ผ์ธ์ ๊ตฌ์ฑ์ agent section, post section, stages section, declaratives, steps ์ผ๋ก ์ด๋ฃจ์ด์ ธ ์๋ค.
pipeline {
agent any
environment {
DOCKER_IMAGE = '์ด๋ฏธ์ง ์ด๋ฆ'
DOCKER_CONTAINER = '์ปจํ
์ด๋ ์ด๋ฆ'
DOCKER_HUB_IMAGE = '๋์ปค ํ๋ธ ์ด๋ฏธ์ง ์ด๋ฆ'
TARGET_DIRECTORY = '์ด๋ํ๊ณ ์ถ์ dir'
}
stages {
stage('Clone Git Repository') {
steps {
git url: 'github URL'
echo 'Git Clone SUCCESS'
}
post {
success {
echo 'Git Clone stage succeeded!'
}
failure {
echo 'Git Clone stage failed!'
}
}
}
stage('Stop and Remove Docker Container') {
when {
expression { isContainerRunning() }
}
steps {
script {
// If the container is running, stop it
sh "docker stop ${DOCKER_CONTAINER}"
// Remove the stopped container
sh "docker rm ${DOCKER_CONTAINER}"
}
}
post {
success {
echo 'Remove Docker Container Success!'
}
failure {
echo 'Remove Docker Container Fail!'
}
}
}
stage('Remove Existing Docker Image') {
when {
expression { doesImageExist() }
}
steps {
script {
// If the Docker image exists, remove it
echo "Docker image : ${DOCKER_IMAGE}"
sh "docker rmi ${DOCKER_IMAGE}"
}
}
post {
success {
echo 'Remove Existing Docker Image Success!'
}
failure {
echo 'Remove Existing Docker Image Fail!'
}
}
}
stage('Build Docker Image') {
steps {
script {
dir(TARGET_DIRECTORY) {
// Build the Docker image from the Dockerfile in the current directory
sh "docker build -t ${DOCKER_IMAGE} ."
}
}
}
post {
success {
echo 'Build Docker Image Success!'
}
failure {
echo 'Build Docker Image Fail!'
}
}
}
stage('Push Docker Image to Docker Hub') {
steps {
script {
withCredentials([usernamePassword(credentialsId: 'docker-hub', usernameVariable: 'DOCKER_HUB_USERNAME', passwordVariable: 'DOCKER_HUB_PASSWORD')]) {
sh "docker login -u $DOCKER_HUB_USERNAME -p $DOCKER_HUB_PASSWORD"
sh "docker tag ${DOCKER_IMAGE} ${DOCKER_HUB_IMAGE}:latest"
sh "docker push ${DOCKER_HUB_IMAGE}:latest"
}
}
}
post {
success {
echo 'Push Docker Image to Docker Hub Success!'
}
failure {
echo 'Push Docker Image to Docker Hub Fail!'
}
}
}
stage('Pull and Run Docker Container') {
steps {
script {
dir(TARGET_DIRECTORY) {
// Pull the Docker image from Docker Hub
sh "docker pull ${DOCKER_HUB_IMAGE}:latest"
// Run the Docker container
sh "docker run -d -p 80:80 -p 443:443 --name ${DOCKER_CONTAINER} ${DOCKER_HUB_IMAGE}:latest"
}
}
}
post {
success {
echo 'Docker Image Pull And Run Success!'
}
failure {
echo 'Docker Image Pull And Run Fail!'
}
}
}
}
post {
success {
echo 'Pipeline executed successfully!'
}
}
}
def isContainerRunning() {
def isRunning = sh(script: "docker ps -q -f name=${DOCKER_CONTAINER}", returnStatus: true)
return isRunning == 0
}
def doesImageExist() {
def imageExists = sh(script: "docker images -q ${DOCKER_IMAGE}", returnStatus: true)
return imageExists == 0
}
๋ด ๋๋ฆ๋๋ก ์ฐพ์๋ณด๊ณ ํ์ดํ๋ผ์ธ์ ์์ฑํ์๋ค. ์ ํจ์ค ์ค์ ๊ณผ ์ค์น๋ ๋ค์์ ๋ค๋ฃจ๋๋ก ํ๊ณ ์ด๋ฌํ ํ์ดํ ๋ผ์ธ์ ๊ตฌ์ฑํ์ฌ ๊ฐ๋ฐ์๊ฐ ๋ก์ปฌ์์ ์ปค๋ฐ ์ ์๋์ผ๋ก ์ ํจ์ค์์ ๋น๋ ํ ๋ฐฐํฌ๊น์ง ๊ณผ์ ์ ๋ณด์ฌ์ฃผ์๋ค.
์์ธํ ๋ด์ฉ์ ๋ค์ ๊ธ์์ ๋ง๋๋ณด๋๋ก..