Skip to content

Instantly share code, notes, and snippets.

@gaborszita
Last active November 25, 2022 22:04
Show Gist options
  • Save gaborszita/28f64eaf78dfdec550a40cc0cb8f8c56 to your computer and use it in GitHub Desktop.
Save gaborszita/28f64eaf78dfdec550a40cc0cb8f8c56 to your computer and use it in GitHub Desktop.
MiloBot automatic deploy from GitHub
#!/bin/bash
cd "$(dirname "$0")"
screen -d -m sh -c "./deploy.sh > ./deploy-output.txt"
<?php
define('WATCH_REPO', 'MiloBotDev/MiloBot');
define('WATCH_REF', 'refs/heads/master');
define('SECRET', 'set_secret_here');
define('LOG_FILE', '/opt/milobot/deploy/deploy.log');
define('DEPLOY_COMMAND', 'sudo -u milobot /opt/milobot/deploy/deploy-start.sh');
$fp = fopen(LOG_FILE, 'a');
if (!$fp) {
error_log('Unable to open log file.');
http_response_code(400);
die('Internal error');
}
fwrite($fp, date('Y-m-d H:i:s') . ' ');
if (verifySignature() === false) {
http_response_code(401);
echo 'Invalid signature';
fwrite($fp, 'Terminated due to invalid signature');
} else if (getallheaders()['X-GitHub-Event'] !== 'push') {
echo 'OK Event received that is not push. No action has been taken.';
fwrite($fp, 'Event received that is not push. No action has been taken.');
} else if (json_decode($_POST['payload'], true)['repository']['full_name'] !== WATCH_REPO) {
echo 'OK Repository not matching. No action has been taken.';
fwrite($fp, 'Repository not matching. No action has been taken.');
} else if (json_decode($_POST['payload'], true)['ref'] !== WATCH_REF) {
echo 'OK Ref not maching. No action has been taken.';
fwrite($fp, 'Ref not matching. No action has been taken.');
} else {
shell_exec(DEPLOY_COMMAND);
http_response_code(202);
echo 'OK Deploy script ran';
fwrite($fp, 'Deploy script ran');
}
fwrite($fp, PHP_EOL);
fclose($fp);
function verifySignature() {
$body = file_get_contents('php://input');
$headers = getallheaders();
if (array_key_exists('X-Hub-Signature-256', $headers) && is_string($headers['X-Hub-Signature-256'])) {
return hash_equals('sha256='.hash_hmac('sha256', $body, SECRET), $headers['X-Hub-Signature-256']);
} else {
return false;
}
}
#!/bin/bash
DIR=/opt/milobot/deploy
CLONE_DIR=MiloBot-deploy-clone
LOCK_FILE=deploy-lock
MILOBOT_JAR=/opt/milobot/bot/Milobot.jar
(
# lock to prevent multiple script instances running at the same time
flock 9
echo 'Starting Milobot automatic deploy'
cd $DIR
if [ $? -ne 0 ]
then
echo 'Unable to cd to deploy directory'
exit 1
fi
echo 'Deleting build directory (if exists) before proceeding'
rm -rf $CLONE_DIR
if [ $? -ne 0 ]
then
echo 'Unable to delete build directory'
exit 1
fi
echo 'Cloning MiloBot repository from GitHub...'
git clone https://github.com/RubenJ01/MiloBot $CLONE_DIR
if [ $? -ne 0 ]
then
echo 'Unable to clone repository'
exit 1
fi
cd $CLONE_DIR
if [ $? -ne 0 ]
then
echo 'Unable to cd to cloned repository directory'
exit 1
fi
echo 'Running maven package'
mvn package -Dmaven.test.skip
if [ $? -ne 0 ]
then
echo 'Maven package failed'
exit 1
fi
cd target
if [ $? -ne 0 ]
then
echo 'Failed to cd to target directory'
exit 1
fi
jar_file=`find . -iname "Milobot-*.jar" -print -quit`
if [ $? -ne 0 ]
then
echo 'Finding jar file - find failed with non-zero exit code'
exit 1
fi
if [ -z "$jar_file" ]
then
echo 'No jar file found for Milobot'
exit 1
fi
echo "Jar file found: $jar_file"
echo 'Stopping Milobot service'
sudo systemctl stop milobot
if [ $? -ne 0 ]
then
echo 'Failed to stop Milobot service'
exit 1
fi
echo 'Copying Milobot jar file'
cp $jar_file $MILOBOT_JAR
if [ $? -ne 0 ]
then
echo 'Failed to copy Milobot jar file'
exit 1
fi
echo 'Starting Milobot service'
sudo systemctl start milobot
if [ $? -ne 0 ]
then
echo 'Failed to start Milobot service'
exit 1
fi
# clean up
echo 'Cleaning up'
echo 'Cleaning up maven cache'
rm -rf ~/.m2/repository
if [ $? -ne 0 ]
then
echo 'WARNING: Failed to clean up maven cache'
fi
echo 'Deleting build directory'
cd $DIR
if [ $? -ne 0 ]
then
echo 'Unable to cd to deploy directory'
exit 1
fi
rm -rf $CLONE_DIR
if [ $? -ne 0 ]
then
echo 'WARNING: Unable to delete build directory'
fi
echo 'Successfully deployed Milobot!'
) 9>$DIR/$LOCK_FILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment