Link
GitHub Get Started

Connecting Perforce to Jenkins

This guide shows you how to set up automatic build triggering when code is committed to Perforce, creating a seamless integration between your version control and CI/CD pipeline.

Overview

When properly configured, this integration will:

  • Automatically trigger Jenkins builds when code is committed to Perforce
  • Pass changelist information to Jenkins for build tracking
  • Work with ButterStack’s existing Perforce integration for complete visibility
  • Support multiple Jenkins jobs and depot paths

Prerequisites

  • Perforce server with admin access
  • Jenkins server with P4 plugin installed
  • Network connectivity between Perforce and Jenkins
  • (Optional) ButterStack with Perforce integration set up

Step 1: Configure Jenkins with Perforce Plugin

Install the P4 Plugin

  1. In Jenkins, go to Manage JenkinsManage Plugins
  2. Search for “P4 Plugin” and install it
  3. Restart Jenkins if required

Set Up Perforce Connection

  1. Go to Manage JenkinsConfigure System
  2. Find the Perforce section
  3. Click Add Perforce Server
  4. Configure your server:
    • Server: your-perforce-server:1666
    • Username: Your Perforce username
    • Password: Your Perforce password or ticket
    • Workspace: Create or select a workspace

Test the Connection

  1. Click Test Connection to verify Jenkins can connect to Perforce
  2. Save the configuration

Step 2: Create a Jenkins Job

Create a New Job

  1. Click New Item in Jenkins
  2. Enter a name (e.g., “ButterStack-Build”)
  3. Select Freestyle project
  4. Click OK

Configure Source Code Management

  1. In the job configuration, go to Source Code Management
  2. Select Perforce
  3. Choose your Perforce server from the dropdown
  4. Configure the workspace:
    • Workspace: Use the same workspace configured in Step 1
    • View: Set your depot path (e.g., //depot/... //workspace/...)

Configure Build Triggers

  1. Go to Build Triggers
  2. Check “Trigger builds remotely”
  3. Set an Authentication Token (optional but recommended)
  4. Note: The P4 plugin will automatically handle changelist-based triggers

Add Build Steps

Add your build steps (compile, test, package, etc.) based on your project needs.

Step 3: Set Up Perforce Triggers

Option A: Using ButterStack Docker Environment

If you’re using ButterStack’s Docker development environment, the trigger is already configured. The Jenkins trigger script is located at:

/Users/ryan/dev/butter_stack/docker/perforce/jenkins-trigger.sh

Option B: Manual Perforce Trigger Setup

1. Create the Trigger Script

Create a script on your Perforce server (e.g., /p4/common/bin/triggers/jenkins.sh):

#!/bin/bash

# Jenkins trigger script for Perforce
CHANGELIST=$1
JENKINS_URL="${JENKINS_URL:-http://your-jenkins-server:8080}"

# Trigger Jenkins build
curl -s -X POST \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "change=${CHANGELIST}" \
  "${JENKINS_URL}/p4/change"

echo "Triggered Jenkins build for changelist ${CHANGELIST}"
exit 0

2. Make the Script Executable

chmod +x /p4/common/bin/triggers/jenkins.sh

3. Configure the Trigger

  1. Edit Perforce triggers:
    p4 triggers
    
  2. Add the Jenkins trigger:
    Triggers:
        jenkins-build change-commit //depot/... "/p4/common/bin/triggers/jenkins.sh %changelist%"
    
  3. Save and exit

Step 4: Environment Configuration

Set Environment Variables

On your Perforce server, set these environment variables:

# Required: Jenkins server URL
export JENKINS_URL="http://your-jenkins-server:8080"

# Optional: Specific job name (defaults to auto-detection)
export JENKINS_JOB="ButterStack-Build"

Network Configuration

Ensure your Perforce server can reach Jenkins:

# Test connectivity
curl -I http://your-jenkins-server:8080/p4/change

Step 5: Test the Integration

Manual Test

  1. Make a change to your Perforce depot
  2. Submit the changelist
  3. Check Jenkins for a new build trigger
  4. Verify the build includes the correct changelist number

Verify Logs

Check Perforce logs for trigger execution:

tail -f $P4ROOT/logs/log

Check Jenkins logs for incoming triggers:

tail -f $JENKINS_HOME/logs/jenkins.log

Advanced Configuration

Multiple Depot Paths

Configure different triggers for different depot paths:

Triggers:
    jenkins-main change-commit //depot/main/... "/p4/common/bin/triggers/jenkins-main.sh %changelist%"
    jenkins-dev change-commit //depot/dev/... "/p4/common/bin/triggers/jenkins-dev.sh %changelist%"

Conditional Builds

Modify the trigger script to check commit messages or file paths:

# Only trigger for specific file types
if p4 describe -s $CHANGELIST | grep -q "\.cpp\|\.h\|\.cs"; then
    # Trigger build
    curl -s -X POST -d "change=${CHANGELIST}" "${JENKINS_URL}/p4/change"
fi

Authentication

If Jenkins requires authentication, modify the trigger script:

# With authentication token
curl -s -X POST \
  -H "Authorization: Bearer ${JENKINS_TOKEN}" \
  -d "change=${CHANGELIST}" \
  "${JENKINS_URL}/p4/change"

Integration with ButterStack

When using ButterStack alongside this integration:

  1. ButterStack webhook tracks changes and creates Change records
  2. Jenkins trigger starts builds automatically
  3. Jenkins webhook (configured in ButterStack) reports build status back
  4. Complete visibility of changes → builds → results in ButterStack

This creates a complete CI/CD pipeline with full tracking and visibility.

Troubleshooting

Common Issues

Jenkins builds not triggering:

  • Check Perforce trigger logs: p4 triggers -o
  • Verify network connectivity: curl -I $JENKINS_URL/p4/change
  • Check Jenkins P4 plugin configuration

Wrong changelist in builds:

  • Verify the %changelist% parameter is passed correctly
  • Check Jenkins job workspace configuration

Permission errors:

  • Ensure the trigger script has execute permissions
  • Verify Perforce service user has necessary permissions

Debug Mode

Enable debug logging in the trigger script:

# Add to beginning of trigger script
set -x  # Enable debug mode
exec 2>>/tmp/jenkins-trigger.log  # Log to file

Security Considerations

  • Use service accounts with minimal required permissions
  • Consider using authentication tokens for Jenkins API calls
  • Restrict trigger execution to specific depot paths
  • Monitor trigger logs for unusual activity

Performance Tips

  • Use lightweight trigger scripts to avoid blocking submits
  • Consider asynchronous build triggering for large changesets
  • Monitor Jenkins queue capacity
  • Use workspace caching to speed up builds

This integration provides a robust, automated CI/CD pipeline that scales with your development workflow while maintaining complete visibility through ButterStack.

Copyright © 2026 ButterStack. All rights reserved.

Esc
Type to search the documentation