Skip to content

Commit 7e9e717

Browse files
authored
fix: update workflow
1 parent 313d4a8 commit 7e9e717

File tree

1 file changed

+41
-107
lines changed

1 file changed

+41
-107
lines changed

.github/workflows/release.yml

Lines changed: 41 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,145 +1,79 @@
11
name: Automated Release
22
on:
3-
pull_request:
4-
branches:
5-
- main
6-
- next
73
push:
8-
branches:
9-
- main
10-
- next
4+
branches: [main, next]
5+
pull_request:
6+
branches: [main, next]
117

128
permissions:
139
contents: write
1410
actions: read
1511

1612
jobs:
17-
check-commit:
18-
runs-on: macos-latest
19-
outputs:
20-
should_release: ${{ steps.check.outputs.should_release }}
21-
steps:
22-
- name: Checkout code
23-
uses: actions/checkout@v4
24-
with:
25-
fetch-depth: 1
26-
27-
- name: Check commit message and branch
28-
id: check
29-
run: |
30-
COMMIT_MESSAGE=$(git log -1 --pretty=%B)
31-
BRANCH_NAME=${{ github.ref_name }}
32-
33-
if [[ $COMMIT_MESSAGE =~ ^feat!.* ]] || [[ $COMMIT_MESSAGE =~ ^feat:.* ]] || [[ $COMMIT_MESSAGE =~ ^fix:.* ]]; then
34-
echo "should_release=true" >> $GITHUB_OUTPUT
35-
else
36-
echo "should_release=false" >> $GITHUB_OUTPUT
37-
echo "No version bump needed (commit: $COMMIT_MESSAGE)"
38-
fi /
39-
4013
release:
41-
needs: check-commit
42-
if: needs.check-commit.outputs.should_release == 'true'
4314
runs-on: macos-latest
4415
steps:
4516
- name: Checkout code
4617
uses: actions/checkout@v4
4718
with:
48-
fetch-depth: 0
19+
fetch-depth: 0
4920

5021
- name: Setup Swift
5122
uses: swift-actions/setup-swift@v2
5223
with:
5324
swift-version: "6.1"
5425

55-
- name: Cache SwiftPM dependencies
56-
uses: actions/cache@v3
26+
- name: Cache SwiftPM
27+
uses: actions/cache@v4
5728
with:
5829
path: .build
5930
key: ${{ runner.os }}-swiftpm-${{ hashFiles('**/Package.resolved') }}
60-
restore-keys: |
61-
${{ runner.os }}-swiftpm-
6231

