Files
the-example-app-nodejs/DockerPipelinePluginJenkinsfile

55 lines
1.6 KiB
Plaintext

// You need a valid credentials for Docker Hub
// You need https://plugins.jenkins.io/docker-workflow/ on your jenkins instance
pipeline {
agent {
node {
label 'docker' //Agent needs to have Docker Engine installed
}
}
environment {
// Executem una comanda shell per obtenir la data actual i la guardem com a variable
DATA = sh(returnStdout: true, script: 'date +%Y-%m-%d').trim()
DOCKER_HUB_USER='guillemhs'
IMAGE_NAME="${DOCKER_HUB_USER}/the-example-app-nodejs"
REGISTRY_CRED_ID='docker-credentials'
TAG="${DATA}-${env.BUILD_NUMBER}"
}
stages {
stage('Checkout') {
steps {
// Get some code from the sane repository
checkout scm
}
}
stage('Docker Build') {
steps {
script{
echo "Build image ..."
docker.build("${IMAGE_NAME}:${env.BUILD_NUMBER}")
}
}
}
stage('Docker Push')
{
steps{
script{
docker.withRegistry('',REGISTRY_CRED_ID){
def app = docker.image("${IMAGE_NAME}:${TAG}")
echo "Pushing image to Docker Hub ..."
app.push("${TAG}")
app.push("latest")
}
}
}
}
}
post{
always{
cleanWs()
sh "docker rmi ${TAG} || true"
sh "docker rmi ${IMAGE_NAME}:latest || true"
}
}
}