|
| 1 | +# Runs on pushes to main, manages the `action` branch and `action/` tags family. |
| 2 | +name: Build and release action |
| 3 | + |
| 4 | +permissions: |
| 5 | + contents: read |
| 6 | + packages: read |
| 7 | + |
| 8 | +on: |
| 9 | + push: |
| 10 | + branches: |
| 11 | + - 'main' |
| 12 | + |
| 13 | +jobs: |
| 14 | + release: |
| 15 | + runs-on: ubuntu-latest |
| 16 | + permissions: |
| 17 | + contents: write |
| 18 | + |
| 19 | + steps: |
| 20 | + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 |
| 21 | + with: |
| 22 | + fetch-depth: 0 # needed to make sure we get all tags |
| 23 | + - uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # v5.5.0 |
| 24 | + with: |
| 25 | + go-version: '1.24' |
| 26 | + |
| 27 | + - run: go test -v . |
| 28 | + |
| 29 | + - name: build |
| 30 | + run: | |
| 31 | + GOOS=linux GOARCH=amd64 go build -buildvcs=false -o ./dist/commit-headless-linux-amd64 . |
| 32 | + GOOS=linux GOARCH=arm64 go build -buildvcs=false -o ./dist/commit-headless-linux-arm64 . |
| 33 | +
|
| 34 | + # TODO: Not sure how to determine the current os/arch to select one of the above binaries |
| 35 | + # so we're just going to build another one |
| 36 | + go build -buildvcs=false -o ./dist/commit-headless . |
| 37 | + ./dist/commit-headless version | awk '{print $3}' > ./dist/VERSION.txt |
| 38 | + echo "Current version: $(cat ./dist/VERSION.txt)" |
| 39 | +
|
| 40 | + - name: create action branch commit |
| 41 | + id: create-commit |
| 42 | + run: | |
| 43 | +
|
| 44 | + # Copy the new assets to a temporary location that we can recover later |
| 45 | + cp -R dist /tmp/release-assets |
| 46 | +
|
| 47 | + git switch action |
| 48 | +
|
| 49 | + # Remove everything except the git directory |
| 50 | + find . -not -path "./.git" -not -path '.' -maxdepth 1 -exec rm -rf {} + |
| 51 | +
|
| 52 | + # Bring back the release assets |
| 53 | + mv /tmp/release-assets dist |
| 54 | +
|
| 55 | + # "Restore" the contents of action-template from the previous ref |
| 56 | + git restore --source "${{ github.sha }}" action-template/ |
| 57 | +
|
| 58 | + # Copy the contents of action-template to the top of the repository |
| 59 | + cp action-template/* . && rm -rf action-template |
| 60 | +
|
| 61 | + # Replace the VERSION in README.md |
| 62 | + sed -i "s/%%VERSION%%/$(cat dist/VERSION.txt)/g" README.md |
| 63 | +
|
| 64 | + # Create a commit |
| 65 | + # TODO: A merge should have the PR number in the commit headline, if we use the original |
| 66 | + # commit message it should back-link |
| 67 | + git config user.name "github-actions[bot]" |
| 68 | + git config user.email "41898282+github-actions[bot]@users.noreply.github.com>" |
| 69 | + git commit \ |
| 70 | + --all \ |
| 71 | + --message="Update action from ${{ github.sha }}" \ |
| 72 | + --allow-empty # sometimes we have nothing to change, so this ensures we can still commit |
| 73 | +
|
| 74 | + REF=$(git rev-parse HEAD) |
| 75 | + echo "sha=${REF}" >> $GITHUB_OUTPUT |
| 76 | + echo "Created commit ${REF}" |
| 77 | +
|
| 78 | + - name: push commits |
| 79 | + id: push-commits |
| 80 | + uses: ./ # use the action defined in the action branch |
| 81 | + with: |
| 82 | + branch: action |
| 83 | + command: push |
| 84 | + commits: ${{ steps.create-commit.outputs.sha }} |
| 85 | + |
| 86 | + - name: check release tag |
| 87 | + id: check-tag |
| 88 | + run: | |
| 89 | + TAG="action/v$(cat ./dist/VERSION.txt)" |
| 90 | + if git show-ref --tags --verify --quiet "refs/tags/${TAG}"; then |
| 91 | + echo "Release tag ${TAG} already exists. Not releasing." |
| 92 | + exit 1 |
| 93 | + fi |
| 94 | + echo "tag=${TAG}" >> $GITHUB_OUTPUT |
| 95 | +
|
| 96 | + - name: make release |
| 97 | + uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 |
| 98 | + with: |
| 99 | + script: | |
| 100 | + github.rest.git.createRef({ |
| 101 | + owner: context.repo.owner, |
| 102 | + repo: context.repo.repo, |
| 103 | + ref: 'refs/tags/${{ steps.check-tag.outputs.tag }}', |
| 104 | + sha: '${{ steps.push-commits.outputs.pushed_ref }}' |
| 105 | + }); |
0 commit comments