Skip to content

Commit 2b53894

Browse files
committed
feat: Enhancement to the release workflow
Signed-off-by: Kyle Vorster <[email protected]>
1 parent dd15082 commit 2b53894

File tree

1 file changed

+94
-22
lines changed

1 file changed

+94
-22
lines changed

.github/workflows/release.yml

Lines changed: 94 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,18 @@ on:
88
properties:
99
milestone_number:
1010
type: string
11+
1112
jobs:
1213
release:
1314
runs-on: ubuntu-latest
1415
steps:
16+
# Checkout the main branch with all history
1517
- name: Checkout Repository
1618
uses: actions/checkout@v2
1719
with:
1820
ref: main
1921
fetch-depth: 0 # Fetch all tags
20-
22+
2123
# Fetch merged pull request and determine release labels
2224
- uses: actions-ecosystem/action-get-merged-pull-request@v1
2325
id: get-merged-pull-request
@@ -29,28 +31,29 @@ jobs:
2931
if: ${{ steps.get-merged-pull-request.outputs.title != null }}
3032
with:
3133
github_token: ${{ secrets.GITHUB_TOKEN }}
32-
#labels: ${{ steps.get-merged-pull-request.outputs.labels }}
3334

34-
# Get the latest tag in the repositorys
35+
# Get the latest tag in the repository
3536
- uses: actions-ecosystem/action-get-latest-tag@v1
3637
id: get-latest-tag
3738
if: ${{ steps.release-label.outputs.level != null }}
3839
with:
3940
semver_only: true
4041

42+
# Setup Node.js environment
4143
- name: Setup Node.js (.npmrc)
4244
uses: actions/setup-node@v3
4345
with:
44-
node-version: 16.x
46+
node-version: 20.x
4547
registry-url: https://npm.pkg.github.com/
46-
# Defaults to the user or organization that owns the workflow file
4748
scope: '@frmscoe'
4849

50+
# Install dependencies
4951
- name: Install dependencies
5052
run: npm ci
5153
env:
5254
GH_TOKEN: '${{ secrets.GITHUB_TOKEN }}'
5355

