Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Referencing Pull requests for CI server builds (TeamCity)

We would like to build all New/Updated Pull Requests by our CI server (TeamCity). I'm rummaging through search results both from Atlassian and JetBrains but do not seem to get to a straightforward solution.

 

1. References the use of polling but I can't seem to see any way of directly referencing pull requests in our VCS configuration to trigger automatic builds only on PRs. Otherwise webhooks that need to pass through a 3rd party solution (like Lambda) to translate the webhook call to the right API call towards teamcity.

https://community.atlassian.com/t5/Bitbucket-questions/Trigger-teamcity-build-on-pull-request/qaq-p/601076

 

2. A plugin that used to work on an old version :
https://teamcity-support.jetbrains.com/hc/en-us/community/posts/115000507590-BitBucket-Cloud-Pull-Request-Builds

 

3. An article that point blank says you cannot reference PRs, so you have to build all commits (Which I guess doesn't work with PRs coming from Forks)
https://medium.com/@hazems/lessons-learned-bitbucket-pr-build-integration-with-teamcity-b3fea5c786a8

 

 

Hopefully I'm missing something and there's an easy way of achieving this by directly referencing pull requests, otherwise it's either down to Webhooks + 3rd party like Lambda, or fixing that old plugin.

 

Thanks in advance for any feedback.

3 answers

So I ran into the same issue, I think I have something that works without using lambda in the current version of teamcity.

This is for bitbucket cloud, since bitbucket server has a build feature for bitbucket server pull requests https://www.jetbrains.com/help/teamcity/pull-requests.html

1. Bitbucket pipeline with pull-request keyword. This triggers on pull-request creation or update of pull-request.

bitbucket-pipeline excerpt:

pipelines:
pull-requests:
'**': #this runs as default for any branch not elsewhere defined
- step:
name: Trigger Teamcity Build
script:
- make set_build_branch
- make trigger_teamcity

2. The pipeline triggers the teamcity REST api with curl

Makefile excerpt:

set_build_branch:
curl --header "Content-Type: text/plain" \
--header "Authorization: Bearer $TEAMCITY_ACCESS_TOKEN" \
--header "Origin: [YOUR-URL-HERE]" \
--request PUT \
"[YOUR-URL-HERE]app/rest/projects/id:[YOUR_ROOT_PROJECT]/parameters/trigger_build_branch" \
--data "${BITBUCKET_BRANCH}"

trigger_teamcity:
curl --header "Accept: application/json" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${TEAMCITY_ACCESS_TOKEN}" \
-H "Origin: [YOUR-URL-HERE]" \
-X POST "[YOUR-URL-HERE]app/rest/vcs-root-instances/commitHookNotification?locator=vcsRoot:(id:[YOUR_VCS_ROOT_ID])"

The first command sets the configuration parameter  trigger_build_branch in the root project which all our other projects inherit parameters from.

The second command triggers a commit hook for the vcs root that the root project uses, which all our projects also use.

3. The VCS root has an active commit hook, but the interval for checking is set to basically never trigger (~ 1 year) so that the only time it is ever triggered is by the curl command

4. The commit hook event then triggers a 'VCS Trigger' with the following parameters

VCS trigger rules: 
[YOUR TRIGGER RULES]
Branch filter:
+:develop
+:master
+:%trigger_build_branch%

So that it only checks for changes in the  PR branch, develop and master. Although the develop and master filters aren't really necessary.

5. The Build Feature 'Commit Status Publisher' then reports the status of the build back to the bitbucket pull request.

Deric, thank you for this very helpful overview. I did much as you have described and it is working fairly well, except that the build doesn't always trigger, even though my pipeline shows that the commit hook was sent. I'd like for it to build with every commit to the PR, how do you have your VCS root and triggers configured to make this work?

The fix to Bitbucket Pull Requests Trigger plugin is published as pull request #69 to the main github repository. I downloaded https://github.com/karanagai/teamcity-plugins.git and built the plugin locally, it worked fine with TeamCity 2019.1. 

This is what I am looking for. How do I build the plugin to get the (.zip or other format) so I can upload and install in TeamCity?

If you have Git command line client and Maven installed on your computer, run the following three commands,

    git clone https://github.com/karanagai/teamcity-plugins.git

    cd teamcity-plugins

    mvn package

pullrequests.zip will be located at pullrequests/target directory. Upload and enable it to TeamCity.

Like ashmosaheb likes this

Thanks very much. That helped. I've built the plugin as per the instructions and uploaded the .zip file to TeamCity and all seems to be working so far!

I am working on this same problem right now, and this is my bitbucket pipeline. I send several of the BitBucket built in variables to to TeamCity for use in the build configuration.

 

image: atlassian/default-image:2

pipelines:
 branches:
  '{develop,qa}':
  - step:
     name: Pipeline for Develop and QA Builds
     script:
     - echo "Pipeline for Develop and QA builds"

 pull-requests:
  '**': #runs on all branches not elsewhere defined
 ##
 ## Send PR details to a TeamCity build configuration & posts commit hook 
 ##  TeamCity uses these parameters to decorate the PR in BitBucket
 ##  Values are also used when running SonarScanner s

 ##
 ## Required Variables 
 ##  TC_URL: full http url to TeamCity server
 ##  TC_TOKEN: REST API access token 
 ##  TC_VCS_ROOT: ID (not the name) of the VCS root
 ##  TC_BUILD_ID: ID (not the name) of the Build configuration
 ##
  - step:
     name: Set Parameters on TeamCity Build & send a commit notification
     script:
     ## build the header strings for CURL
     - "ORIG=\"Origin: $TC_URL\""                 
     - "AUTH=\"Authorization: Bearer $TC_TOKEN\""
     - "TEXT=\"Content-Type: text/plain\""        
     
     ## build the path to the TeamCity API to set parameters on the build
     - "SETP=\"--request PUT $TC_URL/app/rest/buildTypes/id:$TC_BUILD_ID/parameters\"" 
     
     ## push bitbucket pull-request variables to TeamCity build
     - "curl -H \"$ORIG\" -H \"$AUTH\" -H \"$TEXT\" $SETP/BITBUCKET_PR_ID                 --data $BITBUCKET_PR_ID"
     - "curl -H \"$ORIG\" -H \"$AUTH\" -H \"$TEXT\" $SETP/BITBUCKET_BRANCH                --data $BITBUCKET_BRANCH"
     - "curl -H \"$ORIG\" -H \"$AUTH\" -H \"$TEXT\" $SETP/BITBUCKET_PR_DESTINATION_BRANCH --data $BITBUCKET_PR_DESTINATION_BRANCH"
     - "curl -H \"$ORIG\" -H \"$AUTH\" -H \"$TEXT\" $SETP/BITBUCKET_COMMIT                --data $BITBUCKET_COMMIT"
     - "curl -H \"$ORIG\" -H \"$AUTH\" -H \"$TEXT\" $SETP/BITBUCKET_REPO_UUID             --data $BITBUCKET_REPO_UUID"
     - "curl -H \"$ORIG\" -H \"$AUTH\" -H \"$TEXT\" $SETP/BITBUCKET_REPO_OWNER_UUID       --data $BITBUCKET_REPO_OWNER_UUID"
     
     ## Build the path to the TeamCity api for commit Hook notifications
     - "HOOK=app/rest/vcs-root-instances/commitHookNotification" 
     
     ## Send a commit notification to the TeamCity VCS root. 
     ## the build must have an appropriate VCS trigger configured
     - "curl -H \"$ORIG\" -H \"$AUTH\" -X POST \"$TC_URL/$HOOK?locator=vcsRoot:(id:$TC_VCS_ROOT)\""

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Bitbucket

Calling any interview participants for Bitbucket Data Center

Hi everyone,  We are looking to learn more about development teams’ workflows and pain points, especially around DevOps, integrations, administration, scale, security, and the related challeng...

441 views 4 4
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you