|
| 1 | +name: Update Homebrew Tap |
| 2 | + |
| 3 | +on: |
| 4 | + release: |
| 5 | + types: [published] |
| 6 | + |
| 7 | +permissions: |
| 8 | + contents: read |
| 9 | + |
| 10 | +jobs: |
| 11 | + update-homebrew: |
| 12 | + runs-on: ubuntu-latest |
| 13 | + steps: |
| 14 | + - |
| 15 | + name: Checkout |
| 16 | + uses: actions/checkout@v4 |
| 17 | + - |
| 18 | + name: Get release info |
| 19 | + id: release_info |
| 20 | + run: | |
| 21 | + VERSION=${{ github.event.release.tag_name }} |
| 22 | + echo "version=$VERSION" >> $GITHUB_OUTPUT |
| 23 | +
|
| 24 | + # Get release assets URLs |
| 25 | + LINUX_AMD64_URL="https://github.com/latitudesh/cli/releases/download/${VERSION}/lsh_Linux_x86_64.tar.gz" |
| 26 | + DARWIN_AMD64_URL="https://github.com/latitudesh/cli/releases/download/${VERSION}/lsh_Darwin_x86_64.tar.gz" |
| 27 | + DARWIN_ARM64_URL="https://github.com/latitudesh/cli/releases/download/${VERSION}/lsh_Darwin_arm64.tar.gz" |
| 28 | +
|
| 29 | + echo "linux_amd64_url=$LINUX_AMD64_URL" >> $GITHUB_OUTPUT |
| 30 | + echo "darwin_amd64_url=$DARWIN_AMD64_URL" >> $GITHUB_OUTPUT |
| 31 | + echo "darwin_arm64_url=$DARWIN_ARM64_URL" >> $GITHUB_OUTPUT |
| 32 | + - |
| 33 | + name: Download assets and calculate SHA256 |
| 34 | + id: checksums |
| 35 | + run: | |
| 36 | + VERSION=${{ steps.release_info.outputs.version }} |
| 37 | +
|
| 38 | + # Download checksums file from release |
| 39 | + wget -q "https://github.com/latitudesh/cli/releases/download/${VERSION}/checksums.txt" |
| 40 | +
|
| 41 | + # Extract SHA256 for each platform |
| 42 | + LINUX_AMD64_SHA=$(grep "lsh_Linux_x86_64.tar.gz" checksums.txt | cut -d' ' -f1) |
| 43 | + DARWIN_AMD64_SHA=$(grep "lsh_Darwin_x86_64.tar.gz" checksums.txt | cut -d' ' -f1) |
| 44 | + DARWIN_ARM64_SHA=$(grep "lsh_Darwin_arm64.tar.gz" checksums.txt | cut -d' ' -f1) |
| 45 | +
|
| 46 | + echo "linux_amd64_sha=$LINUX_AMD64_SHA" >> $GITHUB_OUTPUT |
| 47 | + echo "darwin_amd64_sha=$DARWIN_AMD64_SHA" >> $GITHUB_OUTPUT |
| 48 | + echo "darwin_arm64_sha=$DARWIN_ARM64_SHA" >> $GITHUB_OUTPUT |
| 49 | + - |
| 50 | + name: Update Homebrew formula |
| 51 | + uses: actions/github-script@v7 |
| 52 | + with: |
| 53 | + github-token: ${{ secrets.HOMEBREW_TAP_TOKEN }} |
| 54 | + script: | |
| 55 | + const version = '${{ steps.release_info.outputs.version }}'; |
| 56 | + const versionNumber = version.replace('v', ''); |
| 57 | +
|
| 58 | + // Get current formula content |
| 59 | + const { data: currentFile } = await github.rest.repos.getContent({ |
| 60 | + owner: 'latitudesh', |
| 61 | + repo: 'homebrew-tools', |
| 62 | + path: 'lsh.rb', |
| 63 | + ref: 'main' |
| 64 | + }); |
| 65 | +
|
| 66 | + // Create new formula content |
| 67 | + const formulaContent = `class Lsh < Formula |
| 68 | + desc "Latitude.sh CLI tool" |
| 69 | + homepage "https://www.latitude.sh/" |
| 70 | + version "${versionNumber}" |
| 71 | + license "MIT" |
| 72 | +
|
| 73 | + on_macos do |
| 74 | + if Hardware::CPU.arm? |
| 75 | + url "${{ steps.release_info.outputs.darwin_arm64_url }}" |
| 76 | + sha256 "${{ steps.checksums.outputs.darwin_arm64_sha }}" |
| 77 | + else |
| 78 | + url "${{ steps.release_info.outputs.darwin_amd64_url }}" |
| 79 | + sha256 "${{ steps.checksums.outputs.darwin_amd64_sha }}" |
| 80 | + end |
| 81 | + end |
| 82 | +
|
| 83 | + on_linux do |
| 84 | + if Hardware::CPU.intel? |
| 85 | + url "${{ steps.release_info.outputs.linux_amd64_url }}" |
| 86 | + sha256 "${{ steps.checksums.outputs.linux_amd64_sha }}" |
| 87 | + end |
| 88 | + end |
| 89 | +
|
| 90 | + def install |
| 91 | + bin.install "lsh" |
| 92 | + end |
| 93 | +
|
| 94 | + test do |
| 95 | + system "#{bin}/lsh", "version" |
| 96 | + end |
| 97 | + end |
| 98 | + `; |
| 99 | +
|
| 100 | + // Create a new branch |
| 101 | + const branchName = `update-lsh-${versionNumber}`; |
| 102 | + const { data: mainRef } = await github.rest.git.getRef({ |
| 103 | + owner: 'latitudesh', |
| 104 | + repo: 'homebrew-tools', |
| 105 | + ref: 'heads/main' |
| 106 | + }); |
| 107 | +
|
| 108 | + try { |
| 109 | + await github.rest.git.createRef({ |
| 110 | + owner: 'latitudesh', |
| 111 | + repo: 'homebrew-tools', |
| 112 | + ref: `refs/heads/${branchName}`, |
| 113 | + sha: mainRef.object.sha |
| 114 | + }); |
| 115 | + } catch (error) { |
| 116 | + console.log('Branch might already exist, deleting and recreating...'); |
| 117 | + await github.rest.git.deleteRef({ |
| 118 | + owner: 'latitudesh', |
| 119 | + repo: 'homebrew-tools', |
| 120 | + ref: `heads/${branchName}` |
| 121 | + }); |
| 122 | + await github.rest.git.createRef({ |
| 123 | + owner: 'latitudesh', |
| 124 | + repo: 'homebrew-tools', |
| 125 | + ref: `refs/heads/${branchName}`, |
| 126 | + sha: mainRef.object.sha |
| 127 | + }); |
| 128 | + } |
| 129 | +
|
| 130 | + // Update the file in the new branch |
| 131 | + await github.rest.repos.createOrUpdateFileContents({ |
| 132 | + owner: 'latitudesh', |
| 133 | + repo: 'homebrew-tools', |
| 134 | + path: 'lsh.rb', |
| 135 | + message: `chore: update lsh formula to ${version}`, |
| 136 | + content: Buffer.from(formulaContent).toString('base64'), |
| 137 | + branch: branchName, |
| 138 | + sha: currentFile.sha |
| 139 | + }); |
| 140 | +
|
| 141 | + // Create pull request |
| 142 | + const { data: pr } = await github.rest.pulls.create({ |
| 143 | + owner: 'latitudesh', |
| 144 | + repo: 'homebrew-tools', |
| 145 | + title: `chore: update lsh to ${version}`, |
| 146 | + head: branchName, |
| 147 | + base: 'main', |
| 148 | + body: `Updates lsh Homebrew formula to version ${version}\n\nAuto-generated by release workflow.` |
| 149 | + }); |
| 150 | +
|
| 151 | + console.log(`Pull request created: ${pr.html_url}`); |
0 commit comments