Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions .github/workflows/release.yml
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a default shell as done in https://github.com/NixOS/nixpkgs/blob/7a7849f5f075f6cfa8d6607c65048b7a22479bcb/.github/workflows/build.yml#L21-L23. A nice side effect of this is to get some sane shell behavior (set -euo pipefail or something like that).

Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
name: Release on Version Change

on:
push:
branches:
- master
paths:
- 'nixfmt.cabal'

jobs:
release-if-version-changed:
runs-on: ubuntu-latest

steps:
- name: Checkout main
uses: actions/checkout@v4
with:
fetch-depth: 2 # need previous commit for comparison

- name: Get version from nixfmt.cabal
id: get_version
run: |
CABAL_FILE="nixfmt.cabal"

# Extract the version
VERSION=$(grep -m1 "^version:" $CABAL_FILE | awk '{print $2}')
echo "Version found in cabal: $VERSION"

# Get previous version from last commit
PREV_VERSION=$(git show HEAD~1:"$CABAL_FILE" | grep -m1 "^version:" | awk '{print $2}' || echo "none")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm worried about us failing to parse the file and then strange things happening. Could we remove the || ... part?

Suggested change
PREV_VERSION=$(git show HEAD~1:"$CABAL_FILE" | grep -m1 "^version:" | awk '{print $2}' || echo "none")
PREV_VERSION=$(git show HEAD~1:"$CABAL_FILE" | grep -m1 "^version:" | awk '{print $2}')

echo "Previous version: $PREV_VERSION"

# Export for later steps
echo "version=$VERSION" >> $GITHUB_OUTPUT
echo "prev_version=$PREV_VERSION" >> $GITHUB_OUTPUT

- name: Check if version changed
id: compare
run: |
if [ "${{ steps.get_version.outputs.version }}" != "${{ steps.get_version.outputs.prev_version }}" ]; then
Comment on lines +39 to +40
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's important to pass inputs via env vars instead of directly interpolating into the shell script. Here's how:

Suggested change
run: |
if [ "${{ steps.get_version.outputs.version }}" != "${{ steps.get_version.outputs.prev_version }}" ]; then
env:
PREV_VERSION: ${{ steps.get_version.outputs.prev_version }}
VERSION: ${{ steps.get_version.outputs.version }}
run: |
if [ "$PREV_VERSION" != "$VERSION" ]; then

Similar other interpolations below.

echo "version_changed=true" >> $GITHUB_ENV
echo "::notice ::✅ Version changed from ${{ steps.get_version.outputs.prev_version }} to ${{ steps.get_version.outputs.version }}"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Indentation is a bit wonky here (and in the else block). Please fix.

else
echo "version_changed=false" >> $GITHUB_ENV
echo "::notice ::Version did not change skipping release."
fi
- name: Extract changelog section for current version
if: env.version_changed == 'true'
run: |
VERSION="${{ steps.get_version.outputs.version }}"
echo "Extracting changelog section for version $VERSION"

# Extract everything after the "## <version>" header until the next "## "
awk -v ver="$VERSION" '
$0 ~ "^##[[:space:]]+" ver {found=1; next}
found && /^##[[:space:]]+/ {exit}
found
' CHANGELOG.md > RELEASE_NOTES.md

if [ ! -s RELEASE_NOTES.md ]; then
echo "::error ::⚠️ No changelog section found for version $VERSION"
echo "(Make sure you have a header like '## $VERSION -- YYYY-MM-DD')"
echo "- No notes will be attached to the release -"
echo "No changelog section found for version $VERSION." > RELEASE_NOTES.md
fi
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's explicitly exit nonzero:

Suggested change
fi
exit 1
fi


echo "---- Extracted Changelog ----"
cat RELEASE_NOTES.md
echo "--------------------------"

- name: Setup Nix
if: env.version_changed == 'true'
uses: cachix/install-nix-action@v26

- name: Setup Cachix cache
if: env.version_changed == 'true'
uses: cachix/cachix-action@v14
with:
name: nixos-nixfmt
authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}'

- name: Build static binary
if: env.version_changed == 'true'
run: |
nix build -L .#nixfmt-static
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use stable nix here instead of flakes:

Suggested change
nix build -L .#nixfmt-static
nix-build -A packages.nixfmt-static

mkdir -p release
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we use this release directory? Can we remove this line?

echo "artifact_path=result/bin/nixfmt" >> $GITHUB_ENV

- name: Create Git tag
if: env.version_changed == 'true'
run: |
VERSION="${{ steps.get_version.outputs.version }}"
TAG="v${VERSION}"

echo "Creating tag $TAG"

git config user.name "Github Actions"
git config user.email "[email protected]"
git tag -a "$TAG" -m "Release $TAG"
git push origin "$TAG"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use the gh cli directly (it's installed by default). Take a look at gh release create --help (it will likely create the tag for us).


- name: Create GitHub Release
if: env.version_changed == 'true'
uses: softprops/action-gh-release@v2
with:
tag_name: "v${{ steps.get_version.outputs.version }}"
name: "v${{ steps.get_version.outputs.version }}"
body_path: RELEASE_NOTES.md
files: "${{ env.artifact_path }}"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's just inline artifact_path:

Suggested change
files: "${{ env.artifact_path }}"
files: ./result/bin/nixfmt

env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2 changes: 1 addition & 1 deletion MAINTENANCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ rather than the version from the cabalfile.
- Bump the version in the [cabal file](./nixfmt.cabal)
- Update the [changelog](./CHANGELOG.md) with the new version
- Create a PR with the above changes and merge it
- Create a [new GitHub release](https://github.com/NixOS/nixfmt/releases/new) with tag matching the version and set the release notes to this versions changelog
- After a successful build, a [new GitHub release](https://github.com/NixOS/nixfmt/releases) will automatically be created, along with a tag matching the version, via a GitHub Actions workflow. Keep an eye on the process to ensure everything runs smoothly!