The plugin makes use of the Jenkins Credential store making it easier to manage the Perforce Server connection for multiple Jenkins jobs. Perforce Server credentials must be added to the Global or a user defined domain, using one of the two supported Perforce Credentials: 'Perforce Password Credential' or 'Perforce Ticket Credential'.
To add a Perforce Credential:
- Navigate to the Jenkins Credentials page (select 'Credentials' on the left hand side)
- Select 'Global credentials' (or add domain if needed)
- Select 'Add Credentials' from the left hand side
- Choose 'Perforce Password Credential' from the 'Kind' drop-down select
- Enter a Description e.g. local test server
- Enter the P4Port e.g. localhost:1666
- Enter a valid username and password
- Press the 'Test Connection' button (you should see Success)
- Click 'Save' to save.
The Perforce Ticket Credential supports using a ticket file (such as the default P4TICKETS file) or a ticket value (returned by the command p4 login -p). If Ticket authentication is used for remote builds the Ticket must be valid for the remote host (either login on the remote host or use p4 login -a).
All Perforce Credential types support SSL for use on Secured Perforce Servers; to use just check the SSL box and provide the Trust fingerprint.
Perforce workspaces are configured on the Jenkin Job configuration page and support the following behaviours:
- Static
The workspace specified must have been previously defined. The Perforce Jenkins user must either own the workspace or the spec should be unlocked allowing it to make edits. The workspace View remains static, but Jenkins will update other fields such as the workspace root and clobber option.
- Spec File
The workspace configuration is loaded from a depot file containing a Client workspace Spec (same output as p4 client -o and the Spec depot '.p4s' format). The name of the workspace must match the name of the Client workspace Spec.
- Manual
This allows the specified workspace to be created (if it does not exist) or update the spec by setting the various options. Jenkins will fill out the workspace root and may override the clobber option.
- Template & Stream
In this mode the workspace View is generated using the specified template workspace or stream. The name of the workspace is generated using the Workspace Name Format field and makes it an ideal choice for matrix builds.
Many of the workspace fields can include environment variables to help define their value. Take the 'Worksapce name' often I use:
jenkins-${NODE_NAME}-${JOB_NAME}
If the job is called 'foo' and built on a slave 'linux' it expands to:
jenkins-linux-foo
Jenkins provides a set of environment variable and you can also define your own. Here is a list of built in variables:
BUILD_NUMBER
- The current build number, such as "153"
BUILD_ID
- The current build id, such as "2005-08-22_23-59-59"
BUILD_DISPLAY_NAME
- The name of the current build, something like "#153".
JOB_NAME
- Name of the project of this build, such as "foo"
BUILD_TAG
- String of "jenkins-${JOB_NAME}-${BUILD_NUMBER}".
EXECUTOR_NUMBER
- The unique number that identifies the current executor.
NODE_NAME
- Name of the slave or "master".
NODE_LABELS
- Whitespace-separated list of labels that the node is assigned.
WORKSPACE
- Absolute path of the build as a workspace.
JENKINS_HOME
- Absolute path on the master node for Jenkins to store data.
JENKINS_URL
- URL of Jenkins, like http://server:port/jenkins/
BUILD_URL
- Full URL of this build, like http://server:port/jenkins/job/foo/15/
JOB_URL
- Full URL of this job, like http://server:port/jenkins/job/foo/
The p4 plugin allows the use of environemnt vaiables in fields like the Workspace view and Stream path. For example:
//depot/main/proj/... //jenkins-${NODE_NAME}-${JOB_NAME}/...
or with a Matrix build you might have defined your own variables like ${OS}
. Remember they can be used anywhere in the mapping:
//depot/main/${JOB_NAME}/bin.${OS}/... //jenkins-${NODE_NAME}-${JOB_NAME}-${OS}/bin/${OS}/...
Perforce will populate the workspace with the file revisions needed for the build, the way the workspace is populated is configured on the Jenkin Job configuration page and support the following behaviours:
- Automatic cleanup and sync
Perforce will revert any shelved or pending files from the workspace; this includes the removal of files that were added by the shelved or pending change. Depending on the two check options boxes Perforce will then clean up any extra files or restore any modified or missing files. Finally, Perforce will sync the required file revisions to the workspace populating the 'have' table.
- Forced clean and sync
Perfore will remove all files from under the workspace root, then force sync the required file revisions to the workspace. If the populating the 'have' table options is enabled then the 'have' list will be updated.
This method is not recommended as the cost of IO resources on server and client are high. Apart from exceptional circumstances the Automatic cleanup and sync option will produce the same result.
- Sync only
Perforce will not attempt to cleanup the workspace; the sync operation will update all files (as CLOBBER is set) to the required set of revisions. If the populating the 'have' table options is enabled then the 'have' list will be updated.
Building a Jenkins Job can be triggered using the SCM polling option, Build Now button or calling the build/ URL endpoint.
To enable SCM polling, check the 'Poll SCM' option and provide a Schedule using the Cron format. For example every 10 minutes Monday to Friday, the 'H' is a time offset (calculated using a Hash of the Job name).
To build immediately select the Build now button...
Or use the call the build/ URL endpoint e.g. http://jenkins_host:8080/job/myJobID/build
(where myJobID is the name of your job and jenkins_host the name or IP address of your Jenkins server).
A Jenkins job can build at any point in the codes history, identified by a Perforce change or label.
The Jenkins job can be pinned to a Perforce change or label by setting the Pin build at Perforce Label
field under the Populate options. Any time the Jenkins job is trigged, it will only build upto the pinned point.
Alternativly, a change or label can be passed using the Build Review
paramiters or URL end point (see the Build Review chapter for details)
Related issues: JENKINS-29296
The plugin supports parallel execution of Jenkins Jobs. Jenkins will create a new workspace directory workspace@2
and so on. The plugin will automatically template the current workspace appending .clone2
for the templates' name.
For custom workspaces, where an alternative location has been set e.g. Advanced --> Use custom workspace --> Provide a Directory. Then you will need to add the executor number to the end of your path.
For example:
/Users/pallen/Workspaces/custom@${EXECUTOR_NUMBER}
The plugin will then correctly template the workspaces as needed.
When polling is used, changes can be filtered to not trigger a build; the filters are configured on the Jenkin Job configuration page and support the following types:
- Exclude changes from user
Changes owned by the Perforce user specified in the filter will be excluded.
- Exclude changes from Depot path
Changes where all the file revision's path starting with the String specified in the filter will be excluded.
For example, with a Filter of "//depot/main/tests":
Case A (change will be filtered):
Files:
//depot/main/tests/index.xml
//depot/main/tests/001/test.xml
//depot/main/tests/002/test.xml
Case B (change will not be filtered, as build.xml is outside of the filter):
Files:
//depot/main/src/build.xml
//depot/main/tests/004/test.xml
//depot/main/tests/005/test.xml
The plugin supports a Build Review Action with a review/build/ URL endpoint. Parameters can be passed informing Jenkins of Perforce shelf to unshelve and changelist to sync to. There are also Pass/Fail callback URLs for use with Swarm.
An example URL that would build the review in the shelved change 23980:
http://jenkins_host:8080/job/myJobID/review/build?status=shelved&review=23980
The Build Review Action support the following parameters:
- status (shelved or submitted)
- review (the pending shelved change)
- change (the submitted change)
- label (a Perforce label, instead of change)
- pass (URL to call after a build succeeded)
- fail (URL to call after a build failed)
Please note these paramiter are stored in the Environment and can be used with variable expansion e.g. ${label}
; for this reason please avoid these names for slaves and matrix axis.
The Build Review Action can be invoked manually from within Jenkins by selecting the Build Review button on the left hand side. This provides a form to specify the parameters for build.
The plugin supports unshelving one or more shelved changes into your Jenkins workspace as a build step. Select the 'Perforce: Unshelve' from the 'Add build step' dropdown and provide a change-list number or custom environment variable ${VAR}
in the 'Unshelve Changelist` box.
There is an optional 'Resolve Option' select box to choose the type of resolve to use on the unshelved files. This might be needed if the shelved revisions are at a different revision to the files sync'ed in the workspace.
After a build Jenkins provides the ability to see the details of a build. Select the build of interest from the Build History on the left hand side. You then get a Perforce change summary for the build and clicking on the View Detail link for specific files.
Detailed view...
Jenkins can tag builds automatically as a Post Build Action or allow manual tagging of a build. The Tags are stored in Perforce as Automatic Labels with the label view based on the workspace at the time of tagging.
Tagging with Post Build Action
Manual Tagging
- Select the build that you wish to tag from the project page.
- Click on the 'Label This Build' link on the left hand panel, if the build has already been tagged the link will read 'Perforce Label'.
- Update the label name and description as required and click 'Label Build' to add the label to Perforce.
- Once the build is labeled you will see the label details appear in a table above. New labels can be added to the same build or labels can be updated by providing the same label name.
Jenkins can automatically shelve or submit build assets to Perforce. Select the 'Add post-build action' and select the 'Perforce: Publish assets' from the list. Select the Credentials and Workspace options, you can connect to a different Perforce server if required. Update the description if required, ${variables} are expanded.
Shelving with Post Build Action
Submitting with Post Build Action
Repository browsing allows Jenkins to use an external browser, like Swarm, P4Web, etc... to navigate files and changes associated with a Jenkins build.
To enable the feature select the Repository browser from the Job Configuration page and provide the full URL to the browser.
Link to change in Swarm
The plugin supports the Workflow and has DSL support for Perforce p4sync
, p4unshelve
, p4tag
and p4publish
.
To use the Workflow install the plugin(s) as needed. Create a new Workflow Job by selecting 'New Item' from the left hand menu, provide an 'Item name' and choose a 'Workflow' project.
The Workflow plugin requires a Groovy script. Currently the Groovy CPS DSL from SCM
is not supported. Select Groovy CPS DSL
and provide your script in the text box below.
You can use the snippet genertor to create each step and use the code to compose your workflow script.
For example, a basic sync script:
- Check the 'Snippet Generator' box and from the 'Sample Step' drop down choose
P4 Sync
. - Select a valid Perforce Credential
- Fill out ONLY ONE of the three boxes for the code source.
e.g.
Stream Codeline : //streams/st1-main
or
Template Workspace : template_ws
or
Depot path : //depot/projX
Press the 'Generate Groovy' button...
Use this snippet with your intended target e.g. a slave calles 'my_slave'
node('my_slave') {
p4sync credential: 'server_id', stream: '//stream/main'
}
For more examples and usage please refer to the Workflow docs.
There are several limitations as the Workflow plugin is relativly new, these include:
- SCM Polling (not functional)
- No access to Environment ${VAR} varables
Error: Setting up a SSL Credentials connection to Perforce.
Unable to connect: com.perforce.p4java.exception.ConnectionException:
Error occurred during the SSL handshake:
invalid SSL session
Solution: Due to current US export control restrictions for some countries, the standard JDK package only comes with 128 bit encryption level cyphers. In order to use P4Java to connect to an SSL-enabled Perforce server, those living in eligible countries may download the unlimited strength JCE (Java Cryptography Extension) package and replace the current default cryptography jar files on the build server with the unlimited strength files.
The libraries can be downloaded from:
http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html
Installation instructions can be found in the file 'README.txt' in the JCE download.
Error: Perforce login error part way through a build with an Edge/Commit setup.
Perforce password (P4PASSWD) invalid or unset.
- no such file(s).
ERROR: Unable to update workspace:
com.perforce.p4java.exception.AccessException: Perforce password (P4PASSWD) invalid or unset.
Solution: The following configurables must be set to allow the Edge to forward the login information to the Commit server.
p4 configure set cluster.id=myID
p4 configure set myEdge#rpl.forward.login=1
- One Jenkins Job per Swarm branch