Files

154 lines
4.2 KiB
YAML

name: Publish Homebrew tap
on:
workflow_run:
workflows: ["Build and Release"]
types: [completed]
workflow_dispatch:
inputs:
version:
description: "Version without v, e.g. 1.1.13"
required: true
type: string
permissions:
contents: read
concurrency:
group: homebrew-nyaterm-tap
cancel-in-progress: false
env:
TAP_REPO: nyakang/homebrew-nyaterm
TAP_WORKFLOW: update-cask.yml
APP_NAME: NyaTerm
jobs:
publish-homebrew:
name: Trigger Homebrew tap update
runs-on: ubuntu-latest
if: >
github.event_name == 'workflow_dispatch' ||
github.event.workflow_run.conclusion == 'success'
steps:
- name: Resolve version
id: version
shell: bash
env:
EVENT_NAME: ${{ github.event_name }}
INPUT_VERSION: ${{ inputs.version }}
WORKFLOW_HEAD_BRANCH: ${{ github.event.workflow_run.head_branch }}
run: |
set -euo pipefail
PUBLISH="true"
if [ "${EVENT_NAME}" = "workflow_dispatch" ]; then
VERSION="${INPUT_VERSION#v}"
else
REF="${WORKFLOW_HEAD_BRANCH}"
if [ -z "${REF}" ]; then
echo "Cannot resolve workflow_run head_branch."
PUBLISH="false"
VERSION=""
elif [[ "${REF}" != v* ]]; then
echo "Skip: triggering workflow was not a version tag: ${REF}"
PUBLISH="false"
VERSION=""
else
VERSION="${REF#v}"
fi
fi
if [ "${PUBLISH}" = "true" ]; then
if [[ "${VERSION}" == *-* ]]; then
echo "Skip prerelease version for Homebrew stable cask: ${VERSION}"
PUBLISH="false"
fi
if ! [[ "${VERSION}" =~ ^[0-9]+(\.[0-9]+){1,3}$ ]]; then
echo "Skip invalid stable version: ${VERSION}"
PUBLISH="false"
fi
fi
{
echo "publish=${PUBLISH}"
echo "version=${VERSION}"
echo "tag=v${VERSION}"
} >> "$GITHUB_OUTPUT"
echo "publish=${PUBLISH}"
echo "version=${VERSION}"
- name: Stop when not publishable
if: steps.version.outputs.publish != 'true'
run: |
echo "Nothing to publish to Homebrew."
- name: Verify GitHub release assets
if: steps.version.outputs.publish == 'true'
shell: bash
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
TAG: ${{ steps.version.outputs.tag }}
VERSION: ${{ steps.version.outputs.version }}
run: |
set -euo pipefail
gh release view "${TAG}" \
--repo "${GITHUB_REPOSITORY}" \
--json tagName,isPrerelease,assets \
--jq '.'
assets="$(gh release view "${TAG}" \
--repo "${GITHUB_REPOSITORY}" \
--json assets \
--jq '.assets[].name')"
echo "${assets}"
required_assets=(
"NyaTerm_${VERSION}_macos_x64.dmg"
"NyaTerm_${VERSION}_macos_arm64.dmg"
)
for asset in "${required_assets[@]}"; do
if ! grep -Fxq "${asset}" <<< "${assets}"; then
echo "Missing required Homebrew asset: ${asset}"
exit 1
fi
done
- name: Trigger Homebrew tap workflow
if: steps.version.outputs.publish == 'true'
shell: bash
env:
GH_TOKEN: ${{ secrets.HOMEBREW_TAP_TOKEN }}
VERSION: ${{ steps.version.outputs.version }}
run: |
set -euo pipefail
if [ -z "${GH_TOKEN:-}" ]; then
echo "HOMEBREW_TAP_TOKEN secret is required."
exit 1
fi
run_url="$(gh workflow run "${TAP_WORKFLOW}" \
--repo "${TAP_REPO}" \
-f version="${VERSION}")"
echo "Triggered ${TAP_REPO}/${TAP_WORKFLOW} for version ${VERSION}: ${run_url}"
run_id="${run_url##*/}"
if ! [[ "${run_id}" =~ ^[0-9]+$ ]]; then
echo "Cannot resolve Homebrew tap run ID from: ${run_url}"
exit 1
fi
gh run watch "${run_id}" --repo "${TAP_REPO}" --exit-status