56+
# Run Tests
5457
- name: Run Tests
5558
run: npm test
5659
env:
@@ -60,58 +63,60 @@ jobs:
6063
- name: Determine Release Type
6164
id: determine_release
6265
run: |
63-
export PREV_VERSION=$(git describe --abbrev=0 --tags)
66+
PREV_VERSION=$(git describe --abbrev=0 --tags)
6467
echo "Previous Version: $PREV_VERSION"
6568
66-
export COMMIT_MESSAGES=$(git log $PREV_VERSION^..HEAD --format=%B)
69+
COMMIT_MESSAGES=$(git log $PREV_VERSION^..HEAD --format=%B)
6770
echo "Commit Messages: $COMMIT_MESSAGES"
6871
6972
# Determine release type based on commit messages and labels
7073
RELEASE_TYPE="patch" # Default to patch
7174
7275
if echo "$COMMIT_MESSAGES" | grep -q -e "BREAKING CHANGE:"; then
7376
RELEASE_TYPE="major"
77+
elif echo "$COMMIT_MESSAGES" | grep -q -e "feat!:"; then
78+
RELEASE_TYPE="major"
7479
elif echo "$COMMIT_MESSAGES" | grep -q -e "feat:"; then
7580
RELEASE_TYPE="minor"
7681
elif echo "$COMMIT_MESSAGES" | grep -q -e "feat:" && (echo "$COMMIT_MESSAGES" | grep -q -e "fix:" || echo "$COMMIT_MESSAGES" | grep -q -e "enhancement:" || echo "$COMMIT_MESSAGES" | grep -q -e "docs:" || echo "$COMMIT_MESSAGES" | grep -q -e "refactor:" || echo "$COMMIT_MESSAGES" | grep -q -e "chore:"); then
7782
RELEASE_TYPE="minor"
78-
elif echo "$COMMIT_MESSAGES" | grep -q -e "fix:" -e "enhancement:" -e "docs:" -e "refactor:" -e "chore:"; then
83+
elif echo "$COMMIT_MESSAGES" | grep -q -e "fix:" -e "enhancement:" -e "docs:" -e "refactor:" -e "chore:" -e "build:" -e "ci:" -e "perf:" -e "style:" -e "test:" -e "chore(deps):" -e "chore(deps-dev):"; then
7984
RELEASE_TYPE="patch"
8085
fi
81-
86+
8287
echo "Release Type: $RELEASE_TYPE"
8388
echo "::set-output name=release_type::$RELEASE_TYPE"
8489
8590
# Bump the version based on the determined release type
8691
- name: Bump Version
8792
id: bump_version
8893
run: |
89-
export PREV_VERSION=$(git describe --abbrev=0 --tags)
94+
PREV_VERSION=$(git describe --abbrev=0 --tags)
9095
echo "Previous Version: $PREV_VERSION"
9196
92-
export RELEASE_TYPE=${{ steps.determine_release.outputs.release_type }}
97+
RELEASE_TYPE=${{ steps.determine_release.outputs.release_type }}
9398
echo "Release Type: $RELEASE_TYPE"
9499
95-
export VERSION_PARTS=($(echo $PREV_VERSION | tr '.' '\n'))
96-
MAJOR=${VERSION_PARTS[0]}
97-
MINOR=${VERSION_PARTS[1]}
98-
PATCH=${VERSION_PARTS[2]}
100+
# Strip the 'v' from the version if it exists
101+
PREV_VERSION=${PREV_VERSION#v}
102+
103+
IFS='.' read -r MAJOR MINOR PATCH <<< "$PREV_VERSION"
99104
100105
if [[ $RELEASE_TYPE == "major" ]]; then
101106
MAJOR=$((MAJOR + 1))
102107
MINOR=0
103108
PATCH=0
104109
elif [[ $RELEASE_TYPE == "minor" ]]; then
105-
MINOR=$((MINOR + 1))
106-
PATCH=0
110+
MINOR=$((MINOR + 1))
111+
PATCH=0
107112
else
108113
PATCH=$((PATCH + 1))
109114
fi
110115
111-
NEW_VERSION="$MAJOR.$MINOR.$PATCH"
116+
NEW_VERSION="v$MAJOR.$MINOR.$PATCH"
112117
echo "New Version: $NEW_VERSION"
113118
echo "::set-output name=new_version::$NEW_VERSION"
114-
119+
115120
# Get the milestone details
116121
- name: Get Milestone Details
117122
id: get_milestone
@@ -139,9 +144,18 @@ jobs:
139144
LABEL_DOCS="docs:"
140145
LABEL_REFACTOR="refactor:"
141146
LABEL_CHORE="chore:"
147+
LABEL_BUILD="build:"
148+
LABEL_CI="ci:"
149+
LABEL_PERFORMANCE="perf:"
150+
LABEL_STYLE="style:"
151+
LABEL_TEST="test:"
142152
LABEL_BREAKING_CHANGE="BREAKING CHANGE:"
153+
LABEL_FEAT_BREAKING="feat!:"
154+
LABEL_DEPS="chore(deps):"
155+
LABEL_DEPS_DEV="chore(deps-dev):"
143156
# Get the last release tag
144157
LAST_RELEASE_TAG=$(git describe --abbrev=0 --tags)
158+
echo "Last Release Tag: $LAST_RELEASE_TAG"
145159
# Get the milestone details from the output of the previous step
146160
MILESTONE_TITLE="${{ steps.get_milestone.outputs.milestone_title }}"
147161
MILESTONE_DESCRIPTION="${{ steps.get_milestone.outputs.milestone_description }}"
@@ -157,12 +171,14 @@ jobs:
157171
local section_label="$2"
158172
local section_icon="$3"
159173
# Get the commit messages with the specified label between the last release and the current release
160-
local commit_messages=$(git log --pretty=format:"- %s (Milestone: %b, Linked Issues: %C(yellow)%H%Creset)" "$LAST_RELEASE_TAG..HEAD" --grep="$section_label" --no-merges --decorate --decorate-refs=refs/issues)
174+
local commit_messages=$(git log --pretty=format:"- %s (Linked Issues: %C(yellow)%H%Creset)" "$LAST_RELEASE_TAG..HEAD" --grep="$section_label" --no-merges --decorate --decorate-refs=refs/issues)
161175
# If there are commit messages, append the section to the changelog file
162176
if [ -n "$commit_messages" ]; then
177+
# Remove duplicate commit messages
178+
local unique_commit_messages=$(echo "$commit_messages" | awk '!seen[$0]++')
163179
echo "### $section_icon $section_title" >> "$CHANGELOG_FILE"
164180
echo "" >> "$CHANGELOG_FILE"
165-
echo "$commit_messages" >> "$CHANGELOG_FILE"
181+
echo "$unique_commit_messages" >> "$CHANGELOG_FILE"
166182
echo "" >> "$CHANGELOG_FILE"
167183
fi
168184
}
@@ -173,7 +189,33 @@ jobs:
173189
append_section "Documentation" "$LABEL_DOCS" "📚"
174190
append_section "Refactorings" "$LABEL_REFACTOR" "🔨"
175191
append_section "Chores" "$LABEL_CHORE" "⚙️"
192+
append_section "Build" "$LABEL_BUILD" "🏗️"
193+
append_section "CI" "$LABEL_CI" "⚙️"
194+
append_section "Performance" "$LABEL_PERFORMANCE" "🚀"
195+
append_section "Style" "$LABEL_STYLE" "💅"
196+
append_section "Tests" "$LABEL_TEST" "🧪"
176197
append_section "Breaking Changes" "$LABEL_BREAKING_CHANGE" "💥"
198+
append_section "Feature Breaking Changes" "$LABEL_FEAT_BREAKING" "💥"
199+
append_section "Dependencies" "$LABEL_DEPS" "📦"
200+
append_section "Dev Dependencies" "$LABEL_DEPS_DEV" "🔧"
201+
202+
# Function to append non-labeled commits to the changelog file
203+
append_non_labeled_commits() {
204+
# Get the commit messages that do not match any conventional commit labels between the last release and the current release
205+
local non_labeled_commit_messages=$(git log --pretty=format:"- %s (Linked Issues: %C(yellow)%H%Creset)" "$LAST_RELEASE_TAG..HEAD" --invert-grep --grep="^fix:\|^feat:\|^enhancement:\|^docs:\|^refactor:\|^chore:\|^build:\|^ci:\|^perf:\|^style:\|^test:\|^BREAKING CHANGE:\|^feat!:\|^chore(deps):\|^chore(deps-dev):")
206+
# If there are non-labeled commit messages, append the section to the changelog file
207+
if [ -n "$non_labeled_commit_messages" ]; then
208+
# Remove duplicate commit messages
209+
local unique_commit_messages=$(echo "$non_labeled_commit_messages" | awk '!seen[$0]++')
210+
echo "### 📝 Other Changes" >> "$CHANGELOG_FILE"
211+
echo "" >> "$CHANGELOG_FILE"
212+
echo "$unique_commit_messages" >> "$CHANGELOG_FILE"
213+
echo "" >> "$CHANGELOG_FILE"
214+
fi
215+
}
216+
# Append non-labeled commits to the changelog file
217+
append_non_labeled_commits
218+
177219
echo "::set-output name=changelog_file::$CHANGELOG_FILE"
178220
179221
# Read changelog contents into a variable
@@ -201,7 +243,37 @@ jobs:
201243
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
202244
with:
203245
tag_name: ${{ steps.bump_version.outputs.new_version }}
204-
release_name: Release ${{ steps.bump_version.outputs.new_version }}
246+
release_name: ${{ steps.bump_version.outputs.new_version }}
205247
body_path: /home/runner/work/changelog.txt
206248
draft: false
207249
prerelease: false
250+
251+
# Update the CHANGELOG.md file in the repository
252+
- name: Update CHANGELOG.md
253+
run: |
254+
NEW_VERSION=${{ steps.bump_version.outputs.new_version }}
255+
CHANGELOG_CONTENTS=$(cat /home/runner/work/changelog.txt)
256+
# Prepend the new changelog content to the existing CHANGELOG.md below SPDX-License-Identifier section
257+
echo -e "$(head -n 2 CHANGELOG.md)\n\n## $NEW_VERSION\n\n$CHANGELOG_CONTENTS\n\n$(tail -n +3 CHANGELOG.md)" > CHANGELOG.md
258+
git config --global user.name "github-actions[bot]"
259+
git config --global user.email "github-actions[bot]@users.noreply.github.com"
260+
git add CHANGELOG.md
261+
git commit -m "chore: Update CHANGELOG.md for $NEW_VERSION"
262+
git push origin HEAD:main
263+
env:
264+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
265+
STARTUP_TYPE: 'nats'
266+
267+
# Update the VERSION file
268+
- name: Update VERSION file
269+
run: |
270+
NEW_VERSION=${{ steps.bump_version.outputs.new_version }}
271+
echo -e "# SPDX-License-Identifier: Apache-2.0\n\n$NEW_VERSION" > VERSION
272+
git config --global user.name "github-actions[bot]"
273+
git config --global user.email "github-actions[bot]@users.noreply.github.com"
274+
git add VERSION
275+
git commit -m "chore: Update VERSION to $NEW_VERSION"
276+
git push origin HEAD:main
277+
env:
278+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
279+
STARTUP_TYPE: 'nats'

0 commit comments

Comments
 (0)