Skip to content

Commit b672593

Browse files
authored
Merge pull request #354 from FlashOnFire/release_workflow
Add automated CI workflow for releases
2 parents 2153e60 + 5204483 commit b672593

2 files changed

Lines changed: 103 additions & 1 deletion

File tree

.github/workflows/release.yml

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
name: Release on Version Change
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
paths:
8+
- 'nixfmt.cabal'
9+
10+
defaults:
11+
run:
12+
shell: bash
13+
14+
jobs:
15+
release-if-version-changed:
16+
runs-on: ubuntu-latest
17+
18+
steps:
19+
- name: Checkout main
20+
uses: actions/checkout@v4
21+
with:
22+
fetch-depth: 2 # need previous commit for comparison
23+
24+
- name: Get version from nixfmt.cabal
25+
id: get_version
26+
run: |
27+
CABAL_FILE="nixfmt.cabal"
28+
29+
# Extract the version
30+
VERSION=$(grep -m1 "^version:" $CABAL_FILE | awk '{print $2}')
31+
echo "Version found in cabal: $VERSION"
32+
33+
# Get previous version from last commit
34+
PREV_VERSION=$(git show HEAD~1:"$CABAL_FILE" | grep -m1 "^version:" | awk '{print $2}')
35+
echo "Previous version: $PREV_VERSION"
36+
37+
# Export for later steps
38+
echo "version=$VERSION" >> $GITHUB_OUTPUT
39+
echo "prev_version=$PREV_VERSION" >> $GITHUB_OUTPUT
40+
41+
- name: Check if version changed
42+
id: compare
43+
env:
44+
PREV_VERSION: ${{ steps.get_version.outputs.prev_version }}
45+
VERSION: ${{ steps.get_version.outputs.version }}
46+
run: |
47+
if [ "$PREV_VERSION" != "$VERSION" ]; then
48+
echo "version_changed=true" >> $GITHUB_ENV
49+
echo "::notice ::✅ Version changed from $PREV_VERSION to $VERSION"
50+
else
51+
echo "version_changed=false" >> $GITHUB_ENV
52+
echo "::notice ::Version did not change, skipping release"
53+
fi
54+
- name: Extract changelog section for current version
55+
if: ${{ env.version_changed == 'true' }}
56+
env:
57+
VERSION: ${{ steps.get_version.outputs.version }}
58+
run: |
59+
echo "Extracting changelog section for version $VERSION"
60+
61+
# Extract everything after the "## <version>" header until the next "## "
62+
awk -v ver="$VERSION" '
63+
$0 ~ "^##[[:space:]]+" ver {found=1; next}
64+
found && /^##[[:space:]]+/ {exit}
65+
found
66+
' CHANGELOG.md > RELEASE_NOTES.md
67+
68+
if [ ! -s RELEASE_NOTES.md ]; then
69+
echo "::error ::⚠️ No changelog section found for version $VERSION"
70+
echo "(Make sure you have a header like '## $VERSION -- YYYY-MM-DD')"
71+
echo "- No notes will be attached to the release -"
72+
echo "No changelog section found for version $VERSION." > RELEASE_NOTES.md
73+
exit 1
74+
fi
75+
76+
echo "---- Extracted Changelog ----"
77+
cat RELEASE_NOTES.md
78+
echo "--------------------------"
79+
80+
- name: Setup Nix
81+
if: ${{ env.version_changed == 'true' }}
82+
uses: cachix/install-nix-action@v26
83+
84+
- name: Setup Cachix cache
85+
if: ${{ env.version_changed == 'true' }}
86+
uses: cachix/cachix-action@v14
87+
with:
88+
name: nixos-nixfmt
89+
authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}'
90+
91+
- name: Build static binary
92+
if: ${{ env.version_changed == 'true' }}
93+
run: |
94+
nix-build -A packages.nixfmt-static
95+
96+
- name: Create GitHub Release
97+
if: ${{ env.version_changed == 'true' }}
98+
env:
99+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
100+
VERSION: "${{ steps.get_version.outputs.version }}"
101+
run:
102+
gh release create v$VERSION ./result/bin/nixfmt --title "v$VERSION" --notes-file RELEASE_NOTES.md

MAINTENANCE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ rather than the version from the cabalfile.
88
- Bump the version in the [cabal file](./nixfmt.cabal)
99
- Update the [changelog](./CHANGELOG.md) with the new version
1010
- Create a PR with the above changes and merge it
11-
- 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
11+
- 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!

0 commit comments

Comments
 (0)