Skip to content

Commit

Permalink
GA: Script to update homebrew tap on release publish event
Browse files Browse the repository at this point in the history
  • Loading branch information
petrgazarov committed Oct 9, 2023
1 parent abfe788 commit 28fbe8d
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 1 deletion.
41 changes: 41 additions & 0 deletions .github/scripts/generate_homebrew_tap.sh
Original file line number Diff line number Diff line change
@@ -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}"
45 changes: 45 additions & 0 deletions .github/scripts/salami_tap_template.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Create release
name: Create GH release

on:
push:
Expand Down
56 changes: 56 additions & 0 deletions .github/workflows/release_homebrew_tap.yml
Original file line number Diff line number Diff line change
@@ -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 [email protected]
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

0 comments on commit 28fbe8d

Please sign in to comment.