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