Jenkins: PR pipeline triggered by different PR

I have the following Jenkinsfile:

pipeline {
    environment {
        STAGING_BRANCH = 'project'
    }
    agent any
    stages {
        stage('Staging Environment') {
            steps {
                sh "sh /bin/create-staging-env"
                }
            }
            when {
                expression { env.CHANGE_TARGET == env.STAGING_BRANCH && env.CHANGE_ID }
            }
        }
    }
}

The condition expression { env.CHANGE_TARGET == env.STAGING_BRANCH && env.CHANGE_ID } is meant to only execute the stage when it is a pull request and the target branch is project.

I have this pull request which only has 1 commit:

enter image description here

But jenkins ran this pipeline multiple (7) times:

enter image description here

My guess for the trigger of the additional builds is that when a commit is pushed to another branch, and that branch has a pull request.

Edit Now I understand why the builds were created. They were caused by commits to the target branch project, since the target branch changed by a commit, Jenkins will execute the pipeline for that PR again.

Then my question changes to, How to get which branch the commit that triggered this pipeline is pushed into. For example if a commit is pushed to the project branch I would like to ignore the stage for PRs. I want to do something like expression { ... && env.COMMIT_BRANCH != 'project'}



Read more here: https://stackoverflow.com/questions/67004462/jenkins-pr-pipeline-triggered-by-different-pr

Content Attribution

This content was originally published by Obay Abd-Algader at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: