Skip to content

Instantly share code, notes, and snippets.

@casjay
Created September 3, 2023 16:27
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 casjay/2b725ad7a21c252bc55d30923e88e8b6 to your computer and use it in GitHub Desktop.
Save casjay/2b725ad7a21c252bc55d30923e88e8b6 to your computer and use it in GitHub Desktop.
cron script to build docker containers on a schedule
#!/usr/bin/env bash
# shellcheck shell=bash
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
##@Version : 202309022354-git
# @@Author : Jason Hempstead
# @@Contact : git-admin@casjaysdev.pro
# @@License : WTFPL
# @@ReadME : cron-buildx --help
# @@Copyright : Copyright: (c) 2023 Jason Hempstead, Casjays Developments
# @@Created : Saturday, Sep 02, 2023 23:54 EDT
# @@File : cron-buildx
# @@Description : Run buildx on a schedule
# @@Changelog : newScript
# @@TODO : Refactor code
# @@Other :
# @@Resource :
# @@Terminal App : no
# @@sudo/root : no
# @@Template : bash/system
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# shell check options
# shellcheck disable=SC2317
# shellcheck disable=SC2120
# shellcheck disable=SC2155
# shellcheck disable=SC2199
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
__trap_timer() {
local retval=$?
if [ -n "$(command -v bc 2>/dev/null)" ]; then
local stop_timer="$(date +%s.%N)"
local dt=$(echo "$stop_timer - $START_TIMER" | bc)
local dd=$(echo "$dt/86400" | bc)
local dt2=$(echo "$dt-86400*$dd" | bc)
local dh=$(echo "$dt2/3600" | bc)
local dt3=$(echo "$dt2-3600*$dh" | bc)
local dm=$(echo "$dt3/60" | bc)
local ds=$(echo "$dt3-60*$dm" | bc)
TIMER_SHOW_RESULTS="$(printf '%s\n' "$(LC_NUMERIC=C printf "Total runtime: %d Days, %02d Hours, %02d Minutes, %02.4f Seconds\n" $dd $dh $dm $ds)")"
fi
return $retval
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
set -x
START_TIMER="$(date +%s.%N)"
exitCode=0
printf 'Starting buildx on %s\n' "$(date)" >/var/log/buildx.log
for d in ~/Projects/github/casjaysdev/docker-* ~/Projects/github/casjaysdevdocker/*;do
if buildx --dir $d all --raw >>"$HOME/.local/log/buildx/cron.log" 2>&1; then
echo "Built $d" >>/var/log/buildx.log
exitCode=$((0+$exitCode))
else
echo "Failed to build $d" >>/var/log/buildx.buildx.log
exitCode=$((0+$exitCode))
fi
dockermgr prune &>/dev/null
done
__trap_timer >>/var/log/buildx.log
printf '%s\n%s\n' "$TIMER_SHOW_RESULTS" "Exit status is $exitCode">>/var/log/buildx.log
cat /var/log/buildx.log | web-notify telegram
rm -Rf /var/log/buildx.log
exit $exitCode
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment