|
| 1 | +name: Release And Homebrew |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + tags: |
| 6 | + - 'v*' |
| 7 | + workflow_dispatch: |
| 8 | + |
| 9 | +permissions: |
| 10 | + contents: write |
| 11 | + |
| 12 | +jobs: |
| 13 | + release: |
| 14 | + runs-on: ubuntu-latest |
| 15 | + steps: |
| 16 | + - name: Checkout |
| 17 | + uses: actions/checkout@v4 |
| 18 | + with: |
| 19 | + fetch-depth: 0 |
| 20 | + |
| 21 | + - name: Resolve release metadata |
| 22 | + id: meta |
| 23 | + shell: bash |
| 24 | + run: | |
| 25 | + VERSION="${GITHUB_REF_NAME}" |
| 26 | + NOTES_FILE="docs/releases/${VERSION}.md" |
| 27 | + if [ ! -f "${NOTES_FILE}" ]; then |
| 28 | + NOTES_FILE="$(mktemp)" |
| 29 | + { |
| 30 | + echo "# ${GITHUB_REPOSITORY##*/} ${VERSION}" |
| 31 | + echo |
| 32 | + echo "Automated release generated from tag ${VERSION}." |
| 33 | + } > "${NOTES_FILE}" |
| 34 | + fi |
| 35 | + echo "version=${VERSION}" >> "$GITHUB_OUTPUT" |
| 36 | + echo "notes_file=${NOTES_FILE}" >> "$GITHUB_OUTPUT" |
| 37 | +
|
| 38 | + - name: Create or update release |
| 39 | + env: |
| 40 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 41 | + shell: bash |
| 42 | + run: | |
| 43 | + VERSION="${{ steps.meta.outputs.version }}" |
| 44 | + NOTES_FILE="${{ steps.meta.outputs.notes_file }}" |
| 45 | + ASSET_PATH="docs/images/overview.png" |
| 46 | +
|
| 47 | + if gh release view "${VERSION}" >/dev/null 2>&1; then |
| 48 | + gh release edit "${VERSION}" --title "${GITHUB_REPOSITORY##*/} ${VERSION}" --notes-file "${NOTES_FILE}" |
| 49 | + else |
| 50 | + if [ -f "${ASSET_PATH}" ]; then |
| 51 | + gh release create "${VERSION}" "${ASSET_PATH}#overview.png" --title "${GITHUB_REPOSITORY##*/} ${VERSION}" --notes-file "${NOTES_FILE}" |
| 52 | + else |
| 53 | + gh release create "${VERSION}" --title "${GITHUB_REPOSITORY##*/} ${VERSION}" --notes-file "${NOTES_FILE}" |
| 54 | + fi |
| 55 | + fi |
| 56 | +
|
| 57 | + update-homebrew: |
| 58 | + needs: release |
| 59 | + runs-on: ubuntu-latest |
| 60 | + if: ${{ secrets.HOMEBREW_TAP_TOKEN != '' }} |
| 61 | + steps: |
| 62 | + - name: Checkout source repo |
| 63 | + uses: actions/checkout@v4 |
| 64 | + |
| 65 | + - name: Compute tarball SHA256 |
| 66 | + id: sha |
| 67 | + shell: bash |
| 68 | + run: | |
| 69 | + VERSION="${GITHUB_REF_NAME}" |
| 70 | + curl -L -s "https://github.com/${GITHUB_REPOSITORY}/archive/refs/tags/${VERSION}.tar.gz" -o "/tmp/${VERSION}.tar.gz" |
| 71 | + SHA256="$(sha256sum "/tmp/${VERSION}.tar.gz" | awk '{print $1}')" |
| 72 | + echo "version=${VERSION}" >> "$GITHUB_OUTPUT" |
| 73 | + echo "sha256=${SHA256}" >> "$GITHUB_OUTPUT" |
| 74 | +
|
| 75 | + - name: Checkout tap repository |
| 76 | + uses: actions/checkout@v4 |
| 77 | + with: |
| 78 | + repository: mileson/homebrew-cjfcodexswitcher |
| 79 | + token: ${{ secrets.HOMEBREW_TAP_TOKEN }} |
| 80 | + path: tap |
| 81 | + |
| 82 | + - name: Update formula |
| 83 | + shell: bash |
| 84 | + run: | |
| 85 | + VERSION="${{ steps.sha.outputs.version }}" |
| 86 | + SHA256="${{ steps.sha.outputs.sha256 }}" |
| 87 | + cat > tap/Formula/cjfcodexswitcher.rb <<EOF |
| 88 | + class Cjfcodexswitcher < Formula |
| 89 | + include Language::Python::Virtualenv |
| 90 | +
|
| 91 | + desc "Codex account switcher with live quota view and agent-friendly CLI" |
| 92 | + homepage "https://github.com/${GITHUB_REPOSITORY}" |
| 93 | + url "https://github.com/${GITHUB_REPOSITORY}/archive/refs/tags/${VERSION}.tar.gz" |
| 94 | + sha256 "${SHA256}" |
| 95 | + license "MIT" |
| 96 | +
|
| 97 | + depends_on "python@3.12" |
| 98 | +
|
| 99 | + def install |
| 100 | + virtualenv_install_with_resources |
| 101 | + end |
| 102 | +
|
| 103 | + test do |
| 104 | + output = shell_output("#{bin}/codex-switcher --help") |
| 105 | + assert_match "Codex", output |
| 106 | + end |
| 107 | + end |
| 108 | + EOF |
| 109 | +
|
| 110 | + - name: Commit and push formula |
| 111 | + shell: bash |
| 112 | + run: | |
| 113 | + cd tap |
| 114 | + git config user.name "github-actions[bot]" |
| 115 | + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 116 | + if git diff --quiet; then |
| 117 | + echo "No formula changes." |
| 118 | + exit 0 |
| 119 | + fi |
| 120 | + git add Formula/cjfcodexswitcher.rb |
| 121 | + git commit -m "Update formula to ${GITHUB_REF_NAME}" |
| 122 | + git push |
0 commit comments