From 28fbe8dc740c1b66778ca88b585d21e16a1838a0 Mon Sep 17 00:00:00 2001 From: Petr Gazarov Date: Sun, 8 Oct 2023 21:57:10 -0700 Subject: [PATCH] GA: Script to update homebrew tap on release publish event --- .github/scripts/generate_homebrew_tap.sh | 41 ++++++++++++++ .github/scripts/salami_tap_template.rb | 45 +++++++++++++++ ...eate_release.yml => create_gh_release.yml} | 2 +- .github/workflows/release_homebrew_tap.yml | 56 +++++++++++++++++++ 4 files changed, 143 insertions(+), 1 deletion(-) create mode 100755 .github/scripts/generate_homebrew_tap.sh create mode 100644 .github/scripts/salami_tap_template.rb rename .github/workflows/{create_release.yml => create_gh_release.yml} (98%) create mode 100644 .github/workflows/release_homebrew_tap.yml diff --git a/.github/scripts/generate_homebrew_tap.sh b/.github/scripts/generate_homebrew_tap.sh new file mode 100755 index 0000000..f3ec87e --- /dev/null +++ b/.github/scripts/generate_homebrew_tap.sh @@ -0,0 +1,41 @@ +#!/bin/env bash + +set -euo pipefail + +export SALAMI_VERSION="${1}" +TAP_FILE="./.github/scripts/salami_tap_template.rb}" +TAP_FILE="$(realpath "${TAP_FILE}")" + +cd "$(mktemp -d)" + +>&2 echo "::info Generating Homebrew Tap..." +>&2 echo "::group::Download release assets" +>&2 gh release download --repo petrgazarov/salami "v${SALAMI_VERSION}" -p 'salami*darwin*.tar.gz' -p 'salami*linux*.tar.gz' --skip-existing +>&2 echo "::endgroup::" + +for i in \ + "darwin x64 SALAMI_DARWIN_X64" \ + "darwin arm64 SALAMI_DARWIN_ARM64" \ + "linux x64 SALAMI_LINUX_X64" \ + "linux arm64 SALAMI_LINUX_ARM64" \ + ; do + # shellcheck disable=SC2086 # intentional, we want to split the strings + set -- $i # read loop strings as args + OS="$1" + ARCH="$2" + ENV_VAR="$3" + SHA256="$(sha256sum "salami-${SALAMI_VERSION}-${OS}-${ARCH}.tar.gz" | cut -f1 -d' ')" + + SHA256_VAR="${ENV_VAR}_SHA256" + URL_VAR="${ENV_VAR}_URL" + printf -v "${SHA256_VAR}" "%s" "${SHA256}" + printf -v "${URL_VAR}" "%s" "https://github.com/petrgazarov/salami/releases/download/${SALAMI_VERSION}/salami-${SALAMI_VERSION}-${OS}-${ARCH}.tar.gz" + + export "${SHA256_VAR?}" + export "${URL_VAR?}" + >&2 echo "${OS}-${ARCH} SHA256: " "${!SHA256_VAR}" + >&2 echo "${OS}-${ARCH} URL: " "${!URL_VAR}" +done + +# shellcheck disable=SC2016 # intentional, envsubst requires us to pass variable names with $ prefixes. +envsubst '$SALAMI_VERSION,$SALAMI_DARWIN_X64_URL,$SALAMI_DARWIN_X64_SHA256,$SALAMI_DARWIN_ARM64_URL,$SALAMI_DARWIN_ARM64_SHA256,$SALAMI_LINUX_X64_URL,$SALAMI_LINUX_X64_SHA256,$SALAMI_LINUX_ARM64_URL,$SALAMI_LINUX_ARM64_SHA256' < "${TAP_FILE}" \ No newline at end of file diff --git a/.github/scripts/salami_tap_template.rb b/.github/scripts/salami_tap_template.rb new file mode 100644 index 0000000..a50854a --- /dev/null +++ b/.github/scripts/salami_tap_template.rb @@ -0,0 +1,45 @@ +# frozen_string_literal: true + +# This file was generated by https://github.com/salami/salami/blob/master/.github/scripts/generate-homebrew-tap +class Salami < Formula + desc "Salami - Infrastructure as Natural Language" + homepage "https://github.com/petrgazarov/salami" + version "${SALAMI_VERSION}" + license "Apache-2.0" + + on_macos do + if Hardware::CPU.intel? + url "${SALAMI_DARWIN_X64_URL}" + sha256 "${SALAMI_DARWIN_X64_SHA256}" + end + + if Hardware::CPU.arm? + url "${SALAMI_DARWIN_ARM64_URL}" + sha256 "${SALAMI_DARWIN_ARM64_SHA256}" + end + + def install + bin.install Dir["*"] + end + end + + on_linux do + if Hardware::CPU.arm? && Hardware::CPU.is_64_bit? + url "${SALAMI_LINUX_ARM64_URL}" + sha256 "${SALAMI_LINUX_ARM64_SHA256}" + end + + if Hardware::CPU.intel? + url "${SALAMI_LINUX_X64_URL}" + sha256 "${SALAMI_LINUX_X64_SHA256}" + end + + def install + bin.install Dir["*"] + end + end + + test do + system "#{bin}/salami version" + end +end \ No newline at end of file diff --git a/.github/workflows/create_release.yml b/.github/workflows/create_gh_release.yml similarity index 98% rename from .github/workflows/create_release.yml rename to .github/workflows/create_gh_release.yml index 91f9be0..8935fc8 100644 --- a/.github/workflows/create_release.yml +++ b/.github/workflows/create_gh_release.yml @@ -1,4 +1,4 @@ -name: Create release +name: Create GH release on: push: diff --git a/.github/workflows/release_homebrew_tap.yml b/.github/workflows/release_homebrew_tap.yml new file mode 100644 index 0000000..619c96e --- /dev/null +++ b/.github/workflows/release_homebrew_tap.yml @@ -0,0 +1,56 @@ +name: Release Homebrew tap + +on: + release: + types: + - published + +permissions: + contents: read + +jobs: + update-homebrew-salami: + name: Update Homebrew Tap + runs-on: ubuntu-latest + steps: + - name: Checkout Salami repo + uses: actions/checkout@v3 + with: + ref: ${{ github.event.release.target_commitish }} + path: salami + + - name: Checkout Salami homebrew repo + uses: actions/checkout@v3 + with: + repository: petrgazarov/homebrew-salami + path: homebrew-salami + token: ${{ secrets.HOMEBREW_SALAMI_GITHUB_TOKEN }} + + - name: Update Homebrew Tap + run: | + set -euo pipefail + + # Can simulate this by cloning petrgazarov/salami & petrgazarov/homebrew-salami to adacent directories + # and running from their parent: + + ./salami/.github/scripts/generate-homebrew-salami.sh \ + "${SALAMI_VERSION}" ./salami/.github/scripts/salami-tap-template.rb \ + > ./homebrew-salami/Formula/salami.rb + - name: Commit updated formula + working-directory: homebrew-salami + run: | + set -euo pipefail + + git config user.name petrgazarov + git config user.email petrgazarov@gmail.com + git add Formula/salami.rb + echo "::group::git diff" + git --no-pager diff + echo "::endgroup::" + git commit -m "Brew formula update for Salami version ${SALAMI_VERSION}" + - name: Push formula + working-directory: homebrew-salami + run: | + set -euo pipefail + + git push origin HEAD:main