Integrating TeamCity with Perforce
This article describes how to integrate TeamCity with Perforce Helix Core to:
Build sources of projects stored in a Helix Core repository.
Use Perforce streams as feature branches and build their sources independently of each other.
Pre-test and pre-build files in shelved changelists.
Apply automatic labels to sources.
Report build statuses to code reviews in Perforce Helix Swarm.
Prerequisites
TeamCity supports Perforce Helix Core servers/clients starting from Helix Core 2017.1
If you plan to use the agent-side checkout mode, you need to install a Perforce Helix Core client and add it to
PATH
on build agent machines as well.
Running Builds on Perforce Helix Core Sources
To be able to run builds on project sources stored in Perforce Helix Core and to use all the features described in this article, you need to perform two procedures:
Create a dedicated project in TeamCity:
Go to Administration | Projects and click Create project.
Note that this will add the project right under the Root project. Alternatively, you can add it under any other existing project.
Enter the Name and ID of the project.
Click Create.
Create a Perforce VCS root:
In the Project Settings, go to VCS Roots.
Click Create VCS root.
Choose Perforce Helix Core as a VCS type.
Configure the root's settings as described in this article.
After the project and Perforce root are configured, you can proceed with adding build configurations and running builds.
Running Builds on Perforce Streams
TeamCity can monitor commits in Perforce streams and work with them as with regular feature branches.
If a Perforce root is configured to use the Stream mode, you can enable the feature branches support in the root settings. After it is enabled, all streams which have the specified main stream as a parent will be included into the set of feature branches processed by TeamCity. To include only specific streams to this set, edit the branch specification to filter these streams. Each filter rule should start with a new line. The syntax is +|-:stream_name
. For example, use +://stream-depot/*
to monitor only streams located in the stream-depot
depot, where *
(for example, master
) is a logical branch name. Note that streams used in the branch specification should be descendants of the main stream.
TeamCity can process task streams as well, but it only detects new task streams if there is a non-merge commit made to them.
Running Builds on Streams from IntelliJ IDE
You can launch a remote build run from IntelliJ-platform IDEs, but only if a stream has been already detected by TeamCity. The TeamCity Remote Run plugin tries to deduce the correct stream according to the depot paths of the files in the IDE's working copy.
For instance, if a file path in the working copy starts with //depot/stream1/some/path
, TeamCity will try finding the //depot/stream1
stream and starting the remote run there. If you have modified a file from another stream (imported into the working copy) and want to enforce a build in a particular stream, you need to specify a configuration parameter teamcity.build.branch
when triggering the remote run.
Cleaning Stream Workspaces
To properly process task streams, TeamCity needs to create dedicated workspaces on the Perforce server. To save the server resources, you can clean inactive workspaces created by TeamCity directly from the TeamCity UI.
Running Personal Builds from IntelliJ IDE
If you write code in an IntelliJ-based IDE, you can pretest and prebuild local changes before committing them to a main Perforce repository: see common instructions on remote run, remote debug, and pre-test delayed commits.
The remote run/debug functionality is common for all types of VCS, but there are extra conveniences specific to Perforce:
Running Builds on Perforce Shelved Files
TeamCity allows you to run personal builds on Perforce shelved files. This way, you can try building changed source files before checking them into a common depot.
To manually run a custom build on Perforce shelved files:
Open the custom run dialog by clicking the context menu next to the Run button.
Enable run as a personal build option.
Enter the ID of the changelist that contains the shelved files.
Choose the target Perforce root.
To configure automatic triggering for a Perforce shelved changelist:
Go to Build Configuration Settings | Triggers.
Add a new trigger of the Perforce Shelve Trigger type.
Configure its settings as described in this article.
To build the target shelved changelist with TeamCity REST API, send a POST
request with the following body to the /app/rest/buildQueue
endpoint:
- swarmUpdateUrl
A URL for the Perforce Helix Swarm test updates. This property is optional and used only by builds triggered by Helix Swarm.
- shelvedChangelist
This property should point to a shelved changelist number and a valid VCS root external ID. The value should be in the
vcsRoot.<external_id>.shelvedChangeList
format.
In addition to this endpoint, you can also use a dedicated runBuildForShelve
endpoint that was available prior to version 2024.12:
- BUILD_TYPE_ID
The ID of your build configuration.
- VCS_ROOT_ID
The external ID of a related VCS Root.
- SHELVED_CHANGELIST_ID
The ID of the required changelist.
Publishing Build Statuses to Perforce Helix Swarm
If you use Perforce Helix Swarm for code review of shelved files, you can configure TeamCity to posts build statuses as comments to your reviews.
See this help article for more information: Integration with Perforce Helix Swarm.
TeamCity Workspaces in Perforce
To perform Perforce-related operations, a TeamCity server usually executes Perforce commands without the workspace context. For instance, workspaces are not required for tracking changes and for most server-side operations. However, certain cases require creating a dedicated workspace:
By default, TeamCity uses agent-side checkout mode for checking out sources of a build. In this case, it creates a dedicated Perforce workspace and executes a corresponding
p4 sync
command to get the sources.Using a Perforce VCS root to store versioned project settings.
Using Perforce streams as feature branches. In this case, TeamCity creates workspaces on the Perforce server to correctly process task streams.
Learn how TeamCity creates workspaces in Perforce and works with them in this article.
Configuring Post-Commit Hooks on Perforce Server
By default, TeamCity uses a polling approach to detect changes in a VCS repository. It periodically sends requests to the Perforce server to detect new revisions. For large installations with hundreds of VCS roots, this may create a noticeable load on both Perforce server and TeamCity. To avoid background polling, you can set up a post-commit hook on your Perforce server. This hook will notify TeamCity to start checking for changes only when they are available.
TeamCity provides a dedicated hook script that should be saved on your Perforce server. You can find the detailed instruction here.
Label Perforce Sources
TeamCity can assign custom labels to Perforce project sources. The list of applied labels and their status is displayed on the Changes tab of Build Results.
To configure automatic labeling for a build configuration:
Go to Build Configuration Settings | Build Features.
Add a new feature of the VCS labeling type.
Choose a Perforce VCS root to label.
Specify the labeling pattern as described here.
View Perforce Jobs
If a build contains a changelist that is associated with one or more jobs, TeamCity will show a wrench icon next to this change in the build results. Click or hover it to view the details of the relevant jobs.
Perforce Service Messages
Personal builds working with Perfoce projects may fail during the final "Undo personal changes" stage. This usually occurs if the code source checkout directory becomes unavailable before this stage begins.
In such cases, you must undo personal changes earlier than TeamCity normally does — while the build steps are still running. To do so, send the following service message during the final build step: