작업 파이프라인
gitlab 코드작성 -> jenkins 스크립트 실행 -> 도커 빌드 -> 내부 registry push
-> 운영서버 docker-compose 통한 도커 이미지 중지 및 실행
설치
1. 내부 테스트 서버 셋팅이기 때문에 XCP 를 서버 구성하여 xen 서버로 셋팅 진행
2. gitlab 로컬에 설치
3. jenkins 로컬에 설치
4. docker 설치
jenkins file작성
pipeline {
agent any
parameters {
booleanParam(name : 'BUILD_DOCKER_IMAGE', defaultValue : true, description : 'BUILD_DOCKER_IMAGE')
booleanParam(name : 'PUSH_DOCKER_IMAGE', defaultValue : true, description : 'PUSH_DOCKER_IMAGE')
booleanParam(name : 'DEPLOY_WORKLOAD', defaultValue : true, description : 'DEPLOY_WORKLOAD')
string(name : 'DOCKER_IMAGE_NAME', defaultValue : 'nginx-test', description : 'DOCKER_IMAGE_NAME')
string(name : 'DOCKER_TAG', defaultValue : '0.3', description : 'DOCKER_TAG')
string(name : 'ACCOUNT_ID', defaultValue : '192.168.1.4', description : 'ACCOUNT_ID')
string(name : 'TARGET_SVR_USER', defaultValue : 'centos', description : 'TARGET_SVR_USER')
string(name : 'TARGET_SVR_DIR', defaultValue : '/home/centos/', description : 'TARGET_SVR_PATH')
string(name : 'TARGET_SVR', defaultValue : '192.168.1.4', description : 'TARGET_SVR')
}
environment {
ECR_REPOSITORY = "${params.ACCOUNT_ID}:5000/${params.DOCKER_IMAGE_NAME}"
ECR_DOCKER_TAG = "${params.DOCKER_TAG}"
TARGET_USER = "${params.TARGET_SVR_USER}"
TARGET_SERVER = "${params.TARGET_SVR}"
TARGET_PATH = "${params.TARGET_SVR_DIR}"
CONTAINER_NAME = "${params.'DOCKER_IMAGE_NAME'}"
}
stages {
stage('============ Build Docker Image ============') {
when {
expression {return params.BUILD_DOCKER_IMAGE}
}
steps {
dir("${env.WORKSPACE}") {
sh 'docker build -t ${ECR_REPOSITORY}:${ECR_DOCKER_TAG} .'
}
}
post {
always {
echo "Docker build success!!"
}
}
}
stage('============ Push Docker Image ============') {
when { expression { return params.PUSH_DOCKER_IMAGE }}
steps {
sh 'docker push ${ECR_REPOSITORY}:${ECR_DOCKER_TAG}'
echo "Push Docker Image to Docker_registry"
}
}
stage('============ Deploy workload ============') {
when { expression { return params.DEPLOY_WORKLOAD }}
steps{
sshagent (credentials: ["registry-ssh"]) {
sh '''#!/bin/bash
ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no \
${TARGET_USER}@${TARGET_SERVER} \
'export IMAGE='${ECR_REPOSITORY}'; \
export TAG='${ECR_DOCKER_TAG}'; \
export CNAME='${CONTAINER_NAME}'-'${ECR_DOCKER_TAG}'; \
docker-compose -f docker-compose.yml down;
docker-compose -f docker-compose.yml up -d';
'''
echo "complate"
}
}
}
}
}