Search code examples
jenkinsgroovy

Cant use changeset in JenkinsFile script


I have a problem with a step in my jenkinsfile script. I am trying to use when changeset to determine if a particular set of files has changed as I want to only build when certain files are changed. I added this step to call a separate build job if the files are changed.

        stage('File check') {
             when { changeset "**/files"}
             steps {
                  build 'Build and deploy'
             }
         }

However I get an error

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 5: Unknown conditional changeset. Valid conditionals are: allOf, anyOf, branch, environment, expression, not @ line 5, column 21.
                when { changeset "**/files"}

What am I missing? Is it a problem with my version of Jenkins/groovy? Im using Jenkins ver. 2.73.3.


Solution

  • Here is what works for me:

      String projectDirectory = "terraform"
      String changesetPathRegex = "**/${projectDirectory}/**"
    
        stage('Build Dockerfile') {
                when { changeset changesetPathRegex }
                steps {
                    dir(projectDirectory) {
                        sh 'terraform plan'
                    }
                }
            }
    

    Should look in the current repo's "terraform" folders and do the things if it sees a change there.