GIT SCM configuration for bitbucket pull requests checkout using generic Webhook parameters

拈花ヽ惹草 提交于 2019-12-20 07:46:43

问题


This stackoverflow page can help you checkout bitbucket pull requests in Jenkins with Generic webhook trigger.

Pre-requisites:

Jenkins: 1) Installing Generic Webhook Trigger Plugin and Bitbucket plugin in Jenkins

2) Configuring 'Bitbucket Endpoints' in Jenkins configuration.

3) Creating sample 'Pipeline' job in Jenkins (can configure Generic webhook plugin / configure bitbucket repo / include custom Jenkins file path)

Bitbucket: 4) Configuring Webhook in bitbucket repo to connect to Jenkins webhook plugin and checking 'Events' - Push, Comment Added.

Now assuming you can trigger Jenkins job from your bitbucket PR's by commenting.

I had a problem cloning the bitbucket pull-requests after triggering. It has to do with the Git SCM configuration.

Below is the way I resolved this issue.

1) In your sample pipeline job, check 'Print post content' under Generic Webhook plugin. You can see json content in the job console output.

2) Create a variable 'BRANCH' under 'Post content parameters' with expression value:

$.pullRequest.fromRef.displayId 

(can be derived from post content in console output) and check 'JSON path'.

Below is the pipeline Git SCM configuration:

Name: origin
RefSpec: +refs/heads/${BRANCH}
Branches to build
  Branch Specifier (blank for 'any'): **/pull-requests/**

I was successful in resolving this cloning PR's issue after configuring in the above way.

I tried using the bitbucket pr id but worked only in 'Pipeline Script' when I tried to checkout the PR as below:

checkout([$class: 'GitSCM',
                        branches: [[name: 'FETCH_HEAD']],
                        extensions: [[$class: 'LocalBranch']],
                        userRemoteConfigs: [[refspec: "+refs/pull-requests/${PR_ID}/from:pr/${PR_ID}", credentialsId: '*****', url: 'https://stash***************.git']]
                        ])

where PR_ID has expression value

$.pullRequest.id

回答1:


Also you can try GIT SCM configuration using:

Name: FETCH_HEAD`
RefSpec: +refs/heads/*:refs/remotes/origin/*
Branch Specifier: **/${BRANCH}
Additional Behaviours:
1. Wipe out repository & force clone
2. Clean after checkout 

where BRANCH value is '$.pullRequest.fromRef.displayId' retrieved from Generic Webhook trigger post parameters.



来源:https://stackoverflow.com/questions/56657607/git-scm-configuration-for-bitbucket-pull-requests-checkout-using-generic-webhook

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