Skip to content

Instantly share code, notes, and snippets.

@mugithi
Created November 1, 2016 02:42
Show Gist options
  • Save mugithi/a81d2a9ed4a45fc119fd58470a519b27 to your computer and use it in GitHub Desktop.
Save mugithi/a81d2a9ed4a45fc119fd58470a519b27 to your computer and use it in GitHub Desktop.
// Push the build to the nodes labled staging
node ('stage'){
//Perform a build on recieving a webhook from the github repo
stage ('Code Pull on Github') {
git credentialsId: 'github',
url: 'https://github.com/mugithi/blog.git'
}
//Build the container and tag it with abuild tag of the job
stage ('Container Image Builds') {
sh 'sudo docker build -t mugithi/blog:${BUILD_TAG} .'
}
//Set dockerhub credentials and use them to push to dockerhub
stage ('Image Push to Dockerhub') {
withCredentials([[$class: 'UsernamePasswordMultiBinding',
// set the dockerhub credentials
credentialsId: 'dockerhub',
passwordVariable: 'DOCKER_PASSWORD',
usernameVariable: 'DOCKER_USERNAME']]) {
sh '''sudo docker login -u ${DOCKER_USERNAME} -p ${DOCKER_PASSWORD} -e [email protected]
sudo docker push mugithi/blog:${BUILD_TAG}'''
}
}
// Update the staging environment and push out a URL for the user to view the website
stage ('Update staged-blog.isaack.io') {
//stop any running containers
sh '''CID=`sudo docker ps -qa`; if [ -n "$CID" ]; then sudo docker stop $CID && sudo docker rm $CID; else echo "No Container is running" ; fi'''
//run the latest build of the container over port 80
sh 'sudo docker run -itd -p 80:4000 --name isaack.io mugithi/blog:${BUILD_TAG} serve --host= 0.0.0.0'
sh 'sudo docker ps -qa'
//return the url to the user where to view the blog
sh '''set +x; echo "Please naviate to http://"`curl ifconfig.ca`; set -x'''
}
// Check with the user if staging is ready to be pushed to Production
stage ('Promote to Productions') {
//Send user prompt with yes/no prompt on whether you can push site to production
def userInput = input id: 'Promote', message: 'Promote This to Production?', parameters: [choice(choices: 'Yes\nNo', description: '', name: 'answer')]
echo ("Answer: ${userInput}")
//If yes push to production
if (userInput == "Yes") {
node ('production') {
def DOCKER_CONTAINER_NAME = "blog.isaack.io"
sh '''CID=`sudo docker ps -qa`; if [ -n "$CID" ]; then sudo docker stop $CID && sudo docker rm $CID; else echo "No Container is running" ; fi'''
sh 'sudo docker run -itd -p 80:4000 --name isaack.io mugithi/blog:${BUILD_TAG} serve --host= 0.0.0.0'
// sh 'sudo docker run -itd -p 80:4000 --name isaack.io mugithi/blog:${BUILD_TAG} serve -H 0.0.0.0'
sh 'sudo docker ps -qa'
sh '''set +x; echo "Please naviate to http://"`curl ifconfig.ca`; set -x'''
//blog.run('-itd -p 8080:4000')
}
}
else {
echo ("Time to get some work done")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment