Search code examples
jenkinsjenkins-pipelinejenkins-pluginswebhooksbitbucket-server

Jenkins webhook build pull requests from forks


We are using bitbucket-push-and-pull-request plugin in order to build our project. The pipeline is set to checkout the source repo when webhook is triggered and everything works fine, when PR's coming from the origin repo.

When the pull requests comes from forked repo then the problem appear where cannot find the commit because is still in the fork.

Any idea how we can solve it?

Here is a example of the jenkinsfile:

pipeline {

  triggers {
    bitBucketTrigger credentialsId: 'GIT_CREDS',
      triggers: [
        [$class: 'BitBucketPPRPullRequestServerTriggerFilter',
          actionFilter: [$class: 'BitBucketPPRPullRequestServerCreatedActionFilter',
            allowedBranches: ''
          ]
        ],
        [$class: 'BitBucketPPRPullRequestServerTriggerFilter',
          actionFilter: [$class: 'BitBucketPPRPullRequestServerMergedActionFilter',
            allowedBranches: ''
          ]
        ],
        [$class: 'BitBucketPPRPullRequestServerTriggerFilter',
          actionFilter: [$class: 'BitBucketPPRPullRequestServerSourceUpdatedActionFilter',
            allowedBranches: ''
          ]
        ]
      ]
  }

  agent any

  stages {
    stage('Checkout Bitbucket repo') {
      steps {
        script {
          git branch: 'env.CHANGE_BRANCH',
            credentialsId: 'GIT_CREDS',
            url: env.GIT_URL
        }
      }
    }
    stage('Start the build') {
      steps {
        script {
            sh 'echo "BUILD"'
        }
      }
    }
  }
}

Here is the LOG:

stderr: fatal: ambiguous argument '0648334d4491907a45a840a7326c3b8f54180144^{commit}': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

Solution

  • I was able to fix this by adding Refspec in Advanced tab.

    The refspec is:

    Pipeline script from SCM