|
| 1 | +name: Release |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + tags: |
| 6 | + - 'v*' |
| 7 | + |
| 8 | +jobs: |
| 9 | + release: |
| 10 | + name: Create Release and Update Homebrew |
| 11 | + runs-on: macos-14 |
| 12 | + permissions: |
| 13 | + contents: write |
| 14 | + |
| 15 | + steps: |
| 16 | + - name: Checkout repository |
| 17 | + uses: actions/checkout@v4 |
| 18 | + with: |
| 19 | + fetch-depth: 0 |
| 20 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 21 | + |
| 22 | + - name: Setup Swift |
| 23 | + uses: swift-actions/setup-swift@v1 |
| 24 | + with: |
| 25 | + swift-version: "5.9" |
| 26 | + |
| 27 | + - name: Get version from tag |
| 28 | + id: get_version |
| 29 | + run: | |
| 30 | + VERSION=${GITHUB_REF#refs/tags/} |
| 31 | + VERSION=${VERSION#v} |
| 32 | + echo "version=$VERSION" >> $GITHUB_OUTPUT |
| 33 | + echo "tag=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT |
| 34 | + |
| 35 | + - name: Build project |
| 36 | + run: swift build -c release --disable-sandbox |
| 37 | + |
| 38 | + - name: Get previous tag |
| 39 | + id: previous_tag |
| 40 | + run: | |
| 41 | + PREVIOUS_TAG=$(git describe --tags --abbrev=0 HEAD^ 2>/dev/null || echo "") |
| 42 | + if [ -z "$PREVIOUS_TAG" ]; then |
| 43 | + PREVIOUS_TAG=$(git rev-list --max-parents=0 HEAD 2>/dev/null || echo "") |
| 44 | + fi |
| 45 | + echo "previous_tag=$PREVIOUS_TAG" >> $GITHUB_OUTPUT |
| 46 | + |
| 47 | + - name: Generate release notes |
| 48 | + id: release_notes |
| 49 | + run: | |
| 50 | + TAG=${{ steps.get_version.outputs.tag }} |
| 51 | + PREVIOUS_TAG=${{ steps.previous_tag.outputs.previous_tag }} |
| 52 | + |
| 53 | + if [ -n "$PREVIOUS_TAG" ] && [ "$PREVIOUS_TAG" != "" ]; then |
| 54 | + NOTES=$(cat <<NOTES_EOF |
| 55 | + ## Changes in ${TAG} |
| 56 | + |
| 57 | + See the [full changelog](https://github.com/${{ github.repository }}/compare/${PREVIOUS_TAG}...${TAG}) for details. |
| 58 | + NOTES_EOF |
| 59 | + ) |
| 60 | + else |
| 61 | + NOTES=$(cat <<NOTES_EOF |
| 62 | + ## Changes in ${TAG} |
| 63 | + |
| 64 | + Initial release. |
| 65 | + NOTES_EOF |
| 66 | + ) |
| 67 | + fi |
| 68 | + |
| 69 | + INSTALL_SECTION=$(cat <<INSTALL_EOF |
| 70 | + |
| 71 | + ### Installation |
| 72 | + |
| 73 | + \`\`\`bash |
| 74 | + brew tap manucodin/swift-mock-generator |
| 75 | + brew install swift-mock-generator |
| 76 | + \`\`\` |
| 77 | + INSTALL_EOF |
| 78 | + ) |
| 79 | + |
| 80 | + NOTES="${NOTES}${INSTALL_SECTION}" |
| 81 | + |
| 82 | + echo "notes<<EOF" >> $GITHUB_OUTPUT |
| 83 | + echo "$NOTES" >> $GITHUB_OUTPUT |
| 84 | + echo "EOF" >> $GITHUB_OUTPUT |
| 85 | + |
| 86 | + - name: Create GitHub Release |
| 87 | + uses: softprops/action-gh-release@v1 |
| 88 | + with: |
| 89 | + name: ${{ steps.get_version.outputs.tag }} |
| 90 | + body: ${{ steps.release_notes.outputs.notes }} |
| 91 | + draft: false |
| 92 | + prerelease: false |
| 93 | + |
| 94 | + - name: Wait for GitHub to generate tarball |
| 95 | + run: sleep 10 |
| 96 | + |
| 97 | + - name: Calculate SHA256 |
| 98 | + id: sha256 |
| 99 | + run: | |
| 100 | + TAG=${{ steps.get_version.outputs.tag }} |
| 101 | + URL="https://github.com/${{ github.repository }}/archive/refs/tags/${TAG}.tar.gz" |
| 102 | + |
| 103 | + MAX_RETRIES=5 |
| 104 | + RETRY_COUNT=0 |
| 105 | + SHA256="" |
| 106 | + |
| 107 | + while [ $RETRY_COUNT -lt $MAX_RETRIES ]; do |
| 108 | + SHA256=$(curl -sL "$URL" | shasum -a 256 | awk '{print $1}') |
| 109 | + if [ -n "$SHA256" ] && [ ${#SHA256} -eq 64 ]; then |
| 110 | + break |
| 111 | + fi |
| 112 | + RETRY_COUNT=$((RETRY_COUNT + 1)) |
| 113 | + sleep 5 |
| 114 | + done |
| 115 | + |
| 116 | + if [ -z "$SHA256" ] || [ ${#SHA256} -ne 64 ]; then |
| 117 | + echo "Error: Failed to calculate SHA256" |
| 118 | + exit 1 |
| 119 | + fi |
| 120 | + |
| 121 | + echo "sha256=$SHA256" >> $GITHUB_OUTPUT |
| 122 | + |
| 123 | + - name: Update Homebrew formula |
| 124 | + run: | |
| 125 | + TAG=${{ steps.get_version.outputs.tag }} |
| 126 | + SHA256=${{ steps.sha256.outputs.sha256 }} |
| 127 | + FORMULA_FILE="homebrew-tap/swift-mock-generator.rb" |
| 128 | + |
| 129 | + if [ ! -f "$FORMULA_FILE" ]; then |
| 130 | + echo "Error: Formula file not found at $FORMULA_FILE" |
| 131 | + exit 1 |
| 132 | + fi |
| 133 | + |
| 134 | + sed -i '' "s/VERSION_PLACEHOLDER/${TAG}/g" "$FORMULA_FILE" |
| 135 | + sed -i '' "s/SHA256_PLACEHOLDER/${SHA256}/g" "$FORMULA_FILE" |
| 136 | + |
| 137 | + - name: Create branch and push changes |
| 138 | + run: | |
| 139 | + TAG=${{ steps.get_version.outputs.tag }} |
| 140 | + BRANCH_NAME="update-homebrew-formula-${TAG}" |
| 141 | + |
| 142 | + # Checkout the main branch (tags are in detached HEAD state) |
| 143 | + git checkout main || git checkout master |
| 144 | + git pull |
| 145 | + |
| 146 | + # Check if there are changes |
| 147 | + if git diff --quiet homebrew-tap/swift-mock-generator.rb; then |
| 148 | + echo "No changes to commit" |
| 149 | + exit 0 |
| 150 | + fi |
| 151 | + |
| 152 | + # Create branch, commit and push |
| 153 | + git checkout -b "$BRANCH_NAME" |
| 154 | + |
| 155 | + git config user.name "github-actions[bot]" |
| 156 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 157 | + |
| 158 | + git add homebrew-tap/swift-mock-generator.rb |
| 159 | + git commit -m "Update Homebrew formula to ${TAG}" |
| 160 | + git push -u origin "$BRANCH_NAME" |
| 161 | + |
| 162 | + echo "✅ Branch '$BRANCH_NAME' created and pushed successfully" |
| 163 | + echo "You can now create a PR from this branch to main" |
0 commit comments