Skip to content

Brew formula update #50

Brew formula update

Brew formula update #50

# Copyright AGNTCY Contributors (https://github.com/agntcy)
# SPDX-License-Identifier: Apache-2.0
name: Brew formula update
# This workflow automatize the brew formula file update process with replacing the version number to the latest,
# recalculate all hash for the binaries and create a new PR with the changes.
on:
release:
types: [released]
workflow_call:
workflow_dispatch:
inputs:
latest_tag_override:
description: "Latest tag override for testing"
required: false
type: string
default: ""
dry-run:
description: "Dry run mode (not creating a PR)"
required: false
type: boolean
default: false
permissions:
contents: write
pull-requests: write
jobs:
formula-update:
name: Update Formula
runs-on: ubuntu-latest
env:
# Required for `gh` CLI authentication
GH_TOKEN: ${{ secrets.AGNTCY_BUILD_BOT_GH_TOKEN }}
steps:
- name: Checkout code
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
fetch-depth: 0
- name: Download latest binary releases
id: release-infos
run: |
if [ -n "${{ inputs.latest_tag_override }}" ]; then
LATEST_VERSION="${{ inputs.latest_tag_override }}"
else
LATEST_VERSION=$(gh release view --json tagName --jq '.tagName')
fi
echo "LATEST_VERSION=$LATEST_VERSION" >> $GITHUB_OUTPUT
while IFS=$'\t' read -r name hash; do
case "$name" in
dirctl-darwin-arm64) echo "DIRCTL_HASH_DARWIN_ARM=$hash" >> $GITHUB_OUTPUT ;;
dirctl-darwin-amd64) echo "DIRCTL_HASH_DARWIN_AMD=$hash" >> $GITHUB_OUTPUT ;;
dirctl-linux-arm64) echo "DIRCTL_HASH_LINUX_ARM=$hash" >> $GITHUB_OUTPUT ;;
dirctl-linux-amd64) echo "DIRCTL_HASH_LINUX_AMD=$hash" >> $GITHUB_OUTPUT ;;
esac
done < <(
gh release view $LATEST_VERSION --repo agntcy/dir --json assets \
--jq '.assets[] | select(.name | startswith("dirctl-")) | "\(.name)\t\(.digest | sub("^sha256:"; ""))"'
)
- name: Update Brew formula
id: brew-formula
run: |
# Note: the following account information will not work on GHES
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git remote set-url origin https://github.com/agntcy/dir.git
git push origin --delete chore/brew-formula-update || true
git checkout -B chore/brew-formula-update origin/main
# Replace version
sed -i "s/version \"v*.*.*\"/version \""${{ steps.release-infos.outputs.LATEST_VERSION }}"\"/" HomebrewFormula/dirctl.rb
# Replace hashes
sed -i "/url \".*\/dirctl-darwin-arm64\"/ {N;s/sha256 \".*\"/sha256 \"${{ steps.release-infos.outputs.DIRCTL_HASH_DARWIN_ARM }}\"/}" ./HomebrewFormula/dirctl.rb
sed -i "/url \".*\/dirctl-darwin-amd64\"/ {N;s/sha256 \".*\"/sha256 \"${{ steps.release-infos.outputs.DIRCTL_HASH_DARWIN_AMD }}\"/}" ./HomebrewFormula/dirctl.rb
sed -i "/url \".*\/dirctl-linux-arm64\"/ {N;s/sha256 \".*\"/sha256 \"${{ steps.release-infos.outputs.DIRCTL_HASH_LINUX_ARM }}\"/}" ./HomebrewFormula/dirctl.rb
sed -i "/url \".*\/dirctl-linux-amd64\"/ {N;s/sha256 \".*\"/sha256 \"${{ steps.release-infos.outputs.DIRCTL_HASH_LINUX_AMD }}\"/}" ./HomebrewFormula/dirctl.rb
DIFF_FOUND=0
if ! git diff --exit-code; then
DIFF_FOUND=1
fi
echo "DIFF_FOUND=$DIFF_FOUND" >> $GITHUB_OUTPUT
if [ $DIFF_FOUND -eq 1 ]; then
# NOTE: Make versioned copy of latest formula
LATEST_VERSION="${{ steps.release-infos.outputs.LATEST_VERSION }}"
VERSION_NUMBER="${LATEST_VERSION#v}"
MAJOR_VERSION="${VERSION_NUMBER%%.*}"
MINOR_VERSION="${VERSION_NUMBER#*.}"; MINOR_VERSION="${MINOR_VERSION%%.*}"
VERSION_DIR="./HomebrewFormula/dirctl/v${MAJOR_VERSION}/v${MAJOR_VERSION}.${MINOR_VERSION}"
mkdir -p $VERSION_DIR || true
cp ./HomebrewFormula/dirctl.rb $VERSION_DIR/dirctl@${VERSION_NUMBER}.rb
# NOTE: Fixing the class name in the versioned formula file
AT_SUFFIX=$(ruby -e '
abort "missing version" if ARGV[0].empty?
core, pre = ARGV[0].split("-", 2)
s = core.gsub(".", "")
s << pre.split(".").map(&:capitalize).join if pre
print s
' "$VERSION_NUMBER")
sed -i "1s/^class Dirctl </class DirctlAT${AT_SUFFIX} </" "$VERSION_DIR/dirctl@${VERSION_NUMBER}.rb"
git commit --all -m "chore: update brew formula version" --signoff
git push --set-upstream origin chore/brew-formula-update
fi
- name: Test new formula
id: test-brew-formula
if: ${{ steps.brew-formula.outputs.DIFF_FOUND == 1 }}
run: |
apt update && apt install curl git -y
NONINTERACTIVE=1 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
# Make local formula for testing from the repo homebrew formula file
brew tap-new --no-git agntcy/dir-test
FORMULA_DIR="$(brew --repository)/Library/Taps/agntcy/homebrew-dir-test/Formula/"
cp ./HomebrewFormula/dirctl.rb "$FORMULA_DIR"
brew install agntcy/dir-test/dirctl --verbose
dirctl --help
- name: Create PR
if: ${{ steps.brew-formula.outputs.DIFF_FOUND == 1 && steps.test-brew-formula.outcome == 'success' && inputs.dry-run == false }}
run: |
gh pr create --title "chore(dirctl): update brew formula to ${{ steps.release-infos.outputs.LATEST_VERSION }}" --body "This PR is created by brew-formula-update workflow."