63-
- name: Resolve dependencies
64-
run: swift package resolve
65-
66-
- name: Run tests
67-
run: swift test
68-
continue-on-error: false
69-
70-
- name: Setup GitHub CLI
71-
run: |
72-
brew install gh
73-
echo "${{ secrets.GITHUB_TOKEN }}" | gh auth login --with-token
74-
75-
- name: Bump version and create release
32+
- name: Check commit and bump version
33+
id: version
7634
env:
7735
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
7836
run: |
79-
set -e
80-
81-
# Get the latest version tag (default to 0.0.0 if none)
82-
LATEST_TAG=$(git tag -l '[0-9]*.[0-9]*.[0-9]*' --sort=-v:refname | head -n 1)
83-
if [ -z "$LATEST_TAG" ]; then
84-
CURRENT_VERSION="0.0.0"
85-
else
86-
CURRENT_VERSION="$LATEST_TAG"
37+
COMMIT_MSG=$(git log -1 --pretty=%B)
38+
BRANCH=${{ github.ref_name }}
39+
if ! [[ $COMMIT_MSG =~ ^(feat!|feat|fix): ]]; then
40+
echo "No release needed"
41+
exit 0
8742
fi
88-
IFS='.' read -r MAJOR MINOR PATCH <<< "$CURRENT_VERSION"
8943
90-
# Get last commit message
91-
COMMIT_MESSAGE=$(git log -1 --pretty=%B)
44+
LATEST_TAG=$(git tag -l '[0-9]*.[0-9]*.[0-9]*' --sort=-v:refname | head -n 1 || echo "0.0.0")
45+
IFS='.' read -r MAJOR MINOR PATCH <<< "$LATEST_TAG"
9246
93-
# Determine version bump
94-
if [[ $COMMIT_MESSAGE =~ ^feat!.* ]]; then
95-
MAJOR=$((MAJOR + 1))
96-
MINOR=0
97-
PATCH=0
98-
elif [[ $COMMIT_MESSAGE =~ ^feat:.* ]]; then
99-
MINOR=$((MINOR + 1))
100-
PATCH=0
101-
elif [[ $COMMIT_MESSAGE =~ ^fix:.* ]]; then
102-
PATCH=$((PATCH + 1))
103-
else
104-
echo "No version bump needed (commit: $COMMIT_MESSAGE)"
105-
exit 0
106-
fi
47+
case $COMMIT_MSG in
48+
feat!*) MAJOR=$((MAJOR + 1)); MINOR=0; PATCH=0 ;;
49+
feat:*) MINOR=$((MINOR + 1)); PATCH=0 ;;
50+
fix:*) PATCH=$((PATCH + 1)) ;;
51+
esac
10752
10853
NEW_VERSION="$MAJOR.$MINOR.$PATCH"
109-
BRANCH_NAME=${{ github.ref_name }}
54+
TAG=$([ "$BRANCH" = "main" ] && echo "$NEW_VERSION" || echo "next-$NEW_VERSION")
55+
TITLE=$([ "$BRANCH" = "main" ] && echo "Release $NEW_VERSION" || echo "Pre-release $TAG")
56+
PRERELEASE=$([ "$BRANCH" = "main" ] && echo "false" || echo "true")
11057
111-
# Determine tag and release type based on branch
112-
if [ "$BRANCH_NAME" = "main" ]; then
113-
RELEASE_TAG="$NEW_VERSION"
114-
RELEASE_TITLE="Release $NEW_VERSION"
115-
IS_PRERELEASE=false
116-
else
117-
RELEASE_TAG="next-$NEW_VERSION"
118-
RELEASE_TITLE="Pre-release next-$NEW_VERSION"
119-
IS_PRERELEASE=true
120-
fi
58+
echo "tag=$TAG" >> $GITHUB_OUTPUT
59+
echo "title=$TITLE" >> $GITHUB_OUTPUT
60+
echo "prerelease=$PRERELEASE" >> $GITHUB_OUTPUT
61+
echo "notes=$(git log ${LATEST_TAG:+${LATEST_TAG}..HEAD} --pretty=format:'- %s')" >> $GITHUB_OUTPUT
12162
122-
echo "Bumping version from $CURRENT_VERSION to $RELEASE_TAG"
63+
- name: Run tests
64+
if: steps.version.outputs.tag
65+
run: swift test
12366

67+
- name: Create release
68+
if: steps.version.outputs.tag
69+
env:
70+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
71+
run: |
12472
git config user.name "GitHub Actions"
12573
git config user.email "actions@github.com"
126-
git remote set-url origin https://x-access-token:${GITHUB_TOKEN}@github.com/${{ github.repository }}.git
127-
git push origin "$BRANCH_NAME"
128-
129-
git tag -a "$RELEASE_TAG" -m "$RELEASE_TAG"
130-
git push origin "$RELEASE_TAG"
131-
132-
# Generate release notes from commits since last tag
133-
if [ -z "$LATEST_TAG" ]; then
134-
RELEASE_NOTES=$(git log --pretty=format:"- %s")
135-
else
136-
RELEASE_NOTES=$(git log "$LATEST_TAG"..HEAD --pretty=format:"- %s")
137-
fi
138-
139-
echo "Release notes:"
140-
echo "$RELEASE_NOTES"
141-
142-
gh release create "$RELEASE_TAG" \
143-
--title "$RELEASE_TITLE" \
144-
--notes "$RELEASE_NOTES" \
145-
--prerelease=$IS_PRERELEASE
74+
git tag -a "${{ steps.version.outputs.tag }}" -m "${{ steps.version.outputs.tag }}"
75+
git push origin "${{ steps.version.outputs.tag }}"
76+
gh release create "${{ steps.version.outputs.tag }}" \
77+
--title "${{ steps.version.outputs.title }}" \
78+
--notes "${{ steps.version.outputs.notes }}" \
79+
--prerelease=${{ steps.version.outputs.prerelease }}

0 commit comments

Comments
 (0)