Skip to content

Instantly share code, notes, and snippets.

@paulolorenzobasilio
Last active August 2, 2020 17:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save paulolorenzobasilio/2c3ebfb8077ecc1d3fdb77d527fa8822 to your computer and use it in GitHub Desktop.
Save paulolorenzobasilio/2c3ebfb8077ecc1d3fdb77d527fa8822 to your computer and use it in GitHub Desktop.
Build & deploy a docker app using jenkinsfile
#!/usr/bin/env groovy
pipeline {
agent any
stages {
stage('Build'){
steps {
echo 'Building...'
script {
/**
* login to docker for private repository
* create credentials in jenkins page.
**/
withCredentials([usernamePassword(credentialsId: 'docker-login-creds', passwordVariable: 'password', usernameVariable: 'username')]){
sh '''
echo "${password} | docker login -u ${username} --password-stdin"
'''
def app = docker.build("docker-image")
app.push("latest")
}
}
}
}
stage('Test'){
steps {
echo 'Testing...'
}
}
stage('Deploy'){
steps {
sh "exit"
withCredentials([usernamePassword(credentialsId: 'docker-login-creds', passwordVariable: 'password', usernameVariable: 'username')]){
/**
* Restart docker server
**/
sh '''
echo "${password} | docker login -u ${username} --password-stdin"
docker stop docker_image
docker rm docker_image
docker pull docker_image:latest
docker run -d -p 80:80 --name docker-image-name -t docker_image:latest
'''
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment