Skip to content

Instantly share code, notes, and snippets.

@nobu
Created May 12, 2023 05:56
Show Gist options
  • Save nobu/1eb131daa9f63ed4a38b30c5c0c5b4ab to your computer and use it in GitHub Desktop.
Save nobu/1eb131daa9f63ed4a38b30c5c0c5b4ab to your computer and use it in GitHub Desktop.
Cancel GitHub Actions Workflows
#!/bin/bash
repo_arg=()
limit_arg=
list_args=()
sha_arg=
dryrun=
for arg; do
if test ${#repo_arg[@]} = 1; then
repo_arg=("${repo_arg[@]}" "$arg")
elif test "${sha_arg}" = true; then
sha_arg="$arg"
continue
fi
case "$arg" in
-R|--repo)
repo_arg=($arg)
;;
-L|--limit)
limit_arg=1
;;
--sha)
sha_arg=true
continue
;;
--dryrun)
dryrun=true
continue
;;
--help)
echo "usage: $0 [options]"
echo " -R|--repo repository"
echo " -b|--branch branch"
echo " -L|--limit num"
echo " --sha commit-hash"
exit
;;
esac
list_args=("${list_args[@]}" "$arg")
done
if [ -z $limit_arg ]; then
list_args=(--limit 25 "${list_args[@]}")
fi
jq='select(.status!="completed")'
case "$sha_arg" in
'')
:
;;
true)
echo "$0: missing commit hash for --sha option" 1>2
exit 1
;;
*)
jq="$jq"' | select(.headSha | startswith("'"$sha_arg"'"))'
;;
esac
set gh run list "${list_args[@]}" --json databaseId,status,headSha --jq ".[] | $jq"
set "$@"
if [ -n "$dryrun" ]; then
exec "$@"
fi
for id in $("$@".databaseId); do
gh run "${repo_arg[@]}" cancel $id
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment