|
| 1 | +# This is NOT a reusable workflow, and is not part of the public API. |
| 2 | +# |
| 3 | +# This workflow is triggered by a push to a version tag (e.g. v1.2.3) or |
| 4 | +# manually via workflow_dispatch. It moves the commit pointed to by the tag to |
| 5 | +# a major branch (e.g. v1, v2, etc.). For v0.x.y releases, it creates branches |
| 6 | +# like v0.x (e.g. v0.3 for v0.3.5). If the major branch already exists, it |
| 7 | +# will reset it to the commit of the tag. If it does not exist, it will create |
| 8 | +# the branch at the commit of the tag. |
| 9 | +name: "Move/update major branch" |
| 10 | + |
| 11 | +on: |
| 12 | + push: |
| 13 | + tags: |
| 14 | + - 'v[0-9]+\.[0-9]+\.[0-9]+' |
| 15 | + workflow_dispatch: |
| 16 | + inputs: |
| 17 | + tag: |
| 18 | + description: "Tag to process (e.g. v3.2.1 creates v3 branch, v0.3.5 creates v0.3 branch)" |
| 19 | + required: true |
| 20 | + |
| 21 | +permissions: |
| 22 | + contents: write |
| 23 | + |
| 24 | +concurrency: |
| 25 | + group: move-major-branch |
| 26 | + cancel-in-progress: false |
| 27 | + |
| 28 | +jobs: |
| 29 | + move-major-branch: |
| 30 | + runs-on: ubuntu-latest |
| 31 | + steps: |
| 32 | + # 1) Check out repo so we can move branches |
| 33 | + - name: Check out repository |
| 34 | + uses: actions/checkout@v4 |
| 35 | + with: |
| 36 | + fetch-depth: 0 |
| 37 | + token: ${{ secrets.SERVICE_TOKEN }} |
| 38 | + |
| 39 | + # 2) Work out which tag & commit we’re supposed to handle |
| 40 | + - name: Determine tag & commit |
| 41 | + id: vars |
| 42 | + run: | |
| 43 | + if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then |
| 44 | + TAG="${{ inputs.tag }}" |
| 45 | + # Make sure the tag exists locally |
| 46 | + if ! git rev-parse -q --verify "refs/tags/${TAG}" >/dev/null; then |
| 47 | + echo "::error::Tag '${TAG}' not found in origin; did you push it?" |
| 48 | + exit 1 |
| 49 | + fi |
| 50 | + COMMIT=$(git rev-parse "refs/tags/${TAG}^{commit}") |
| 51 | + else |
| 52 | + TAG="${GITHUB_REF#refs/tags/}" |
| 53 | + COMMIT="${GITHUB_SHA}" |
| 54 | + fi |
| 55 | +
|
| 56 | + # Extract the target branch based on semver, handling v0.x.y specially |
| 57 | + if [[ "${TAG}" =~ ^v0\. ]]; then |
| 58 | + # For v0.x.y releases, use v0.x as the target branch |
| 59 | + TARGET_BRANCH=$(echo "${TAG}" | cut -d. -f1,2) # e.g. v0.3.5 -> v0.3 |
| 60 | + else |
| 61 | + # For v1.x.y and above, use just the major version |
| 62 | + TARGET_BRANCH="${TAG%%.*}" # e.g. v1.2.3 -> v1 |
| 63 | + fi |
| 64 | +
|
| 65 | + echo "tag=${TAG}" >>"$GITHUB_OUTPUT" |
| 66 | + echo "commit=${COMMIT}" >>"$GITHUB_OUTPUT" |
| 67 | + echo "target_branch=${TARGET_BRANCH}" >>"$GITHUB_OUTPUT" |
| 68 | +
|
| 69 | +
|
| 70 | + # 3) Fast‑forward or create the major branch |
| 71 | + - name: Move or create major branch |
| 72 | + env: |
| 73 | + TARGET_BRANCH: ${{ steps.vars.outputs.target_branch }} |
| 74 | + COMMIT: ${{ steps.vars.outputs.commit }} |
| 75 | + run: | |
| 76 | + set -euo pipefail |
| 77 | +
|
| 78 | + git config user.name "github-actions[bot]" |
| 79 | + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 80 | +
|
| 81 | + if git ls-remote --exit-code --heads origin "$TARGET_BRANCH" >/dev/null; then |
| 82 | + echo "Resetting branch $TARGET_BRANCH to $COMMIT" |
| 83 | + git branch -f "$TARGET_BRANCH" "$COMMIT" |
| 84 | + else |
| 85 | + echo "Creating new branch $TARGET_BRANCH at $COMMIT" |
| 86 | + git branch "$TARGET_BRANCH" "$COMMIT" |
| 87 | + fi |
| 88 | + git push origin "$COMMIT:refs/heads/$TARGET_BRANCH" --force-with-lease |
0 commit comments