Using bashew in GitHub Actions
15 Oct 2022GitHub Actions, used in countless CI/CD setups, are a good example of the ubiquity of bash scripting. Most of the run:
lines in an Action YML document are nothing but (a sequence of) bash command lines.
I knew this could be a good match for bashew
scripts-based bash scripts, but I only found some time to try that out last weekend.
TL;DR:
bashew
scripts are a great way to streamline your GitHub Actions
shot-scraper
I got inspired by Automating screenshots using shot-scraper via Hacker News. The shot-scraper
documentation describes how to set it up for using GitHub Actions to do the actual screen grabbing. I used that as a starting point and I distilled it into an efficient example project: pforret/bashew_github_action.
bashew_github_action
I created 3 sub commands in the bashew_github_action.sh
script:
- gha:before: does the GitHub Action setup (in this case: check if Python is installed, install shot-scraper and do initial shot-scraper setup)
- gha:execute: runs the actual payload. In this case: use shot-scraper to get screenshots of a number of websites
- gha:after: save the results by doing an automatic
git commit
andgit push
.
The YML code to run each of them in sequence, is super easy:
- name: gha:before
run: |
./bashew_github_action.sh gha:before
- name: gha:execute
run: |
./bashew_github_action.sh gha:execute
- name: gha:after
run: |-
./bashew_github_action.sh gha:after
This is one of the screenshots it generates:
Advantages:
Using a bashew
script to streamline your GitHub Actions is a good idea because:
- Single Responsibility. You edit your setup code in the same file as the payload or the wrap-up code. You don’t touch the
action.yml
file anymore after initial setup. - Cleaner Code. The YML syntax forces you to a
do this && do that && do the other
one-liner format. A script allows you to do if/then, for/do, while/do, recursion … in an easy and clean way. - Easy Debugging.
bashew
has built-in support for log files and optional verbose mode. It also cleans up log files after 30 days, so they don’t clog up your repo. - Self-Contained. You don’t need external libraries or runtimes.
bash
runs on most Action Runners (Linux/MacOS) and eachbashew
script contains its entire library of functions.
Try it out
# install bashew, if necessary
basher install pforret/bashew
# add a new bashew script to your project
bashew script
# add subcommands setup/payload/wrapup, or gha:before,gha:execute,gha:after
or even easier:
# clone the example repo
git clone https://github.com/pforret/bashew_github_action.git
# copy bashew_github_action.sh and .github/workflows/shots.yml to your project
# start editing the gha:before and gha:execute subcommands
gha:before)
[[ -z "${RUNNER_OS:-}" ]] && IO:die "Only run inside a Github Action"
Os:require pip python3
pip install shot-scraper
shot-scraper install
;;