Skip to content

Instantly share code, notes, and snippets.

@garystafford
Last active April 22, 2021 17:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save garystafford/c4dc2c4bbcaaa8f05560f45e3c2e515f to your computer and use it in GitHub Desktop.
Save garystafford/c4dc2c4bbcaaa8f05560f45e3c2e515f to your computer and use it in GitHub Desktop.
#!/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