Skip to content

Instantly share code, notes, and snippets.

@thinkier
Created April 4, 2022 07:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thinkier/5bd27986f10c07eff4fd57097699e4a9 to your computer and use it in GitHub Desktop.
Save thinkier/5bd27986f10c07eff4fd57097699e4a9 to your computer and use it in GitHub Desktop.
Automanted code deployment based on github webhooks (on pull request merge into the main branch)
<?php
const REPO_DIR = "/env";
const DISCORD_WEBHOOK = "...";
const TOKEN = "...";
if (key_exists('auth', $_GET) && $_GET['auth'] == TOKEN) {
$payload = json_decode($_POST['payload'], true);
if (key_exists("action", $payload) &&
$payload["action"] == "closed" &&
$payload["pull_request"]["merged_at"] != null &&
$payload["pull_request"]["base"]["ref"] == "main") {
header("HTTP/1.1 202 Accepted");
$out = "";
$res = 0;
chdir(REPO_DIR);
exec("git pull", $out, $res);
if ($res == 0) {
notifyDiscord([
"content" => "Code deployed! Consult the pull request above for more info!"
]);
}
} else {
header("HTTP/1.1 204 No Content");
}
die();
}
header("HTTP/1.1 403 Forbidden");
?>
<h1>403 Forbidden</h1>
<?php
function notifyDiscord($body)
{
$curl = curl_init(DISCORD_WEBHOOK);
curl_setopt($curl, CURLOPT_URL, DISCORD_WEBHOOK);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, [
"Content-Type: application/json"
]);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($body));
curl_exec($curl);
curl_close($curl);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment