Reputation: 12906
In a declarative multibranch Jenkins pipeline I'd like to run a stage only if a Git tag is present. For testing reasons I've set up a simple dummy pipeline. However, the demo
stage is always skipped. I checked that a Git tag is present and that I pushed the tag as well but this doesn't seem to have any effect.
Jenkinsfile
pipeline {
agent any
environment {
APP_NAME = 'myapp'
}
stages {
stage('Demo') {
when { buildingTag() }
steps {
echo "Found a tag..."
}
}
}
}
Console output from Jenkins build
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] withCredentials
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Demo)
Stage "Demo" skipped due to when conditional
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // withCredentials
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Log output from cloning the repo
> git rev-parse --is-inside-work-tree # timeout=10
Setting origin to [email protected]:my-project/myapp.git
> git config remote.origin.url [email protected]:my-project/myapp.git # timeout=10
Fetching origin...
Fetching upstream changes from origin
> git --version # timeout=10
> git config --get remote.origin.url # timeout=10
using GIT_SSH to set credentials
> git fetch --tags --progress origin +refs/heads/*:refs/remotes/origin/*
Seen branch in repository origin/master
Seen 1 remote branch
Obtained Jenkinsfile from 1bcf713882dbdba6b8007e0f6c23b44207cbab1e
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] node
Running on vm-abn-jenk-002 in /var/lib/jenkins/workspace/build-myapp-app/master
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Checkout SCM)
[Pipeline] checkout
using credential 7253b0d3-b5a6-4569-a3ad-5392cba89d9b
Fetching changes from the remote Git repository
Checking out Revision 1bcf713882dbdba6b8007e0f6c23b44207cbab1e (master)
Commit message: "Update Jenkinsfile"
> git rev-parse --is-inside-work-tree # timeout=10
> git config remote.origin.url [email protected]:my-project/myapp.git # timeout=10
Fetching upstream changes from [email protected]:my-project/myapp.git
> git --version # timeout=10
using GIT_SSH to set credentials
> git fetch --tags --progress [email protected]:my-project/myapp.git +refs/heads/*:refs/remotes/origin/* --depth=1
> git config core.sparsecheckout # timeout=10
> git checkout -f 1bcf713882dbdba6b8007e0f6c23b44207cbab1e
> git rev-list --no-walk 1bcf713882dbdba6b8007e0f6c23b44207cbab1e # timeout=10
Upvotes: 0
Views: 4936
Reputation: 4271
Note the following:
Checking out Revision 1bcf713882dbdba6b8007e0f6c23b44207cbab1e (master)
This shows that the code is checking out a branch, not a tag. You need to check out a tag to make it work. The output would then be something like:
Checking out Revision a8deb8ef28d4b83e5f146afe132ac21dee47f225 (TEST_TAG)
Technically the tag and buildingTag conditions check whether there is a TAG_NAME environment variable set (tag also checks the value). This is set automatically when Git plugin checks out a tag, but is not available when checking out a branch, even if there are tags on the same commit.
Since it seems you are using a Multibranch pipeline, you need to start your build from Tags tab to make it work.
Upvotes: 1