jenkinsjenkins-pipelinepipelinejenkins-groovychangeset

Ending Jenkins Pipeline Early


I have a C# repo that has multiple Projects inside of a single solution. Each Project has their own Jenkinsfile but they could share a Pipeline with another Project. Inside of their Jenkinsfile it gives a subdirectory. I am trying to get it to where it will only continue through the Pipeline if there have been changes to a subdirectory.

stage ('Check Subdirectory for Change') {
          when {
            allOf {
              expression {
                config.Project.SubDirectory != ""
              }
              not { changeset config.Project.SubDirectory + '/**' }
            }
          }
          steps {
            script {
              currentBuild.result = 'SUCCESS'
              config.ContinueBuild = false
              } 
            }
          }

This is my current setup inside the pipeline. This will do the check and then skip the stage if there weren't any changes. The problem is that if there are no changes it will skip the Check Subdirectory for Change stage and continue the build. I need it to exit the build inside and ONLY continue if there was changes to the subdirectory.


Solution

  • In summary, you need to abort the build without failing. How to abort?

    However, you are only testing the when condition into one single stage, this is why the stage is skipped but the build continuous.

    As per design, the when expression for Jenkins DSL cannot be executed outside a stage. Documentation

    So you have two alternatives:

    1. Duplicate the same when condition in each stage. Easy but inefficient.
    2. Configure an upstream job to only very if the condition is met (changes found), if so, then call the other downstream (existing) pipelines. Complex but efficient.