GitHub CLI (gh) for WordPress Plugin Release Automation

GitHub CLI (gh) for plugin release automation

A plugin release on GitHub is a series of clicks: check that CI is green on the release commit, create the tag, write the release notes, upload the ZIP, maybe update a draft into a final release. The GitHub CLI gh does each of these in one command, so the whole release becomes a short script that anyone on the team can run the same way.

Problem: Releases of a plugin are done by hand in the GitHub web interface. Twice, a ZIP from a local build with debug files was uploaded, and once a release was published while CI on the tagged commit had failed.

Solution: Write a release script that refuses to run with a dirty working tree, waits for the CI checks on the commit with gh run watch or checks gh pr checks, builds the ZIP with git archive, creates the release with gh release create including notes and the asset, and leaves it as a draft for a final review.

Install and log in

brew install gh               # or the packages for Debian/Ubuntu from cli.github.com
gh auth login                 # browser login, stores a token
gh auth status

In GitHub Actions, gh is preinstalled and uses the GITHUB_TOKEN when it is passed as GH_TOKEN in the environment.

The release script

#!/usr/bin/env bash
# bin/release.sh 2.5.0
set -euo pipefail
version="$1"
tag="v$version"

[ -z "$(git status --porcelain)" ] || { echo "working tree not clean"; exit 1; }
git fetch origin
[ "$(git rev-parse HEAD)" = "$(git rev-parse origin/main)" ] || { echo "HEAD is not origin/main"; exit 1; }

bin/check-version.sh "$version"

# CI must be green for this commit
run_id=$(gh run list --branch main --commit "$(git rev-parse HEAD)" --workflow tests.yml --json databaseId --jq '.[0].databaseId')
[ -n "$run_id" ] || { echo "no CI run for this commit"; exit 1; }
gh run watch "$run_id" --exit-status

git tag -a "$tag" -m "Release $version"
git push origin "$tag"

git archive --format=zip --prefix=ha-plugin/ -o "ha-plugin-$version.zip" "$tag"

gh release create "$tag" "ha-plugin-$version.zip" \
    --title "$version" \
    --notes-file <(git cliff --latest --strip all) \
    --draft --verify-tag
echo "Draft created: $(gh release view "$tag" --json url --jq .url)"
  • gh run list ... --json --jq returns machine-readable data; --commit finds the run for exactly this commit.
  • gh run watch --exit-status waits until the run finishes and exits non-zero if it failed, which stops the script under set -e.
  • gh release create uploads the ZIP as a release asset in the same call. --verify-tag refuses to create the tag implicitly, so the release is always attached to the tag pushed above. --draft leaves it unpublished until someone clicks Publish (or runs gh release edit "$tag" --draft=false).
  • The notes come from the commit history: Conventional Commits and git-cliff. Without it, --generate-notes lets GitHub write notes from the merged pull requests.

The version check script is the one from git tags and signed releases.

Other everyday commands

gh pr create --fill --base main          # PR from the current branch, title and body from commits
gh pr checks --watch                     # wait for the checks of the current PR
gh pr merge --squash --delete-branch
gh issue list --label bug --state open
gh release download v2.4.1 --pattern '*.zip'   # get a released ZIP for testing
gh api repos/{owner}/{repo}/releases/latest --jq .tag_name

gh api calls any REST endpoint with the stored authentication; {owner} and {repo} are filled from the current repository.

NOTE: The ZIP from git archive contains only tracked files. A plugin that needs vendor/ or built assets in the release must build them in a clean checkout of the tag (or in CI) and zip that folder instead; building in the developer’s working copy is how debug files end up in releases. The CI pipeline that runs the tests: GitHub Actions CI/CD for WordPress plugins.