Search code examples
jenkinsgroovycontinuous-integrationjenkins-pipeline

Why is a certain stage skipped in my Jenkinsfile when I use the 'when' keyword?


I apologise if this is a bit hard to articulate, please let me know if any more details are needed.

I am trying to get a function to run when there has been a change to the requirements.txt file

Here is my code:

stage('Testing push of requirements') {
    when{ expression { return fileExists('build-scripts/pushreqs.sh') } }
    steps {
        requirementsPusher()
    }
}
stage('Create Jira Ticket') {
    when {changeset "requirements.txt"}
    steps{
        jiraTicketCreator()
    }
}
                              

The first stage block is irrelevant but it is used for comparison/reference.

When I run the above as it is, the Testing push of requirements appears in the build, but it completely skips over the Create Jira Ticket stage in the current build, where there IS a change to requirements.txt

Any ideas why this is? Could this be an incorrect syntax?


Solution

  • Probably requirements.txt file is not in the root directory, generally, the changeset contains the full qualified path of the file, hence try refining your file pattern. Example below.

    when { changeset "**/requirements.txt" }
    

    You can also print the changeset and see what you are getting. You can access the changeset like currentBuild.changeSets