Skip to content

Commit 44e033a

Browse files
authored
fix: correct workflow
1 parent 0f2a230 commit 44e033a

File tree

1 file changed

+107
-42
lines changed

1 file changed

+107
-42
lines changed

.github/workflows/release.yml

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

812
permissions:
913
contents: write
1014
actions: read
1115

1216
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+
1340
release:
41+
needs: check-commit
42+
if: needs.check-commit.outputs.should_release == 'true'
1443
runs-on: macos-latest
1544
steps:
1645
- name: Checkout code
1746
uses: actions/checkout@v4
1847
with:
19-
fetch-depth: 0
48+
fetch-depth: 0
2049

2150
- name: Setup Swift
2251
uses: swift-actions/setup-swift@v2
2352
with:
2453
swift-version: "6.1"
2554

26-
- name: Cache SwiftPM
27-
uses: actions/cache@v4
55+
- name: Cache SwiftPM dependencies
56+
uses: actions/cache@v3
2857
with:
2958
path: .build
3059
key: ${{ runner.os }}-swiftpm-${{ hashFiles('**/Package.resolved') }}
60+
restore-keys: |
61+
${{ runner.os }}-swiftpm-
62+
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
3174
32-
- name: Check commit and bump version
33-
id: version
75+
- name: Bump version and create release
3476
env:
3577
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3678
run: |
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
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"
4287
fi
88+
IFS='.' read -r MAJOR MINOR PATCH <<< "$CURRENT_VERSION"
4389
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"
90+
# Get last commit message
91+
COMMIT_MESSAGE=$(git log -1 --pretty=%B)
4692
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
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
52107
53108
NEW_VERSION="$MAJOR.$MINOR.$PATCH"
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")
57-
NOTES=$(git log ${LATEST_TAG:+${LATEST_TAG}..HEAD} --pretty=format:'%s' | sed 's/^/- /' | jq -R -s -c .)
109+
BRANCH_NAME=${{ github.ref_name }}
58110
59-
echo "tag=$TAG" >> $GITHUB_OUTPUT
60-
echo "title=$TITLE" >> $GITHUB_OUTPUT
61-
echo "prerelease=$PRERELEASE" >> $GITHUB_OUTPUT
62-
echo "notes=$NOTES" >> $GITHUB_OUTPUT
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
63121
64-
- name: Run tests
65-
if: steps.version.outputs.tag
66-
run: swift test
122+
echo "Bumping version from $CURRENT_VERSION to $RELEASE_TAG"
67123
68-
- name: Create release
69-
if: steps.version.outputs.tag
70-
env:
71-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
72-
run: |
73124
git config user.name "GitHub Actions"
74125
git config user.email "actions@github.com"
75-
git tag -a "${{ steps.version.outputs.tag }}" -m "${{ steps.version.outputs.tag }}"
76-
git push origin "${{ steps.version.outputs.tag }}"
77-
gh release create "${{ steps.version.outputs.tag }}" \
78-
--title "${{ steps.version.outputs.title }}" \
79-
--notes "${{ steps.version.outputs.notes }}" \
80-
--prerelease=${{ steps.version.outputs.prerelease }}
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

0 commit comments

Comments
 (0)