diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..946096e --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,66 @@ +pipeline { + agent any + + environment { + REGISTRY = 'git.virtheart.com' + REGISTRY_OWNER = 'virtheart' + REGISTRY_USERNAME = 'chenchun@virtheart.com' + IMAGE_NAME = "${env.JOB_NAME.replace(' ', '-').toLowerCase()}" + IMAGE_TAG = "${env.BUILD_NUMBER}" + } + + stages { + stage('Checkout') { + steps { + checkout scm + } + } + + stage('Build Image') { + steps { + script { + def imageFullName = "${REGISTRY}/${REGISTRY_OWNER}/${IMAGE_NAME}:${IMAGE_TAG}" + sh """ + docker build -t ${imageFullName} . + """ + } + } + } + + stage('Push Image') { + steps { + withCredentials([usernamePassword(credentialsId: 'docker-registry-credentials', usernameVariable: 'REGISTRY_USERNAME', passwordVariable: 'REGISTRY_PASSWORD')]) { + script { + def imageFullName = "${REGISTRY}/${REGISTRY_OWNER}/${IMAGE_NAME}:${IMAGE_TAG}" + sh """ + echo ${REGISTRY_PASSWORD} | docker login ${REGISTRY} -u ${REGISTRY_USERNAME} --password-stdin + docker push ${imageFullName} + docker rmi ${imageFullName} + """ + } + } + } + } + + stage('Deploy Container') { + steps { + script { + def imageFullName = "${REGISTRY}/${REGISTRY_OWNER}/${IMAGE_NAME}:${IMAGE_TAG}" + sh """ + docker stop ${IMAGE_NAME} || true + docker rm ${IMAGE_NAME} || true + docker run -d --name ${IMAGE_NAME} \ + --restart unless-stopped \ + ${imageFullName} + """ + } + } + } + } + + post { + always { + cleanWs() + } + } +} \ No newline at end of file