|
| 1 | +name: Release + Homebrew |
| 2 | +# Maintainer notes: |
| 3 | +# - Required secret in this repo: HOMEBREW_TAP_TOKEN |
| 4 | +# Token must be able to push to the tap repository. |
| 5 | +# - Optional repo variable: HOMEBREW_TAP_REPO (format: owner/repo) |
| 6 | +# - If HOMEBREW_TAP_REPO is unset, default tap is <repo-owner>/homebrew-capa. |
| 7 | + |
| 8 | +on: |
| 9 | + release: |
| 10 | + types: [published] |
| 11 | + workflow_dispatch: |
| 12 | + inputs: |
| 13 | + version: |
| 14 | + description: "Version without leading v (e.g. 0.2.0)" |
| 15 | + required: true |
| 16 | + |
| 17 | +permissions: |
| 18 | + contents: write |
| 19 | + |
| 20 | +jobs: |
| 21 | + publish: |
| 22 | + runs-on: macos-15 |
| 23 | + env: |
| 24 | + BIN_NAME: capa |
| 25 | + REPO_OWNER: ${{ github.repository_owner }} |
| 26 | + REPO_NAME: ${{ github.event.repository.name }} |
| 27 | + TAP_REPO: ${{ vars.HOMEBREW_TAP_REPO }} |
| 28 | + steps: |
| 29 | + - name: Checkout |
| 30 | + uses: actions/checkout@v4 |
| 31 | + |
| 32 | + - name: Resolve version |
| 33 | + id: version |
| 34 | + shell: bash |
| 35 | + run: | |
| 36 | + set -euo pipefail |
| 37 | + if [[ "${{ github.event_name }}" == "release" ]]; then |
| 38 | + tag="${{ github.event.release.tag_name }}" |
| 39 | + version="${tag#v}" |
| 40 | + else |
| 41 | + version="${{ inputs.version }}" |
| 42 | + tag="v${version}" |
| 43 | + fi |
| 44 | + echo "tag=$tag" >> "$GITHUB_OUTPUT" |
| 45 | + echo "version=$version" >> "$GITHUB_OUTPUT" |
| 46 | +
|
| 47 | + - name: Build release binary |
| 48 | + shell: bash |
| 49 | + run: swift build -c release |
| 50 | + |
| 51 | + - name: Validate CLI version matches release version |
| 52 | + shell: bash |
| 53 | + run: | |
| 54 | + set -euo pipefail |
| 55 | + expected="${{ steps.version.outputs.version }}" |
| 56 | + actual="$(.build/release/${BIN_NAME} --version | tr -d '[:space:]')" |
| 57 | + if [[ "$actual" != "$expected" ]]; then |
| 58 | + echo "Version mismatch: binary reports '$actual' but release expects '$expected'." >&2 |
| 59 | + exit 1 |
| 60 | + fi |
| 61 | +
|
| 62 | + - name: Package artifact |
| 63 | + id: package |
| 64 | + shell: bash |
| 65 | + run: | |
| 66 | + set -euo pipefail |
| 67 | + version="${{ steps.version.outputs.version }}" |
| 68 | + artifact="${BIN_NAME}-v${version}-macos-arm64.tar.gz" |
| 69 | + mkdir -p dist |
| 70 | + cp ".build/release/${BIN_NAME}" "dist/${BIN_NAME}" |
| 71 | + tar -C dist -czf "dist/${artifact}" "${BIN_NAME}" |
| 72 | + sha=$(shasum -a 256 "dist/${artifact}" | awk '{print $1}') |
| 73 | + echo "artifact=$artifact" >> "$GITHUB_OUTPUT" |
| 74 | + echo "sha256=$sha" >> "$GITHUB_OUTPUT" |
| 75 | +
|
| 76 | + - name: Upload release asset |
| 77 | + uses: softprops/action-gh-release@v2 |
| 78 | + with: |
| 79 | + tag_name: ${{ steps.version.outputs.tag }} |
| 80 | + files: dist/${{ steps.package.outputs.artifact }} |
| 81 | + fail_on_unmatched_files: true |
| 82 | + generate_release_notes: false |
| 83 | + |
| 84 | + - name: Select tap repo |
| 85 | + id: tap |
| 86 | + shell: bash |
| 87 | + run: | |
| 88 | + set -euo pipefail |
| 89 | + # Allow overriding the tap repo via Actions variable. |
| 90 | + tap_repo="${TAP_REPO}" |
| 91 | + if [[ -z "$tap_repo" ]]; then |
| 92 | + # Default tap naming convention for this project. |
| 93 | + tap_repo="${REPO_OWNER}/homebrew-capa" |
| 94 | + fi |
| 95 | + echo "repo=$tap_repo" >> "$GITHUB_OUTPUT" |
| 96 | +
|
| 97 | + - name: Update Homebrew tap formula |
| 98 | + shell: bash |
| 99 | + env: |
| 100 | + # PAT used to clone/push the tap repository. |
| 101 | + GH_TOKEN: ${{ secrets.HOMEBREW_TAP_TOKEN }} |
| 102 | + run: | |
| 103 | + set -euo pipefail |
| 104 | + if [[ -z "${GH_TOKEN}" ]]; then |
| 105 | + echo "HOMEBREW_TAP_TOKEN is not set; skipping tap update." >&2 |
| 106 | + exit 1 |
| 107 | + fi |
| 108 | +
|
| 109 | + tag="${{ steps.version.outputs.tag }}" |
| 110 | + version="${{ steps.version.outputs.version }}" |
| 111 | + artifact="${{ steps.package.outputs.artifact }}" |
| 112 | + sha="${{ steps.package.outputs.sha256 }}" |
| 113 | + tap_repo="${{ steps.tap.outputs.repo }}" |
| 114 | + asset_url="https://github.com/${REPO_OWNER}/${REPO_NAME}/releases/download/${tag}/${artifact}" |
| 115 | +
|
| 116 | + temp_dir="$(mktemp -d)" |
| 117 | + trap 'rm -rf "$temp_dir"' EXIT |
| 118 | +
|
| 119 | + git clone "https://x-access-token:${GH_TOKEN}@github.com/${tap_repo}.git" "$temp_dir/tap" |
| 120 | + mkdir -p "$temp_dir/tap/Formula" |
| 121 | +
|
| 122 | + scripts/generate_homebrew_formula.sh "$version" "$asset_url" "$sha" > "$temp_dir/tap/Formula/capa.rb" |
| 123 | +
|
| 124 | + pushd "$temp_dir/tap" > /dev/null |
| 125 | + git config user.name "github-actions[bot]" |
| 126 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 127 | + git add Formula/capa.rb |
| 128 | + if git diff --cached --quiet; then |
| 129 | + echo "No formula changes to commit." |
| 130 | + exit 0 |
| 131 | + fi |
| 132 | + git commit -m "capa ${version}" |
| 133 | + git push origin HEAD |
| 134 | + popd > /dev/null |
0 commit comments