Build and Upload Homebrew Bottles #2
Workflow file for this run
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Build and Upload Homebrew Bottles | |
| on: | |
| release: | |
| types: [created] | |
| permissions: | |
| contents: write | |
| jobs: | |
| build-bottles: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout homebrew-cli | |
| uses: actions/checkout@v4 | |
| - name: Extract version from release tag | |
| id: version | |
| run: | | |
| # Expects tag like v1.0.0-beta.5 | |
| TAG="${GITHUB_REF_NAME}" | |
| VERSION="${TAG#v}" | |
| echo "version=${VERSION}" >> "$GITHUB_OUTPUT" | |
| echo "Building bottles for version ${VERSION}" | |
| - name: Build bottles | |
| run: | | |
| set -euo pipefail | |
| VERSION="${{ steps.version.outputs.version }}" | |
| FORMULA_NAME="terrakube" | |
| declare -A TAGS=( | |
| ["darwin-amd64"]="big_sur" | |
| ["darwin-arm64"]="arm64_big_sur" | |
| ["linux-amd64"]="x86_64_linux" | |
| ["linux-arm64"]="aarch64_linux" | |
| ) | |
| mkdir -p bottles | |
| for PLATFORM in "${!TAGS[@]}"; do | |
| TAG="${TAGS[$PLATFORM]}" | |
| ARCHIVE_URL="https://github.com/terrakube-io/terrakube-cli/releases/download/v${VERSION}/terrakube-v${VERSION}-${PLATFORM}.tar.gz" | |
| BOTTLE_FILE="${FORMULA_NAME}-${VERSION}.${TAG}.bottle.tar.gz" | |
| echo "==> Building bottle for ${PLATFORM} (${TAG})" | |
| STAGING=$(mktemp -d) | |
| mkdir -p "${STAGING}/${FORMULA_NAME}/${VERSION}/bin" | |
| curl -sL "$ARCHIVE_URL" | tar xz -C "${STAGING}/${FORMULA_NAME}/${VERSION}/bin" | |
| (cd "${STAGING}" && tar czf "${GITHUB_WORKSPACE}/bottles/${BOTTLE_FILE}" "${FORMULA_NAME}") | |
| SHA=$(sha256sum "bottles/${BOTTLE_FILE}" | cut -d' ' -f1) | |
| echo " sha256 cellar: :any_skip_relocation, ${TAG}: \"${SHA}\"" | |
| # Save SHA for formula update step | |
| echo "${TAG}=${SHA}" >> "$GITHUB_ENV" | |
| rm -rf "${STAGING}" | |
| done | |
| - name: Upload bottles to release | |
| uses: softprops/action-gh-release@v2 | |
| with: | |
| files: bottles/*.bottle.tar.gz | |
| - name: Update formula with new bottle SHAs | |
| run: | | |
| set -euo pipefail | |
| VERSION="${{ steps.version.outputs.version }}" | |
| # Read SHAs from environment variables set during build | |
| BIG_SUR_SHA="${big_sur}" | |
| ARM64_BIG_SUR_SHA="${arm64_big_sur}" | |
| X86_64_LINUX_SHA="${x86_64_linux}" | |
| AARCH64_LINUX_SHA="${aarch64_linux}" | |
| # Generate the new bottle block | |
| BOTTLE_BLOCK=$(cat <<EOF | |
| bottle do | |
| root_url "https://github.com/terrakube-io/homebrew-cli/releases/download/v${VERSION}" | |
| sha256 cellar: :any_skip_relocation, big_sur: "${BIG_SUR_SHA}" | |
| sha256 cellar: :any_skip_relocation, arm64_big_sur: "${ARM64_BIG_SUR_SHA}" | |
| sha256 cellar: :any_skip_relocation, x86_64_linux: "${X86_64_LINUX_SHA}" | |
| sha256 cellar: :any_skip_relocation, aarch64_linux: "${AARCH64_LINUX_SHA}" | |
| end | |
| EOF | |
| ) | |
| # Update the formula: replace existing bottle block or insert before def install | |
| FORMULA="Formula/terrakube.rb" | |
| if grep -q "bottle do" "$FORMULA"; then | |
| # Replace existing bottle block | |
| sed -i '/^ bottle do$/,/^ end$/c\'"$(echo "$BOTTLE_BLOCK")" "$FORMULA" | |
| else | |
| # Insert bottle block before 'def install' | |
| sed -i "/^ def install$/i\\${BOTTLE_BLOCK}\n" "$FORMULA" | |
| fi | |
| # Also update the version if it changed | |
| sed -i "s/version \".*\"/version \"${VERSION}\"/" "$FORMULA" | |
| - name: Commit and push formula update | |
| run: | | |
| git config user.name "github-actions[bot]" | |
| git config user.email "github-actions[bot]@users.noreply.github.com" | |
| git add Formula/terrakube.rb | |
| git diff --cached --quiet && echo "No formula changes to commit" && exit 0 | |
| git commit -m "Update bottle SHAs for v${{ steps.version.outputs.version }}" | |
| git push |