Settings up P4 trigger to start Jenkins job

Posted on Jun 26, 2023

Trigger script

First we need a script that will be actually executed upon receiving an event from helix core server. Here is a template bash script that works just fine out of the box:

#!/bin/bash
CHANGE=$1

P4PORT=<insert_your_p4_ip_address>
JUSER=<username> // Note - this is not P4 username, it's a user used to authenticate in Jenkins!
JPASS=<password> // Note - same as above, password to the jenkin's user, not P4
JSERVER=<jenkins_url> // e.g. http://jenkins.local:8080

curl --header 'Content-Type: application/json' \
     --request POST \
     --silent \
     --user $JUSER:$JPASS \
     --data payload="{change:$CHANGE,p4port:\"$P4PORT\"}" \
     $JSERVER/p4/change

Note that the bash script needs to have Linux line endings, otherwise it will fail to execute properly.

Submitting trigger script

Although it is not required to store trigger scripts on a depot I found this setup to be the most robust one as it allow other people to modify it without logging into the server that is a host to the P4 server.

I recommend creating standalone Triggers depot that doesn’t have to be stream depot, regular one is just fine for that.

Once created simply submit the trigger script, let’s say with a jenkins_trigger.sh name.

P4 Trigger

For this trigger we will use change-commit event as this is event is broadcasted when a new change is submitted and files are available on the server already.

To change triggers setup you need to be logged as P4 user with a super access. Then in the command line type p4 triggers. You should now see a text editor. Here is what you need to add below Triggers: label:

	jenkins change-commit //foo/Main/... "%//Triggers/jenkins_trigger.sh% %change%"

In the above script:

  • jenkins – this is a name of the trigger, can be arbitrary value
  • change-commit – this is the name of an event that this trigger listens to
  • //foo/Main/... – this is the depot we listen for changes to occur. If something changes in //bar/Main/... this trigger will not be executed
  • "%//Triggers/jenkins_trigger.sh% %change%" - please note that the depot path to the trigger script is wrapper with the % signs. The %change% parameter is there for compatibility reasons

Once you put the above line to the text file you can save it and close. If something is wrong you should be able to see the error message in the command line. Otherwise it should just notify you that triggers have been updated and you are good to go.