Last active
September 19, 2021 05:38
-
-
Save VireshDoshi/626f7d39332971e4d349eaf851a7fe9d to your computer and use it in GitHub Desktop.
Jenkinsfille configuration - build and push image to docker hub
This file contains 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
node { | |
def myImage | |
stage('Clone repository') { | |
/* Let's make sure we have the repository cloned to our workspace */ | |
dir('.') { | |
git url: 'https://github.com/VireshDoshi/jen-build-node-app-aws.git' | |
} | |
} | |
stage('Build image') { | |
/* This builds the actual image; synonymous to | |
* docker build on the command line */ | |
myImage = docker.build("vireshdoshi/jen-build-node-app-aws:${env.BUILD_NUMBER}") | |
} | |
stage('Test image') { | |
/* Ideally, we would run a test framework against our image. */ | |
myImage.inside { | |
sh 'echo "Tests passed"' | |
sh 'ls -ltr' | |
} | |
} | |
stage('Push image') { | |
/* Finally, we'll push the image with two tags: | |
* First, the incremental build number from Jenkins | |
* Second, the 'latest' tag. */ | |
withCredentials([usernamePassword( credentialsId: 'docker-hub-credentials', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) { | |
docker.withRegistry('', 'docker-hub-credentials') { | |
sh "docker login -u ${USERNAME} -p ${PASSWORD}" | |
myImage.push("${env.BUILD_NUMBER}") | |
myImage.push("latest") | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment