|
| 1 | +name: Push GitHub Action to release branch |
| 2 | +on: |
| 3 | + push: |
| 4 | + branches: |
| 5 | + - main |
| 6 | + paths: |
| 7 | + - 'action.yml' |
| 8 | + - 'index.js' |
| 9 | + - 'package*.json' |
| 10 | + - '.github/workflows/push-to-release-branch.yml' |
| 11 | + |
| 12 | +permissions: |
| 13 | + contents: write |
| 14 | + |
| 15 | +env: |
| 16 | + V: v0 |
| 17 | + |
| 18 | +jobs: |
| 19 | + push-to-release-branch: |
| 20 | + runs-on: ubuntu-latest |
| 21 | + steps: |
| 22 | + - name: Checkout code |
| 23 | + uses: actions/checkout@v5 |
| 24 | + with: |
| 25 | + persist-credentials: true |
| 26 | + - name: Determine required Node.js version |
| 27 | + run: | |
| 28 | + sed -n 's/^ *using: "node\([0-9]\+\)".*/NODE_VERSION=\1/p' <action.yml >>$GITHUB_ENV |
| 29 | + - name: Set up Node.js |
| 30 | + uses: actions/setup-node@v5 |
| 31 | + with: |
| 32 | + node-version: ${{ env.NODE_VERSION }} |
| 33 | + - name: Install dependencies |
| 34 | + run: npm ci |
| 35 | + - name: Build dist/ |
| 36 | + run: npm run prepare |
| 37 | + - name: Commit the result |
| 38 | + id: commit |
| 39 | + run: | |
| 40 | + git config --local user.name "GitGitGadget CI" && |
| 41 | + git config --local user.email "ci@noreply.github.com" && |
| 42 | +
|
| 43 | + if ! git fetch --tags origin $V:refs/remotes/origin/$V |
| 44 | + then |
| 45 | + # Starting from scratch |
| 46 | + merge_head=HEAD && |
| 47 | + tag_name=$V.0.0 |
| 48 | + else |
| 49 | + set -x && |
| 50 | + git for-each-ref && |
| 51 | + merge_head=refs/remotes/origin/$V && |
| 52 | + tag_name="$(git describe --match 'v[0-9]*' $merge_head)" && |
| 53 | + tag_name="${tag_name%%-*}" && |
| 54 | + case "$tag_name" in |
| 55 | + $V.*) incr="${tag_name#*$V.0.}"; tag_name="$V.0.$(($incr + 1))";; |
| 56 | + *) |
| 57 | + tag_name=$V.0.0 |
| 58 | + ! git rev-parse --verify refs/tags/$tag_name || { |
| 59 | + echo "$V.0.0 already exists but is not reachable from $V?!?" >&2 |
| 60 | + exit 1 |
| 61 | + } |
| 62 | + ;; |
| 63 | + esac |
| 64 | + fi && |
| 65 | +
|
| 66 | + # Now, add the generated files |
| 67 | + git add -Af dist/ && |
| 68 | + # Remove the rest |
| 69 | + git rm -r -- \* ':(exclude)dist/' ':(exclude)action.yml' && |
| 70 | +
|
| 71 | + # Now make that fake merge commit |
| 72 | + tree=$(git write-tree) && |
| 73 | + msg="Sync $V with ($(git show -s --pretty=reference HEAD))" && |
| 74 | + case "$merge_head" in |
| 75 | + HEAD) commit=$(git commit-tree -m "$msg" $tree -p HEAD);; |
| 76 | + *) commit=$(git commit-tree -m "$msg" $tree -p $merge_head -p HEAD);; |
| 77 | + esac && |
| 78 | +
|
| 79 | + # Now update the release branch and tag it |
| 80 | + git update-ref refs/heads/$V $commit && |
| 81 | + git tag $tag_name $commit && |
| 82 | + echo "result=$tag_name" >>$GITHUB_OUTPUT |
| 83 | + - name: Push to ${{ env.V }} |
| 84 | + run: git push origin $V ${{ steps.commit.outputs.result }} |
0 commit comments