-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebhook.gitlab.php
144 lines (123 loc) · 5.76 KB
/
webhook.gitlab.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
/**
* projectr
* Tools for deployments and project management
*
* GitLab frontend for deployments
*
* @author Lukas Bestle <[email protected]>
* @copyright Copyright 2014 Lukas Bestle
* @license MIT
* @file webhook.gitlab.php
*/
// 1. Configuration
// ====================
// Long token used as authentication (appended as GET parameter: https://hooks.example.com/webhook.gitlab.php?token=<AUTH_TOKEN>)
// ATTENTION: Make sure this is long and secure, as anyone could checkout any commit of your projects otherwise
define('AUTH_TOKEN', '<long token>');
// Path where you installed the scripts from the "bin" directory of this repository
// This is required, as PHP doesn't automatically use the environment and therefore your $PATH from your shell
define('TOOLKIT_PATH', '/home/<user>/bin');
// 2. Setup
// ====================
/**
* This is an example webhook for GitLab servers.
* It's fairly simple to set this script up on your server/Uberspace, here's how:
*
* 1. Setup the toolkit, so you can access the tools from your SSH shell (more about that in README.md)
* 2. Take a look at the settings above.
* 2.1. Seriously, have you set an auth token? Yes? Alright.
* 3. Place this script in a directory in ~/web/ (something like "~/web/hooks.example.com")
* 4. You should now be able to access https://hooks.example.com/webhook.gitlab.php?token=<AUTH_TOKEN>
* This should output "Authenticated, but no POST body sent.".
*
* Now you can create projects and let this script deploy them for you:
*
* 5. If your project is a private repository:
* 5.1. Generate an SSH key by using `ssh-keygen`. The default values are alright for this purpose.
* 5.2. Copy the contents of `~/.ssh/id_rsa.pub` to your clipboard and add it as "Deploy Key" in your project's GitLab settings interface.
* 6. Create the project on the server by running either `project_add <path> <exact SSH url from GitLab>#<branch to deploy>` or
* `site_add <name> <exact SSH url from GitLab>#<branch to deploy>`.
* It's important to use the exact SSH url (not the HTTPS one) so this script can find the project!
* 7. Add the URL (the one you tested in 4.) of the script to your project's web hooks.
* 8. Hit "Test Hook" in GitLab's settings interface. The project should be deployed a few seconds later.
*
* Troubleshooting:
* - Use `tail -f <path to project>/logs/*.log`. This should tell you what went wrong.
* - Check if the TOOLKIT_PATH you set above is correct.
* - Check if you set the origin of your project to the correct URL.
* - Open the web hook URL you entered in GitLab in your browser and check if the output is "Authenticated, but no POST body sent.".
* - If there is no log file and you can't find the reason yourself, it is complicated. ;)
* Debugging this is not easy (GitLab does not seem to store logs of its requests), but you can try to craft a JSON body like GitLab,
* send it to this script by using cURL or GUI tools like Rested and it will tell you exactly what went wrong.
*/
// 3. Done
// The code starts here
// ====================
// We are always returning plain text
header('Content-Type: text/plain');
// Check if an auth token has been set
if (AUTH_TOKEN === '<long token>') {
http_response_code(500);
die('No auth token has been set in ' . basename(__FILE__) . ". This script won't work without one.");
}
// Check if the authentication is valid
if (isset($_GET['token']) !== true) {
http_response_code(401);
die('Missing authentication.');
}
if (hash_equals(AUTH_TOKEN, $_GET['token']) !== true) {
http_response_code(403);
die('Invalid authentication.');
}
// Get the request body
$input = file_get_contents('php://input');
if (!$input) {
http_response_code(400);
die('Authenticated, but no POST body sent.');
}
// Parse payload
$payload = json_decode($input, true);
if (is_array($payload) !== true) {
http_response_code(400);
die('Invalid payload (no JSON?).');
}
// Get some interesting information from the payload
$url = $payload['repository']['url'];
$commit = $payload['after'];
$ref = $payload['ref'];
if (preg_match('{(?:.*/){2}(.*)}', $ref, $matches) !== 1) {
http_response_code(400);
die('Invalid ref field (does not match regular expression "(?:.*/){2}(.*)").');
}
$branch = $matches[1];
// Debug
echo "Received commit hash \"$commit\" for repository URL \"$url\" (branch \"$branch\").\n";
// Determine the path to the projects file
$xdgProjectsFile = ($_ENV['XDG_CONFIG_HOME'] ?? ($_SERVER['HOME'] . '/.config')) . '/projectr/projects';
$projectsFile = is_file($xdgProjectsFile) === true ? $xdgProjectsFile : $_SERVER['HOME'] . '/.projects';
// Open the projects file and iterate through every project
$listPointer = fopen($projectsFile, 'r');
$exitCode = 0;
while (($project = fgets($listPointer)) !== false) {
// Trim whitespace
$project = trim($project);
// Only deployable projects are interesting for us
if (is_file($project . '/.origin') !== true || is_file($project . '/.branch') !== true) {
continue;
}
// If there is a .origin and .branch file, check if they match
if (trim(file_get_contents($project . '/.origin')) === $url && trim(file_get_contents($project . '/.branch')) === $branch) {
// Found the right project
echo "Found project at $project, running deploy script.\n";
// Run deploy script (in the background, because GitLab doesn't like responses > 10sec by default)
passthru('export PATH=' . escapeshellarg(TOOLKIT_PATH) . ':$PATH; project_deploy ' . escapeshellarg($project) . ' ' . escapeshellarg($commit) . ' &> /dev/null &', $exitCode);
// If it didn't work, add debug statement
if ($exitCode !== 0) {
echo "Something didn't work.\n";
}
}
}
// Iterated through every project, exit
http_response_code(($exitCode === 0)? 200 : 500);
die('All done.');