Created
April 8, 2023 21:49
-
-
Save sdesalas/54538062b9297a0efc0477f972baa4f8 to your computer and use it in GitHub Desktop.
Self CICD using Cron + Git
This file contains hidden or 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
#!/bin/bash | |
# | |
# Helps redeploy services when there are git remote changes | |
# | |
# @usage: sudo ./check.sh [branch] [--force] | |
# @example: sudo ./check.sh origin/master --force | |
PATH=$PATH:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin | |
FETCH=$(git fetch) | |
UPSTREAM=${1:-'origin/master'} | |
LOCAL=$(git rev-parse @) | |
REMOTE=$(git rev-parse "$UPSTREAM") | |
BASE=$(git merge-base @ "$UPSTREAM") | |
function rebuild() { | |
echo "rebuild()" | |
cd .. | |
git pull | |
docker-compose up -d --build | |
} | |
if [ "$2" = "--force" ]; then | |
echo "$(date +%FT%T) --force rebuild" | |
rebuild | |
elif [ $LOCAL = $REMOTE ]; then | |
echo "$(date +%FT%T) Up-to-date" | |
elif [ $LOCAL = $BASE ]; then | |
echo "$(date +%FT%T) Need to pull" | |
rebuild | |
elif [ $REMOTE = $BASE ]; then | |
echo "$(date +%FT%T) Need to push" | |
else | |
echo "$(date +%FT%T) Diverged" | |
fi | |
This file contains hidden or 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
#! /bin/bash | |
# | |
# Installs updater using cron+git | |
# Check git every minute to compare remote with local | |
# Logs output to `update.log` | |
# | |
# | |
# 1) Cron doesnt know the script directory so find out where it is | |
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) | |
# 2) Check git remote with local, log to `update.log` | |
(crontab -l 2>/dev/null; echo "*/1 * * * * cd $SCRIPT_DIR && ./check.sh >> update.log 2>&1") | sudo crontab - | |
# 3) Keep the logs trimmed to last 1000 lines | |
(crontab -l 2>/dev/null; echo "0 * * * * cd $SCRIPT_DIR && tail -1000 update.log | cat > update.log") | sudo crontab - | |
# Done | |
crontab -l | |
echo "" | |
echo "Updater installed. Check the 'update.log' in a minute or two." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment