Merge pull request #220 from dvm-shlee/main #14
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: Release On Merge | |
| on: | |
| push: | |
| branches: [main] | |
| permissions: | |
| contents: write | |
| actions: write | |
| jobs: | |
| tag_release: | |
| if: ${{ github.repository == 'BrkRaw/brkraw' }} | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - name: Detect version change | |
| id: version | |
| env: | |
| GH_TOKEN: ${{ github.token }} | |
| REPO: ${{ github.repository }} | |
| SHA: ${{ github.sha }} | |
| BEFORE: ${{ github.event.before }} | |
| run: | | |
| set -euo pipefail | |
| # Default outputs | |
| echo "changed=false" >> "$GITHUB_OUTPUT" | |
| # Only proceed for merge commits ("Merge pull request ...") | |
| if [[ "${{ github.event.head_commit.message }}" != Merge\ pull\ request* ]]; then | |
| echo "Not a merge commit. Skipping." | |
| exit 0 | |
| fi | |
| # Find the PR associated with this merge commit and ensure it has the 'release' label. | |
| api="https://api.github.com/repos/${REPO}/commits/${SHA}/pulls" | |
| pr_json=$(curl -fsSL \ | |
| -H "Authorization: Bearer ${GH_TOKEN}" \ | |
| -H "Accept: application/vnd.github+json" \ | |
| -H "Accept: application/vnd.github.groot-preview+json" \ | |
| "$api" || true) | |
| export PR_JSON="$pr_json" | |
| pr_number=$(python3 - <<'PY' | |
| import json, os | |
| s = os.environ.get('PR_JSON','') | |
| if not s.strip(): | |
| print('') | |
| raise SystemExit(0) | |
| data = json.loads(s) | |
| if isinstance(data, list) and data: | |
| print(data[0].get('number','')) | |
| else: | |
| print('') | |
| PY | |
| ) | |
| if [[ -z "${pr_number}" ]]; then | |
| echo "No PR associated with ${SHA}. Skipping." | |
| exit 0 | |
| fi | |
| labels_api="https://api.github.com/repos/${REPO}/issues/${pr_number}/labels" | |
| labels_json=$(curl -fsSL \ | |
| -H "Authorization: Bearer ${GH_TOKEN}" \ | |
| -H "Accept: application/vnd.github+json" \ | |
| "$labels_api") | |
| export LABELS_JSON="$labels_json" | |
| has_release=$(python3 - <<'PY' | |
| import json, os | |
| labels = json.loads(os.environ['LABELS_JSON']) | |
| names = {l.get('name','') for l in labels if isinstance(l, dict)} | |
| print('true' if 'release' in names else 'false') | |
| PY | |
| ) | |
| if [[ "${has_release}" != "true" ]]; then | |
| echo "PR #${pr_number} does not have 'release' label. Skipping." | |
| exit 0 | |
| fi | |
| # Check whether version file changed in this push | |
| if ! git diff --name-only "${BEFORE}" "${SHA}" | grep -q '^src/brkraw/__init__.py$'; then | |
| echo "src/brkraw/__init__.py not changed. Skipping." | |
| exit 0 | |
| fi | |
| version=$(python3 - <<'PY' | |
| import re | |
| from pathlib import Path | |
| text = Path("src/brkraw/__init__.py").read_text(encoding="utf-8").lstrip("\ufeff") | |
| match = re.search( | |
| r"__version__(?:\s*:\s*[^=]+)?\s*=\s*[\"']([^\"']+)[\"']", | |
| text, | |
| ) | |
| if not match: | |
| raise SystemExit("No __version__ found in src/brkraw/__init__.py") | |
| print(match.group(1)) | |
| PY | |
| ) | |
| echo "changed=true" >> "$GITHUB_OUTPUT" | |
| echo "value=$version" >> "$GITHUB_OUTPUT" | |
| shell: bash | |
| - name: Create and push tag | |
| if: ${{ steps.version.outputs.changed == 'true' }} | |
| run: | | |
| tag="${{ steps.version.outputs.value }}" | |
| if git rev-parse "$tag" >/dev/null 2>&1; then | |
| echo "Tag $tag already exists. Skipping." | |
| exit 0 | |
| fi | |
| git tag "$tag" "${{ github.sha }}" | |
| git push origin "$tag" | |
| - name: Trigger release workflow | |
| if: ${{ steps.version.outputs.changed == 'true' }} | |
| env: | |
| GH_TOKEN: ${{ github.token }} | |
| run: | | |
| tag="${{ steps.version.outputs.value }}" | |
| gh workflow run release.yml -f tag="$tag" |