Automating Scheduled Releases with GitHub Actions
Contents
In the previous post, I learned what goes into a GitHub Actions workflow.
Understanding What Goes into a GitHub Actions Workflow
In this post, I've put together an idea for automating scheduled releases with GitHub Actions.
Note
The setup in this post assumes the following.
- The workflow that automates releases, which this post creates, and the workflow that deploys are separate
- The deploy workflow starts, triggered by the tag attached at release time
- This post covers only the workflow that automates releases
The flow up to deployment is as follows.
GitHub Actions (cron)
↓
Create a release (the tag is also created automatically by a rule)
↓
The deploy workflow starts, triggered by the tag
↓
Deploy
name: Auto Release
on:
schedule:
# Starts automatically with cron. Here it is set to every Monday 09:00 JST (= 00:00 UTC)
- cron: "0 0 * * 1"
workflow_dispatch: {}
permissions:
contents: write
jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Fetch tags
run: git fetch --tags --force
- name: Compute next tag (vX.Y.Z)
id: ver
shell: bash
run: |
latest=$(git tag -l 'v[0-9]*.[0-9]*.[0-9]*' --sort=-v:refname | head -n 1 || true)
[ -n "$latest" ] || latest="v0.0.0"
ver="${latest#v}"
IFS='.' read -r major minor patch <<< "$ver"
patch=$((patch + 1))
next="v${major}.${minor}.${patch}"
echo "next=$next" >> "$GITHUB_OUTPUT"
- name: Create and push tag
run: |
tag="${{ steps.ver.outputs.next }}"
if git rev-parse "$tag" >/dev/null 2>&1; then
echo "Tag already exists: $tag"
exit 0
fi
git tag "$tag"
git push origin "$tag"
- name: Create GitHub Release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
tag="${{ steps.ver.outputs.next }}"
gh release create "$tag" \
--title "$tag" \
--generate-notes
Note
Only workflows on the default branch (such as main) run. Be careful: putting it on a PR branch will not start it with cron.
From here, I'll go through what the workflow above does, from top to bottom.
on:
schedule:
- cron: "0 0 * * 1"
workflow_dispatch: {}
schedule: starts every Monday at 09:00 (JST) (00:00 in UTC)workflow_dispatch: added as a safety net so it can also be run manually when needed
permissions:
contents: write
This workflow
- pushes a tag
- creates a release
so it needs permission to write the repository's contents.
(Without this, git push origin <tag> and gh release create fail.)
- uses: actions/checkout@v4
with:
fetch-depth: 0
The default checkout can have shallow history or be missing tags.
It is safer to reliably get the history and tags with fetch-depth: 0.
On top of that, I fetch tags just in case:
- name: Fetch tags
run: git fetch --tags --force
Assuming tags in the vX.Y.Z format, it automatically increments the tag and creates it.
- name: Compute next tag (vX.Y.Z)
id: ver
shell: bash
run: |
latest=$(git tag -l 'v[0-9]*.[0-9]*.[0-9]*' --sort=-v:refname | head -n 1 || true)
[ -n "$latest" ] || latest="v0.0.0"
ver="${latest#v}"
IFS='.' read -r major minor patch <<< "$ver"
patch=$((patch + 1))
next="v${major}.${minor}.${patch}"
echo "next=$next" >> "$GITHUB_OUTPUT"
What it intends to do:
- Find the single latest tag among the existing
vX.Y.Ztags - If there is none, treat it as the first run with
v0.0.0 - Add 1 to only
Z(patch) to make the next tag
- name: Create and push tag
run: |
tag="${{ steps.ver.outputs.next }}"
if git rev-parse "$tag" >/dev/null 2>&1; then
echo "Tag already exists: $tag"
exit 0
fi
git tag "$tag"
git push origin "$tag"
If a tag with the same name exists, it exits. (This makes accidents less likely even if concurrent runs or manual tagging get mixed in.)
- name: Create GitHub Release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
tag="${{ steps.ver.outputs.next }}"
gh release create "$tag" \
--title "$tag" \
--generate-notes
--generate-notesuses GitHub's auto-generated release notes- It nicely summarizes things like the list of PRs and the commit diff
It starts, triggered by a tag
on:
push:
tags:
- "v*.*.*"
With this workflow in place, you can automate the work needed for scheduled releases. Whether a deploy actually runs can be held back on the deploy workflow side, for example by requiring approval, so you can also take steps to run it safely.
Translated from the Japanese original.
Thanks. I'll look into it.
Sorry, that didn't go through. Please try again later.