✔️ SSH Agent 추가 (plugins에서 추가해주면 된다.)
✔️ Credentials에 ec2 추가
Kind
: SSH Username with private key를 선택ID
: 하고 싶은 IDUsername
: ubuntu
✔️ 완성된 pipeline
pipeline {
agent any
environment {
imagename = "lkc263/d208backend"
registryCredential = 'DockerHub_IdPwd'
dockerImage = ''
}
stages {
stage('git clone') {
steps {
dir('study_docker') {
checkout scmGit(branches: [[name: '*/main']], extensions: [submodule(parentCredentials: true, reference: '', trackingSubmodules: true)], userRemoteConfigs: [[credentialsId: 'github-repo-user', url: 'https://github.com/ToDoStudy/study_docker']])
}
}
}
stage('build') {
steps {
dir('study_docker/chuno') {
sh "chmod +x gradlew"
sh "./gradlew clean bootJar"
}
}
}
stage('docker-build'){
steps {
echo 'Build Docker'
dir('study_docker/chuno'){
script {
sh "pwd"
dockerImage = docker.build imagename
}
}
}
}
stage('docker-push'){
steps{
echo 'Push Docker'
script {
docker.withRegistry('', registryCredential){
dockerImage.push("1.0")
}
}
}
}
stage('SSH-Server-EC2'){
steps {
echo 'SSH'
sshagent(credentials: ['ECGumD208']) {
sh 'ssh -o StrictHostKeyChecking=no ubuntu@52.79.219.150 "whoami"'
sh 'ssh -o StrictHostKeyChecking=no ubuntu@52.79.219.150 "docker pull lkc263/d208backend:1.0"'
sh 'ssh -o StrictHostKeyChecking=no ubuntu@52.79.219.150 "docker run lkc263/d208backend:1.0"'
}
}
}
}
}
-o StrictHostKeyChecking=no
: ssh접속 시 host의 key를 확인하지 않는다.
✔️ pipeline
clone -> build -> docker-build -> docker-push -> SSH-Server-EC2
pipeline {
agent any
environment {
imagename = "lkc263/d208backend"
registryCredential = 'DockerHub_IdPwd'
dockerImage = ''
}
stages {
stage('git clone') {
steps {
dir('study_docker') {
checkout scmGit(branches: [[name: '*/main']], extensions: [submodule(parentCredentials: true, reference: '', trackingSubmodules: true)], userRemoteConfigs: [[credentialsId: 'github-repo-user', url: 'https://github.com/ToDoStudy/study_docker']])
}
}
}
stage('build') {
steps {
dir('study_docker/chuno') {
sh "chmod +x gradlew"
sh "./gradlew clean bootJar"
}
}
}
stage('docker-build'){
steps {
echo 'Build Docker'
dir('study_docker/chuno'){
script {
sh """
if ! test docker; then
curl -fsSL https://get.docker.com -o get-docker.sh
sh get-docker.sh
fi
"""
dockerImage = docker.build imagename
}
}
}
}
stage('docker-push'){
steps{
echo 'Push Docker and delete image'
sshagent(credentials: ['ECGumD208']) {
sh '''
if test "`docker ps -aq --filter ancestor=lkc263/d208backend:1.0`"; then
ssh -o StrictHostKeyChecking=no ubuntu@52.79.219.150 "docker stop $(docker ps -aq --filter ancestor=lkc263/d208backend:1.0)"
ssh -o StrictHostKeyChecking=no ubuntu@52.79.219.150 "docker rm -f $(docker ps -aq --filter ancestor=lkc263/d208backend:1.0)"
ssh -o StrictHostKeyChecking=no ubuntu@52.79.219.150 "docker rmi lkc263/d208backend:1.0"
fi
'''
}
script {
docker.withRegistry('', registryCredential){
dockerImage.push("1.0")
}
}
}
}
stage('SSH-Server-EC2'){
steps {
echo 'SSH'
sshagent(credentials: ['ECGumD208']) {
sh 'ssh -o StrictHostKeyChecking=no ubuntu@52.79.219.150 "docker pull lkc263/d208backend:1.0"'
sh 'ssh -o StrictHostKeyChecking=no ubuntu@52.79.219.150 "docker run -i -p 8080:9999 -d lkc263/d208backend:1.0"'
}
}
}
}
}
✔️ docker image 만들 때 만약 아직 docker가 설치되지 않았다면 설치
sh """
if ! test docker; then
curl -fsSL https://get.docker.com -o get-docker.sh
sh get-docker.sh
fi
"""
✔️ push 할 때, ec2 가상서버가 실행되고 있다면 종료시키기
새로운 가상 서버를 open 하기 위해 이전 가상서버를 종료시킨다.
sshagent(credentials: ['ECGumD208']) {
sh '''
if test "`docker ps -aq --filter ancestor=lkc263/d208backend:1.0`"; then
ssh -o StrictHostKeyChecking=no ubuntu@52.79.219.150 "docker stop $(docker ps -aq --filter ancestor=lkc263/d208backend:1.0)"
ssh -o StrictHostKeyChecking=no ubuntu@52.79.219.150 "docker rm -f $(docker ps -aq --filter ancestor=lkc263/d208backend:1.0)"
ssh -o StrictHostKeyChecking=no ubuntu@52.79.219.150 "docker rmi lkc263/d208backend:1.0"
fi
'''
}
✔ image를 pull 받고, port 8080에 9999번 포트를 뛰운다.
sshagent(credentials: ['ECGumD208']) {
sh 'ssh -o StrictHostKeyChecking=no ubuntu@52.79.219.150 "docker pull lkc263/d208backend:1.0"'
sh 'ssh -o StrictHostKeyChecking=no ubuntu@52.79.219.150 "docker run -i -p 8080:9999 -d lkc263/d208backend:1.0"'
}
참고