|
1 | 1 | name: Automated Release
|
2 | 2 | on:
|
3 |
| - pull_request: |
4 |
| - branches: |
5 |
| - - main |
6 |
| - - next |
7 | 3 | push:
|
8 |
| - branches: |
9 |
| - - main |
10 |
| - - next |
| 4 | + branches: [main, next] |
| 5 | + pull_request: |
| 6 | + branches: [main, next] |
11 | 7 |
|
12 | 8 | permissions:
|
13 | 9 | contents: write
|
14 | 10 | actions: read
|
15 | 11 |
|
16 | 12 | 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 |
| -
|
40 | 13 | release:
|
41 |
| - needs: check-commit |
42 |
| - if: needs.check-commit.outputs.should_release == 'true' |
43 | 14 | runs-on: macos-latest
|
44 | 15 | steps:
|
45 | 16 | - name: Checkout code
|
46 | 17 | uses: actions/checkout@v4
|
47 | 18 | with:
|
48 |
| - fetch-depth: 0 |
| 19 | + fetch-depth: 0 |
49 | 20 |
|
50 | 21 | - name: Setup Swift
|
51 | 22 | uses: swift-actions/setup-swift@v2
|
52 | 23 | with:
|
53 | 24 | swift-version: "6.1"
|
54 | 25 |
|
55 |
| - - name: Cache SwiftPM dependencies |
56 |
| - uses: actions/cache@v3 |
| 26 | + - name: Cache SwiftPM |
| 27 | + uses: actions/cache@v4 |
57 | 28 | with:
|
58 | 29 | path: .build
|
59 | 30 | key: ${{ runner.os }}-swiftpm-${{ hashFiles('**/Package.resolved') }}
|
60 |
| - restore-keys: | |
61 |
| - ${{ runner.os }}-swiftpm- |
62 | 31 |
|
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 |
76 | 34 | env:
|
77 | 35 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
78 | 36 | 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 |
87 | 42 | fi
|
88 |
| - IFS='.' read -r MAJOR MINOR PATCH <<< "$CURRENT_VERSION" |
89 | 43 |
|
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" |
92 | 46 |
|
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 |
107 | 52 |
|
108 | 53 | 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") |
110 | 57 |
|
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 |
121 | 62 |
|
122 |
| - echo "Bumping version from $CURRENT_VERSION to $RELEASE_TAG" |
| 63 | + - name: Run tests |
| 64 | + if: steps.version.outputs.tag |
| 65 | + run: swift test |
123 | 66 |
|
| 67 | + - name: Create release |
| 68 | + if: steps.version.outputs.tag |
| 69 | + env: |
| 70 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 71 | + run: | |
124 | 72 | git config user.name "GitHub Actions"
|
125 | 73 | 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