Jenkins Pipeline and stash Pull Request Builder not working on PR create/update

大兔子大兔子 提交于 2019-12-06 06:57:14

问题


Below is the requirement needed to achieve using the Jenkins Pipeline and i am new bee into Jenkins Pipeline.

  • After completing development work and pushing his changes to Bitbucket the user creates a pull request.

  • In order to approve a pull request we require at least one successful Jenkins build. Thereby we would like to get only the build result of the code checked in for the pull request.

  • When a pull request is created/updated Jenkins shall be triggered automatically for real continuous integration.

  • The build result shall be reported back to Bitbucket.

Used Stash Pull Request Builder and stash Notifier for the above process which is working for Normal Freestyle Project.

We need to migrate the similar functionality using Jenkins pipeline, So have created the jenkins job as below. The pipeline script to checks out the PR branch and trigger build is as below

node {
    stage('Checkout') {         
        checkout(
        [
            $class: 'GitSCM',
            extensions: [               
                [$class: 'CleanCheckout'],              
            ],
            branches: [
                [name: '']
            ], 
            userRemoteConfigs: 
            [[
                credentialsId: 'id', 
                url: 'repourl.git'
                refspec: ('+refs/pull-requests/*/from:refs/remotes/origin/pr/*/from'), 
                branch: ('origin/pr/${pullRequestId}/from')

            ]]
        ])
    }

    stage('Build') {    
        sh 'make'
    }
    stage('notify') {
      step([$class: 'StashNotifier'])
        try {
        // Do stuff
        currentBuild.result = 'SUCCESS'     
    } catch(err) {
        currentBuild.result = 'FAILED'
    }
   step([$class: 'StashNotifier'])
 }
}

Though i have done the above configuration when i create/update the PR, the build is not automatically triggered in jenkins. I guess the notification from stash to jenkins did not happen because we specify "origin/${pullRequestId}/from" in free style project. But i do not have that option to specify in pipeline job.

Tried few alternatives as below.

Instead of stash Pull Request Builder i tried with just "Poll SCM" project and specified cron job to trigger as "H/2 * * * *". Upon commits the job is triggered at jenkins. It means that for every commit the jenkins job gets triggered. But Jenkins should trigger the job when PR is created/updated.

Also based on below question, one answer says "In freestyle job add a trigger to start your pipeline job as build step" which i did not found.

I am missing something here certainly which could be basic and new to jenkins pipeline.

Any hint on achieving the desired behavior?

来源:https://stackoverflow.com/questions/45038463/jenkins-pipeline-and-stash-pull-request-builder-not-working-on-pr-create-update

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!