Created
April 4, 2022 07:19
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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