Perforce - BredaUniversityGames/JenkinsLib GitHub Wiki

perforce.sync()

Checks out your project from a Perforce/Helix Core depot. Supports workspace templates and direct depot mappings, with incremental syncs for fast builds.

Usage

stages {
    perforce.sync()
    // ...
}

With overrides:

stages {
    perforce.sync(
        P4_HOST: 'ssl:perforce.buas.nl:1666',
        P4_FORCE_CLEAN: true
    )
    // ...
}

Prerequisites

Parameters

Parameter Default Description
P4_CREDENTIAL (empty) Required. Jenkins credential ID for Perforce
P4_HOST ssl:perforce.buas.nl:1666 Perforce server host
P4_WORKSPACE jenkins-<JOB_NAME> Perforce workspace name (used as template if P4_VIEW is empty)
P4_VIEW (empty) Workspace view mapping — if set, overrides P4_WORKSPACE template (see Sync Modes)
P4_FORCE_CLEAN false Force a clean sync (re-downloads all files)

How It Works

  1. Connects to the Perforce server using the provided credential
  2. Creates or reuses a Jenkins workspace based on the template or depot mapping
  3. Performs an incremental sync (only changed files) by default
  4. Sets ctx.vcsType, ctx.revision, and ctx.changelist for downstream stages
  5. On cleanup, reverts any pending changes in the workspace

Sync Modes

  • Workspace template (default): Uses the existing Perforce workspace named in P4_WORKSPACE as a template. Simplest option — create the workspace once in P4V and the pipeline reuses its view mapping.

  • View mapping (P4_VIEW set): Defines the workspace view directly in the parameter. No existing workspace template needed. Format:

    //depot/MyProject/... //${P4_WORKSPACE}/...
    

    Use ${P4_WORKSPACE} as the client name in the right side of the mapping.

Force Clean

When P4_FORCE_CLEAN is enabled, the sync discards all local files and re-downloads everything. Use this when:

  • You suspect corrupted local files
  • Build artifacts are interfering with the sync
  • You want a guaranteed clean state

Workspace Setup

You have two options for configuring how the pipeline syncs your Perforce depot:

Option 1: Workspace Template (recommended)

Create a Perforce workspace to use as a template. The pipeline will clone its view mapping.

In P4V:

  1. Right-click an existing template workspace (e.g., jenkins-TemplatePipeline)

  2. Select Create/Update Workspace from...

  3. Name it to match P4_WORKSPACE (defaults to jenkins-<JOB_NAME>, e.g., jenkins-MyProject)

  4. Set Root to C:\Jenkins\<JOB_NAME>

  5. Set View to your project's depot path:

    //depot/MyProject/... //jenkins-MyProject/...
    

Via CLI:

p4 client -t jenkins-TemplatePipeline jenkins-MyProject
p4 client jenkins-MyProject  # edit root and view

Option 2: View Mapping

Set P4_VIEW directly in the pipeline parameters — no workspace template needed:

//depot/MyProject/... //${P4_WORKSPACE}/...

Use ${P4_WORKSPACE} as the client name on the right side. The pipeline creates the workspace automatically.

Tip: Only map the folder containing your .uproject file. Mapping the entire depot wastes sync time and disk space.

Direct-Use Methods

These methods are available on the perforce object for advanced usage outside the standard pipeline:

Method Description
perforce.createTicket(config) Create a Perforce login ticket
perforce.getLatestChangelist(config) Get the latest submitted changelist number
perforce.unshelve(config) Unshelve a changelist
perforce.getChangelistDescription(config) Get a changelist's description
perforce.publish(config) Submit pending changes

Credential Setup

The P4 Plugin supports Password and Ticket credential types.

Password Credential

Use when your Perforce server accepts standard password authentication.

  • Kind: Perforce Password Credential
  • P4Port: ssl:perforce.buas.nl:1666
  • Username: the Perforce username
  • Password: the Perforce password
  • ID: memorable ID (e.g., p4-myproject)

Ticket Credential

Use when your Perforce server uses SAML/SSO authentication (as at BUAS).

A Perforce ticket is a session token generated by p4 login. Since BUAS uses SAML authentication, login happens through the browser.

To obtain a ticket:

  1. Open a terminal on the Jenkins agent (or any machine with the p4 CLI)

  2. Set the server address and username:

    p4 set P4PORT=ssl:perforce.buas.nl:1666
    p4 set P4USER=your-username
  3. Check for an existing ticket:

    p4 tickets

    If the output includes a hex string for your user, copy it and skip to step 6.

  4. Log in via SAML:

    p4 login -p

    This opens your browser to the SAML identity provider. Complete authentication. The terminal prints a ticket value (hex string like A1B2C3D4E5F6...).

    If the browser doesn't open automatically, the terminal will display a URL to copy/paste.

  5. For a long-lived ticket (recommended for CI):

    p4 login -p -a

    The -a flag creates a ticket valid from any IP. The Perforce admin must have enabled long-lived tickets for the account.

  6. Copy the hex string.

Create the credential in Jenkins:

  • Kind: Perforce Ticket Credential
  • P4Port: perforce.buas.nl:1666 (without ssl: prefix)
  • SSL connection: check this box
  • Username: the Perforce username
  • Login with ticket value: paste the hex string
  • ID: memorable ID (e.g., p4-myproject)

Click Test Connection to verify, then Create.

Tip: Verify a ticket is still valid with p4 login -s. If expired, repeat p4 login -p.

Trusting the SSL Fingerprint

If Test Connection fails with "Trust not established":

  1. Open a terminal on the Jenkins agent

  2. Run:

    p4 -p ssl:perforce.buas.nl:1666 trust
  3. Type yes to accept the fingerprint

  4. Verify: p4 trust -l

Important: Run p4 trust as the same OS user that the Jenkins agent runs as. If Jenkins runs as a different user, copy the .p4trust file to that user's home directory.

Troubleshooting

Issue Solution
"Trust not established" Run p4 trust on the agent. See Server Setup - SSL Fingerprint.
"Client 'xxx' unknown" Workspace name doesn't match. Check P4_WORKSPACE against P4V.
Sync takes too long Narrow your view mapping. Only map folders needed for building.
"File(s) up-to-date" Normal — nothing changed since last sync.
Ticket expired Re-run p4 login -p and update the credential in Jenkins.
⚠️ **GitHub.com Fallback** ⚠️