0

I am trying to configure my teamcity pipeline such that builds are only triggered when a PR for a feature/* branch is targeting the master branch, but I am unable to make it work.

In my VCS Root I've configured my default branch => refs/heads/master then default branch specification is +:refs/pull/*/merge I've also tried

    +:refs/pull/*/merge 
    +:refs/heads/feature/*

VCS Root Branch specification:

then I've configured a VCS Trigger with rule to 'Trigger Build' for all files '**' using the attached VCS Root for this build pipeline. the Branch filder is

   +:feature/*
   +:pull/*/merge

also tried

   +:refs/heads/feature/*
   +:ref/pull/*/merge

VCS Trigger configuration

Teamcity version is "2023.05.6"

What I want to mention is that the pipeline does not trigger, using this configuration. However if I in the VCS Root configure Branch Specification to refs/heads/* it triggers, but it triggers not only on PR , but every time I push a new branch , or changes to existing branch.

1

2 Answers 2

1

The branch pattern for PRs depends on your actual VCS (i.e. Bitbucket, GitHub, etc.) and even on its version (e.g. there was a breaking change in Bitbucket behavior regarding PRs recently). With the VCS root settings in TeamCity you just set what branch pattern you want your builds to be triggered for. You have to check your VCS documentation to set up the VCS root(s) accordingly. Also, have a look at Pull Requests build feature in TeamCity.

Sign up to request clarification or add additional context in comments.

Comments

0

First, you need to upgrade to a newer version of TeamCity, there are a lot of security vulnerabilities that have been patched since the version you list, and secondly, each source code repository handles PRs differently, so you need a relatively recent build that matches how your repo works.

Use the Pull Requests build feature and specify which branches are targets of your PRs. If you only want to pull PRs that target master, the target branch specification would be:

+:refs/heads/master

(or however your repo organizes it)

If you can't use the Pull Requests Build Feature for some reason, another alternative is to trigger a build using the REST API of TeamCity and a "commit hook". When you make the API call, you will have to provide all parameters that your build needs to run, like the branch name. We used to do our PR builds this way before the Pull Requests build feature supported Bitbucket, and I have included our yaml pipeline for reference. Every variable that starts "BITBUCKET_" is a TeamCity build parameter used by our build configuration. The pipeline sets these parameters and then publishes the commit hook to trigger the build. The only parameter you'd have to specify is the branch you want to build; we included others for our analysis tools.

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, no commit hook"

 pull-requests:
  '**': #runs on all branches not elsewhere defined
 ##
 ## Send PR details to a TeamCity build configuration & posts a commit hook notification
 ##  TeamCity uses these parameters to decorate the PR in BitBucket with build status (see "Commit status publisher")
 ##
 ## Required Variables 
 ##  TC_URL: full http url to TeamCity server
 ##  TC_TOKEN: REST API access token (generated through the TeamCity administrator page)
 ##  TC_VCS_ROOT: ID (not the name) of the VCS root to post a commit notification to
 ##  TC_BUILD_ID: ID (not the name) of the Build configuration to pass parameters to
 ##
  - step:
     name: Set Parameters on TeamCity Build & send a commit notification to the VCS
     script:
     ## build the header strings for CURL
     - "ORIG=\"Origin: $TC_URL\""                 # Set origin to TeamCity url 
     - "AUTH=\"Authorization: Bearer $TC_TOKEN\"" # TeamCity auth token to invoke the REST api
     - "TEXT=\"Content-Type: text/plain\""        # Plaintext requests used for setting build parameters
     
     ## 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. for the notification to
     ## trigger a build, 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)\""

If you opt for this route, be sure to very carefully configure the permissions on the tokens you create.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.