Skip to content

Instantly share code, notes, and snippets.

@nothub
Last active February 15, 2023 19:22
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 nothub/b530464e66c889c06d99feb756683219 to your computer and use it in GitHub Desktop.
Save nothub/b530464e66c889c06d99feb756683219 to your computer and use it in GitHub Desktop.
when ci scheduler is big meanie
#!/usr/bin/env bash
# Github is a big meanie disabling your scheduled workflows?
# Not a problem, just schedule ci jobs by pushing git tags!
# echo git@github.com:user/repo-c.git git@github.com:user/repo-d.git | ./citag.sh git@github.com:user/repo-a.git https://github.com/user/repo-b.git
# 42 */12 * * * /foo/bar/citag git@github.com:user/repo.git
set -o errexit
set -o nounset
set -o pipefail
usage() {
set +o xtrace
# docopt syntax
echo "Usage: $(basename "${BASH_SOURCE[0]}") [-t=tag] [-v] [-h] [--] (<url>...|-)
Create or refresh git tags.
Repo URLs will be read from arguments or stdin.
Options:
-t Tag literal to be used
-v Enable verbose output
-h Print help and exit"
}
tag() {
url="${1}"
# fetch bare repo
local dir
dir="$(mktemp -d)"
if ! git clone --bare "${url}" "${dir}"; then
printf >&2 'Failed to clone: %s\n\n' "${url}"
return
fi
cd "${dir}"
# delete tag
git tag -d "${tag}" || true
if git push --delete origin "${tag}"; then
# if we continue directly, the tag
# might still exist on the server.
sleep 1
fi
# (re)create tag
git tag "${tag}" master
git push origin "${tag}"
printf >&2 'Successfuly tagged: %s\n' "${url}"
# cleanup
rm -rf "${dir}"
}
tag="citag"
while getopts t:vh opt; do
case $opt in
t) tag="$OPTARG" ;;
v) set -o xtrace ;;
h)
usage
exit 0
;;
*)
usage
exit 1
;;
esac
done
shift $((OPTIND - 1))
if [[ -z "${tag}" ]]; then
printf >&2 'Empty tag is not allowed!\n'
exit 1
fi
# get urls from args
urls=("$@")
# get urls from stdin
if [[ -p /dev/stdin ]]; then
while IFS= read -r line; do
for word in ${line}; do
urls+=("${word}")
done
done
fi
if [[ ${#urls[@]} -eq 0 ]]; then
usage
exit 1
fi
for url in "${urls[@]}"; do
tag "${url}" &
done
wait
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment