Reputation: 12896
I'm writing a declarative Jenkinsfile
which looks like this. In the stage "build" I define the variable customImage
which I would like to use in the stage "Push".
Unfortunately I cannot get this to work.
pipeline {
agent any
stages {
stage("Build") {
steps {
script {
def commitHash = GIT_COMMIT.take(7)
echo "Building Docker image for commit hash: " + commitHash
def customImage = docker.build("myimage:${commitHash}")
}
}
}
stage("Push") {
steps {
echo "Pushing Docker image to registry..."
script {
docker.withRegistry(REGISTRY_SERVER, REGISTRY_CREDENTIALS) {
$customImage.push()
}
}
}
}
}
}
Upvotes: 1
Views: 303
Reputation: 37600
You just have to define the variable at a scope, where you can access it later, i.e.
def customImage
pipeline {
agent any
stages {
stage("Build") {
steps {
script {
def commitHash = GIT_COMMIT.take(7)
echo "Building Docker image for commit hash: " + commitHash
customImage = docker.build("myimage:${commitHash}")
}
}
}
stage("Push") {
steps {
echo "Pushing Docker image to registry..."
script {
docker.withRegistry(REGISTRY_SERVER, REGISTRY_CREDENTIALS) {
customImage.push()
}
}
}
}
}
}
Upvotes: 2