|
| 1 | +name: Update Homebrew Formula on Release |
| 2 | + |
| 3 | +# Triggers when a new version tag is pushed |
| 4 | +on: |
| 5 | + push: |
| 6 | + tags: |
| 7 | + - 'v*' |
| 8 | + pull_request: |
| 9 | + branches: [master] |
| 10 | + paths: |
| 11 | + - '.github/workflows/homebrew-release.yml' |
| 12 | + workflow_dispatch: |
| 13 | + inputs: |
| 14 | + version: |
| 15 | + description: 'Version to release (without v prefix, e.g., 5.2.0)' |
| 16 | + required: true |
| 17 | + type: string |
| 18 | + dry_run: |
| 19 | + description: 'Dry run (skip push to homebrew-mfc)' |
| 20 | + required: false |
| 21 | + type: boolean |
| 22 | + default: false |
| 23 | + |
| 24 | +jobs: |
| 25 | + update-homebrew-tap: |
| 26 | + name: Update homebrew-mfc tap |
| 27 | + runs-on: ubuntu-latest |
| 28 | + |
| 29 | + steps: |
| 30 | + - name: Determine version |
| 31 | + id: version |
| 32 | + run: | |
| 33 | + if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then |
| 34 | + VERSION="${{ inputs.version }}" |
| 35 | + elif [[ "${{ github.event_name }}" == "pull_request" ]]; then |
| 36 | + # Use existing version for PR testing |
| 37 | + VERSION="5.2.0" |
| 38 | + echo "::notice::PR test mode - using version $VERSION" |
| 39 | + else |
| 40 | + # Extract version from tag (remove 'v' prefix) |
| 41 | + VERSION="${GITHUB_REF#refs/tags/v}" |
| 42 | + fi |
| 43 | +
|
| 44 | + if [[ ! "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then |
| 45 | + echo "::error::Invalid version format: $VERSION (expected X.Y.Z)" |
| 46 | + exit 1 |
| 47 | + fi |
| 48 | +
|
| 49 | + echo "version=$VERSION" >> "$GITHUB_OUTPUT" |
| 50 | + echo "Version: $VERSION" |
| 51 | +
|
| 52 | + - name: Compute SHA256 of release tarball |
| 53 | + id: sha256 |
| 54 | + run: | |
| 55 | + VERSION="${{ steps.version.outputs.version }}" |
| 56 | + URL="https://github.com/MFlowCode/MFC/archive/refs/tags/v${VERSION}.tar.gz" |
| 57 | +
|
| 58 | + echo "Downloading tarball from: $URL" |
| 59 | +
|
| 60 | + # Verify URL is reachable |
| 61 | + HTTP_CODE=$(curl -sI -w "%{http_code}" -o /dev/null "$URL") |
| 62 | + if [[ "$HTTP_CODE" != "200" && "$HTTP_CODE" != "302" ]]; then |
| 63 | + echo "::error::Release tarball not found at $URL (HTTP $HTTP_CODE)" |
| 64 | + echo "::error::Make sure the tag v${VERSION} exists and the release is published" |
| 65 | + exit 1 |
| 66 | + fi |
| 67 | +
|
| 68 | + # Compute SHA256 |
| 69 | + SHA256=$(curl -sL "$URL" | sha256sum | awk '{print $1}') |
| 70 | +
|
| 71 | + if [[ -z "$SHA256" || "$SHA256" == "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" ]]; then |
| 72 | + echo "::error::Failed to compute SHA256 (empty file or download failed)" |
| 73 | + exit 1 |
| 74 | + fi |
| 75 | +
|
| 76 | + echo "sha256=$SHA256" >> "$GITHUB_OUTPUT" |
| 77 | + echo "SHA256: $SHA256" |
| 78 | +
|
| 79 | + - name: PR test summary |
| 80 | + if: ${{ github.event_name == 'pull_request' }} |
| 81 | + run: | |
| 82 | + echo "## PR Test Mode" >> $GITHUB_STEP_SUMMARY |
| 83 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 84 | + echo "Validated:" >> $GITHUB_STEP_SUMMARY |
| 85 | + echo "- Version parsing: v${{ steps.version.outputs.version }}" >> $GITHUB_STEP_SUMMARY |
| 86 | + echo "- SHA256 computation: \`${{ steps.sha256.outputs.sha256 }}\`" >> $GITHUB_STEP_SUMMARY |
| 87 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 88 | + echo "Skipped (secrets not available for fork PRs):" >> $GITHUB_STEP_SUMMARY |
| 89 | + echo "- Checkout homebrew-mfc" >> $GITHUB_STEP_SUMMARY |
| 90 | + echo "- Update formula" >> $GITHUB_STEP_SUMMARY |
| 91 | + echo "- Push to tap" >> $GITHUB_STEP_SUMMARY |
| 92 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 93 | + echo "The full workflow will run when a \`v*\` tag is pushed after merge." >> $GITHUB_STEP_SUMMARY |
| 94 | +
|
| 95 | + - name: Checkout homebrew-mfc tap |
| 96 | + if: ${{ github.event_name != 'pull_request' }} |
| 97 | + uses: actions/checkout@v4 |
| 98 | + with: |
| 99 | + repository: MFlowCode/homebrew-mfc |
| 100 | + token: ${{ secrets.TAP_REPO_TOKEN }} |
| 101 | + path: homebrew-mfc |
| 102 | + |
| 103 | + - name: Update formula |
| 104 | + if: ${{ github.event_name != 'pull_request' }} |
| 105 | + run: | |
| 106 | + VERSION="${{ steps.version.outputs.version }}" |
| 107 | + SHA256="${{ steps.sha256.outputs.sha256 }}" |
| 108 | + FORMULA="homebrew-mfc/Formula/mfc.rb" |
| 109 | +
|
| 110 | + echo "Updating formula to v${VERSION}..." |
| 111 | +
|
| 112 | + # Update URL |
| 113 | + sed -i "s|url \"https://github.com/MFlowCode/MFC/archive/refs/tags/v[^\"]*\.tar\.gz\"|url \"https://github.com/MFlowCode/MFC/archive/refs/tags/v${VERSION}.tar.gz\"|" "$FORMULA" |
| 114 | +
|
| 115 | + # Update SHA256 (the one right after url, not bottle SHAs) |
| 116 | + # This uses awk to only update the first sha256 after the url line |
| 117 | + awk -v newsha="$SHA256" ' |
| 118 | + /^ url "https:\/\/github.com\/MFlowCode\/MFC/ { found_url=1 } |
| 119 | + found_url && /^ sha256 "/ && !updated { |
| 120 | + sub(/sha256 "[^"]*"/, "sha256 \"" newsha "\"") |
| 121 | + updated=1 |
| 122 | + } |
| 123 | + { print } |
| 124 | + ' "$FORMULA" > "$FORMULA.tmp" && mv "$FORMULA.tmp" "$FORMULA" |
| 125 | +
|
| 126 | + # Remove existing bottle block (new bottles will be built by bottle.yml) |
| 127 | + # This removes everything between "bottle do" and the matching "end" |
| 128 | + awk ' |
| 129 | + /^ bottle do/ { in_bottle=1; next } |
| 130 | + in_bottle && /^ end/ { in_bottle=0; next } |
| 131 | + !in_bottle { print } |
| 132 | + ' "$FORMULA" > "$FORMULA.tmp" && mv "$FORMULA.tmp" "$FORMULA" |
| 133 | +
|
| 134 | + echo "Updated formula:" |
| 135 | + head -30 "$FORMULA" |
| 136 | +
|
| 137 | + - name: Validate updated formula |
| 138 | + if: ${{ github.event_name != 'pull_request' }} |
| 139 | + run: | |
| 140 | + cd homebrew-mfc |
| 141 | + echo "Checking Ruby syntax..." |
| 142 | + ruby -c Formula/mfc.rb |
| 143 | +
|
| 144 | + echo "Verifying URL and SHA256 were updated..." |
| 145 | + grep -q "v${{ steps.version.outputs.version }}.tar.gz" Formula/mfc.rb || (echo "::error::URL not updated"; exit 1) |
| 146 | + grep -q "${{ steps.sha256.outputs.sha256 }}" Formula/mfc.rb || (echo "::error::SHA256 not updated"; exit 1) |
| 147 | +
|
| 148 | + echo "Formula validation passed!" |
| 149 | +
|
| 150 | + - name: Commit and push to homebrew-mfc |
| 151 | + if: ${{ github.event_name != 'pull_request' && github.event.inputs.dry_run != 'true' }} |
| 152 | + run: | |
| 153 | + cd homebrew-mfc |
| 154 | + VERSION="${{ steps.version.outputs.version }}" |
| 155 | +
|
| 156 | + git config user.name "github-actions[bot]" |
| 157 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 158 | +
|
| 159 | + git add Formula/mfc.rb |
| 160 | + git commit -m "Update MFC to v${VERSION}" |
| 161 | +
|
| 162 | + echo "Pushing to homebrew-mfc..." |
| 163 | + git push origin main |
| 164 | +
|
| 165 | + echo "Successfully pushed formula update!" |
| 166 | + echo "The bottle.yml workflow in homebrew-mfc will now build bottles automatically." |
| 167 | +
|
| 168 | + - name: Dry run summary |
| 169 | + if: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.dry_run == 'true' }} |
| 170 | + run: | |
| 171 | + echo "::notice::DRY RUN - skipped push to homebrew-mfc" |
| 172 | + echo "" |
| 173 | + echo "Would have committed the following changes:" |
| 174 | + cd homebrew-mfc |
| 175 | + git diff Formula/mfc.rb |
| 176 | +
|
| 177 | + - name: Summary |
| 178 | + if: ${{ github.event_name != 'pull_request' }} |
| 179 | + run: | |
| 180 | + VERSION="${{ steps.version.outputs.version }}" |
| 181 | + echo "## Homebrew Formula Updated" >> $GITHUB_STEP_SUMMARY |
| 182 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 183 | + echo "- **Version:** v${VERSION}" >> $GITHUB_STEP_SUMMARY |
| 184 | + echo "- **SHA256:** \`${{ steps.sha256.outputs.sha256 }}\`" >> $GITHUB_STEP_SUMMARY |
| 185 | + echo "- **Tap:** [MFlowCode/homebrew-mfc](https://github.com/MFlowCode/homebrew-mfc)" >> $GITHUB_STEP_SUMMARY |
| 186 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 187 | + echo "The [bottle.yml](https://github.com/MFlowCode/homebrew-mfc/actions/workflows/bottle.yml) workflow will now build bottles for this release." >> $GITHUB_STEP_SUMMARY |
0 commit comments