Helix Plugin for Jenkins Guide (1.11.x)

p4: P4Groovy

P4Groovy is a Groovy interface to P4Java that allows you to write Perforce commands in the pipeline DSL (Domain Specific Language). For more information about using P4Groovy manually, see P4Groovy.

Example p4: P4Groovy configuration

Image of an Example of p4: P4Groovy Manual Workspace

Example script generated for p4:P4Groovy

The form above generates the following script snippet:

p4( 
    credential: 'publicperforcecom1666',
    workspace: manualSpec(
        charset: 'none', 
        name: 'jenkins-${NODE_NAME}-${JOB_NAME}-${EXECUTOR_NUMBER}',
        pinHost: false,
        spec: clientSpec(
            allwrite: false,
            backup: true,
            changeView: '',
            clobber: true,
            compress: false,
            line: 'LOCAL',
            locked: false,
            modtime: false,
            rmdir: false,
            serverID: '',
            streamName: '//streams/projectAlpha',
            type: 'WRITABLE',
            view: ''
        )
    )
)
Tip

If you manually edit your script, remember to keep the single quotes that the snippet generator puts around the variables. Replacing them with full quotes " will expand the variables early and the script will fail. For more information about this, see Variable Expansion.

Reduce the script snippet size to make it more readable

You can reduce the script size and make it easier to read by removing any options that are set to the default value.

Example script snippet with default values removed:

p4( 
    credential: 'publicperforcecom1666',
    workspace: manualSpec(
        name: 'jenkins-${NODE_NAME}-${JOB_NAME}-${EXECUTOR_NUMBER}',
        spec: clientSpec(streamName: '//streams/projectAlpha'
        )
    )
)

Paste the script snippet into a basic script

Example: the script snippet above has been pasted into a basic script:

pipeline {
    agent any 
    stages {
        stage('Checkout') {
			steps() {
				// checkout files from //streams/projectAlpha...
				p4( 
					credential: 'publicperforcecom1666',
					workspace: manualSpec(
						name: 'jenkins-${NODE_NAME}-${JOB_NAME}-${EXECUTOR_NUMBER}',
						spec: clientSpec(streamName: '//streams/projectAlpha'
						)
					)
				)
			}
        }
    }
}		

Configure a P4Groovy script

Use p4: P4Groovy to create a P4Groovy script using the Snippet Generator.

  1. To create a P4Groovy script, select p4: P4Groovy from the Sample Step dropdown
  2. Select the credentials for the project from the Perforce credentials dropdown.

Workspace behavior

Important
  • Use a unique workspace name for the Jenkinsfile that will not get reused by the sync steps in the script. A postfix of -script will help to identify the workspace's use and make it unique from code sync steps.
  • If you need more than one sync task in a script you MUST use a different workspace name. For instructions on how to do this, see Multiple Syncs.
  • Only map the Jenkinsfile (and perhaps Pipeline libraries) in the workspace view. Jenkins may create an @script directory on the master and you don't want to unnecessarily sync code to an area not used for the actual build.
Note

Many of the workspace fields can include environment variables to help define their value. We recommend that variables are used in the Workspace name to enable you to identify builds. For more information about environment variables, see Variable Expansion.

Perforce workspaces support the following behaviors:

  • Manual (custom view): Manually define the workspace view, and sync options. Creates the workspace if it does not already exist or updates the workspace spec by setting the various options if it already exists. Jenkins will fill out the workspace root and may override the clobber option. For details about manually configuring your workspace, see Manual workspace.
  • Static (static view, master only): Uses a predefined workspace, the workspace must have a valid view and must already exist. The plugin user must either own the workspace or the spec must be unlocked allowing the plugin user to make edits. The workspace view remains static, but Jenkins will update other fields such as the workspace root, and clobber option. For details about using a static predefined workspace, see Static workspace.
  • Template (view generated for each node): Uses a specified template to generate the workspace view. The name of the workspace is generated using the Workspace Name Format field. This makes it an ideal choice for Matrix builds. For details about using a template to generate the workspace, see Template workspace.
  • Streams (view generated by Perforce for each node): Uses a specified stream to generate the workspace view. The name of the workspace is generated using the Workspace Name Format field. This makes it an ideal choice for Matrix builds. For details about using a stream to generate the workspace, see Streams workspace.
  • Spec File (load workspace spec from file in Perforce): Loads the workspace configuration from a depot file containing a client workspace spec. This is the 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. For details about using a spec file for your workspace, see Spec File workspace.

Generate Pipeline Script

To generate the pipeline script, click Generate Pipeline Script. The script is generated in the text box below the button.

Copy the generated script and paste it into the Script box on the Jenkins Pipeline Script page.

Note

To allow P4Groovy code to be executed clear the Use Groovy Sandbox checkbox on the Jenkins Pipeline Script page.