pipeline { agent any options { timestamps() disableConcurrentBuilds() } environment { // ===== Registry ===== REGISTRY_HOST = "registry.pjt.kr" IMAGE_REPO = "alist/api" DOCKER_CRED = "registry-pjt" // ===== api ssh ===== SSH_HOST = "121.160.234.222" SSH_PORT = "2001" SSH_USER = "bigfuntnp" SSH_COMPOSE_DIR = "/srv/project/alist/compose" SSH_COMPOSE_FILE = "api-compose.pjt.yml" // ===== jenkins workspace path ===== WORK_COMPOSE_DIR = "deploy/pjt/compose" WORK_COMPOSE_FILE = "api-compose.pjt.yml" } stages { stage('1) Git check out') { steps { checkout scm } } stage('2) Build (Gradle)') { steps { sh ''' set -e chmod +x ./gradlew ./gradlew clean build -x test ''' } } stage('3) image tab create') { steps { script { env.SHORT_SHA = sh(script: "git rev-parse --short HEAD", returnStdout: true).trim() echo "SHORT_SHA = ${env.SHORT_SHA}" env.IMAGE_TAG = "${env.BUILD_NUMBER}-${env.SHORT_SHA}" echo "IMAGE_TAG = ${env.IMAGE_TAG}" } } } stage('4) Docker Build') { steps { sh ''' set -e docker build -t ${REGISTRY_HOST}/${IMAGE_REPO}:${IMAGE_TAG} . ''' } } stage('5) Docker Login & Push') { steps { withCredentials([usernamePassword( credentialsId: DOCKER_CRED, usernameVariable: 'DOCKER_USER', passwordVariable: 'DOCKER_PASS' )]) { sh ''' set -e echo "$DOCKER_PASS" | docker login ${REGISTRY_HOST} \ -u "$DOCKER_USER" --password-stdin docker push ${REGISTRY_HOST}/${IMAGE_REPO}:${IMAGE_TAG} docker logout ${REGISTRY_HOST} ''' } } } stage('6) Copy compose') { steps { sshagent(['ssh-pjt']) { sh ''' set -e SRC="${WORK_COMPOSE_DIR}/${WORK_COMPOSE_FILE}" DST="${SSH_USER}@${SSH_HOST}:${SSH_COMPOSE_DIR}/${SSH_COMPOSE_FILE}" scp -P ${SSH_PORT} -o StrictHostKeyChecking=no "$SRC" "$DST" ''' } } } stage('7) Remote docker login') { steps { sshagent(['ssh-pjt']) { withCredentials([usernamePassword( credentialsId: DOCKER_CRED, usernameVariable: 'DOCKER_USER', passwordVariable: 'DOCKER_PASS' )]) { sh ''' set -e ssh -p ${SSH_PORT} \ -o StrictHostKeyChecking=no \ -o UserKnownHostsFile=/dev/null \ ${SSH_USER}@${SSH_HOST} \ 'set -e REGISTRY_HOST='"${REGISTRY_HOST}"' DOCKER_USER='"${DOCKER_USER}"' DOCKER_PASS='"${DOCKER_PASS}"' echo "$DOCKER_PASS" | docker login "$REGISTRY_HOST" -u "$DOCKER_USER" --password-stdin ' ''' } } } } stage('8) Remote docker pull') { steps { sshagent(['ssh-pjt']) { sh ''' set -e ssh -p ${SSH_PORT} -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null \ ${SSH_USER}@${SSH_HOST} \ 'set -e cd '"${SSH_COMPOSE_DIR}"' export IMAGE_TAG='"${IMAGE_TAG}"' docker compose --env-file /srv/project/alist/env/api/.env -f '"${SSH_COMPOSE_FILE}"' pull ' ''' } } } stage('9) Remote docker up') { steps { sshagent(['ssh-pjt']) { sh ''' set -e ssh -p ${SSH_PORT} -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null \ ${SSH_USER}@${SSH_HOST} \ 'set -e cd '"${SSH_COMPOSE_DIR}"' export IMAGE_TAG='"${IMAGE_TAG}"' docker compose --env-file /srv/project/alist/env/api/.env -f '"${SSH_COMPOSE_FILE}"' up -d --remove-orphans ' ''' } } } } }