Last active
April 22, 2021 17:18
-
-
Save garystafford/c4dc2c4bbcaaa8f05560f45e3c2e515f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env groovy | |
// Deploy Hello World Service Stack | |
node('java') { | |
properties([parameters([ | |
choice(choices: ["ci", "dev", "test", "uat"].join("\n"), | |
description: 'Environment', name: 'ENVIRONMENT') | |
])]) | |
stage('Git Checkout') { | |
dir('service') { | |
git branch: 'master', | |
credentialsId: 'jenkins_github_credentials', | |
url: 'ssh://git@garystafford/hello-world.git' | |
} | |
dir('credentials') { | |
git branch: 'master', | |
credentialsId: 'jenkins_github_credentials', | |
url: 'ssh://git@garystafford/ucp-bundle-jenkins.git' | |
} | |
} | |
dir('service') { | |
stage('Build and Unit Test') { | |
sh './gradlew clean cleanTest build' | |
} | |
withEnv(["IMAGE=hello-world:${BUILD_NUMBER}"]) { | |
stage('Docker Build Image') { | |
withCredentials([[$class: 'StringBinding', | |
credentialsId: 'docker_username', | |
variable: 'DOCKER_PASSWORD'], | |
[$class: 'StringBinding', | |
credentialsId: 'docker_username', | |
variable: 'DOCKER_USERNAME']]) { | |
sh "docker login -u ${DOCKER_USERNAME} -p ${DOCKER_PASSWORD} ${DTR_URL}" | |
} | |
sh "docker build --no-cache -t ${DTR_URL}/${IMAGE} ." | |
} | |
stage('Docker Push Image') { | |
sh "docker push ${DTR_URL}/${IMAGE}" | |
} | |
withEnv(['DOCKER_TLS_VERIFY=1', | |
"DOCKER_CERT_PATH=${WORKSPACE}/credentials/", | |
"DOCKER_HOST=${DOCKER_HOST}"]) { | |
stage('Docker Stack Deploy') { | |
try { | |
sh "docker service rm ${params.ENVIRONMENT}_hello-world" | |
sh 'sleep 30s' // wait for service to be completely removed if it exists | |
} catch (err) { | |
echo "Error: ${err}" // catach and move on if it doesn't already exist | |
} | |
sh "docker stack deploy \ | |
--compose-file=docker-compose.yml ${params.ENVIRONMENT}" | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment