Skip to content

Instantly share code, notes, and snippets.

@chadwhitacre

chadwhitacre/ons Secret

Last active March 18, 2021 19:28
Show Gist options
  • Save chadwhitacre/a6bf2a7e5fc2a6222a7bd53b9b7626c2 to your computer and use it in GitHub Desktop.
Save chadwhitacre/a6bf2a7e5fc2a6222a7bd53b9b7626c2 to your computer and use it in GitHub Desktop.
#!/usr/bin/env zsh
set -eu
# Helper for developing and testing GitHub Actions. Some assumptions:
#
# - You have a dev repo separate from the main repo where the actions will
# ultimately be deployed (deployment is out of scope here).
#
# - Your dev repo has additional test files for workflows, themselves
# implemented as workflows and using the naming convention <foo>-test.yml.
#
# - Your local working copy directory structure for the dev repo is
# :org/:repo/.
# Run this script after saving edits to your local {workflow,workflow-test}.yml
# files, and it will commit, push, and run your tests within GitHub Actions.
changed_files=$(
git status --porcelain \
| awk '{print $2}'
)
helpers=$(
echo "${changed_files}" \
| grep '^.github/workflows/test-helpers' \
| sed -e 's/^.github\/workflows\/test-helpers\///'
)
if [ ${helpers} ]; then
echo "Helpers changed (will run all tests):"
for helper in ${helpers}; do
echo " ${helper}"
done
tests=$(
ls .github/workflows/*-test.yml
)
else
tests=$(
git status --porcelain \
| awk '{print $2}' \
| grep '^.github/workflows/[^/]*\.yml'
)
fi
if [ ${tests} ]; then
tests=$(
echo "${tests}" \
| sed -e 's/^.github\/workflows\///' \
| sed -e 's/-test//' \
| uniq \
| sed -e 's/\.yml$/-test.yml/'
)
echo "Will run these tests:"
for test in ${tests}; do
# Merp. Would be better to remove from ${tests}.
test -f ".github/workflows/${test}" || continue
echo " ${test}"
done
else
echo "No tests will be run."
fi
git add .
git commit -m 'save'
git push
# `gh api` supports :owner and :repo in URLs but that's not working well for
# me with a multi-remote setup (origin and upstream). I'm working around that
# by inferring these from the filesystem.
repo=$(basename $(pwd))
owner=$(basename $(dirname $(pwd)))
for test in ${tests}; do
test -f ".github/workflows/${test}" || continue
DEBUG=1 gh api \
--method POST \
--input <(echo '{"ref":"main"}') \
--silent \
"repos/${owner}/${repo}/actions/workflows/${test}/dispatches"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment