This is a script that posts a message to Discord every time a new changelist is submitted from the Perforce version control system.
- This was only tested with p4d running on a linux system (Ubuntu 18.04)
- The Perforce user needs access read access to the depot, so it can access the
p4 describe
command (more info below) - You need a Discord Webhook set up
- You need access to edit the
p4 triggers
on the server
Perforce triggers run as the same linux user as the Perforce server is running. It's usually the perforce
user, but you can double check by running ps aux | grep p4d
So for the script to work, the perforce
user needs to be able to run p4 describe
successfully. To do that, the steps are as following:
- Log in as the
perforce
user on the terminal by runningsudo su perforce
(followed bybash
if needed). - Set your Perforce login by running
p4 set P4PORT=ssl:localhost:1666
p4 set P4USER=your.username
p4 trust
p4 login
Followed by your password.
- It's easier if the user set up in this step has no session timeout, so you don't have to log in over and over. You can change that in the p4 admin app. (See suggestion below)
- If your server doesn't support SSL, make sure to remove
ssl:
from theP4PORT
. Ex.:p4 set P4PORT=localhost:1666
If you're using p4dctl to manage your servers, the user set in the p4dctl config must be the same as set on item 1.2 (in the p4 set P4USER=your.username
command)
To check that, either check the /etc/perforce/p4dctl.conf
file or the /etc/perforce/p4dctl.conf.d/
directory containing your server configuration file. More info here.
That's the easy part.
- Clone the repo, or download the script and save it somewhere where the perforce user (or whatever user your server is running) has access to.
- Make sure the script is executable by running
chmod u+x perforce_discord_webhook.sh
At this point, you should already be able to run the script manually with ./perforce_discord_webhook.sh <changelist number> <discord webhook link>
Perforce has a Triggers system, where you can configure the server to do actions based on triggers. We are gonna create a trigger that runs this script every time a cahngelist is submitted successfully
- on a terminal run
p4 triggers
. This will open the server triggers editor. - Add a new trigger with these settings:
Triggers:
discord change-commit //depot/... "/bin/bash <perforce_discord_webhook.sh location> %changelist% <discord webhook link>
And replace the location and the webhook links according to your setup. Example:
discord change-commit //depot/... "/bin/bash /home/perforce/perforce_discord_webhook.sh %changelist% https://discordapp.com/api/webhooks/<id>/<auth>"
You can also customize this to trigger only on specific directories by changing the //depot/...
bit.
Note: It is really important to keep the tab before the trigger line, otherwise the server will not recognize it.
p4 triggers documentation
You can also run p4 help triggers
for more info
At this point, everything should be working as intended! Submit a new changelist and check it out!
In item 1.2 it was mentioned that it's easier if the user running the p4 describe
command doesn't have a timeout so you don't need to reauthenticate from time to time.
For security reasons, it is better to set up a user that has only read access to the depot, and can only be used from the localhost.
- On the P4Admin app, create a new group with
unset
session time out - Create a new user and assign it to that group.
- On the permissions tab, add the following line:
Access Level | User / Group | Name | Host | Folder / File |
---|---|---|---|---|
read | user | (the username you created) | 127.0.0.1 | //depot/... |
This will mean this new user will have no session timeout, but will only be able to read the depot, and from the localhost.