Jenkins๋ ์คํ์์ค ๊ธฐ๋ฐ์ ์ง์์ ํตํฉ/์ง์์ ๋ฐฐํฌ(CI/CD) ์๋ํ ๋๊ตฌ์ ๋๋ค.
jenkins:
image: jenkins
build:
context: ./JENKINS
container_name: jenkins-container
ports:
- "9090:8080" # ์น UI ์ ์
- "50000:50000" # ์์ด์ ํธ ํต์ ํฌํธ
volumes:
- jenkins-data:/var/jenkins_home
- /var/run/docker.sock:/var/run/docker.sock
networks:
- custom-network
docker exec -it jenkins-container sh
cat /var/jenkins_home/secrets/initialAdminPassword
http://localhost:9090
Add Credentials
Secret text
github-token
SSH Username with private key
ec2-ssh-key
ec2-user
.pem
ํ์ผ ๋ด์ฉ ๋ณต๋ถ (DEPLOY_SERVER.pem
)์๋ก์ด ํ์ดํ๋ผ์ธ ์์ฑ (์: DEPLOY_TEST
)
Pipeline script
pipeline {
agent any
environment {
GITHUB_REPO = 'https://github.com/STD-INFO-COMPUTING-WEB-2024-07-30/09_DEPLOYMENT.git'
TARGET_DIR = '05_DOCKER_COMPOSE_DEPLOY'
EC2_HOST = '43.202.192.42'
EC2_USER = 'ec2-user'
}
stages {
stage('Clone and Deploy on EC2') {
steps {
withCredentials([sshUserPrivateKey(credentialsId: 'ec2-ssh-key', keyFileVariable: 'SSH_KEY')]) {
sh """
chmod 600 ${SSH_KEY}
ssh -i ${SSH_KEY} -o StrictHostKeyChecking=no ${EC2_USER}@${EC2_HOST} "
rm -rf DOCKER && \
git clone https://github.com/Jaewoong-Hwang/DOCKER.git && \
cd DOCKER/05_DOCKER_COMPOSE_DEPLOY && \
sudo docker-compose down || true && \
sudo docker rm -f $(sudo docker ps -aq) 2>/dev/null || true && \
sudo docker rmi -f $(sudo docker images -q) 2>/dev/null || true && \
sudo docker-compose build --no-cache && \
sudo docker-compose up -d --scale jenkins=0
"
"""
}
}
}
}
post {
success {
echo 'โ
Deployment to EC2 completed successfully!'
}
failure {
echo 'โ Deployment to EC2 failed!'
}
}
}