feat!: add --head-sha to better control force pushes (#29) #51
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
| # Runs on pushes to main, manages the `action` branch and `action/` tags family. | |
| name: Build and release action | |
| permissions: | |
| contents: write | |
| on: | |
| push: | |
| branches: | |
| - 'main' | |
| jobs: | |
| release: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 | |
| with: | |
| fetch-depth: 0 # needed to make sure we get all tags | |
| - uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # v5.5.0 | |
| with: | |
| go-version: '1.24' | |
| - run: go test -v . | |
| - name: build | |
| run: | | |
| GOOS=linux GOARCH=amd64 go build -buildvcs=false -o ./dist/commit-headless-linux-amd64 . | |
| GOOS=linux GOARCH=arm64 go build -buildvcs=false -o ./dist/commit-headless-linux-arm64 . | |
| # TODO: Not sure how to determine the current os/arch to select one of the above binaries | |
| # so we're just going to build another one | |
| go build -buildvcs=false -o ./dist/commit-headless . | |
| ./dist/commit-headless version | awk '{print $3}' > ./dist/VERSION.txt | |
| echo "Current version: $(cat ./dist/VERSION.txt)" | |
| - name: create action branch commit | |
| id: create-commit | |
| run: | | |
| # Copy the new assets to a temporary location that we can recover later | |
| cp -R dist /tmp/release-assets | |
| git switch action | |
| # Remove everything except the git directory | |
| find . -not -path "./.git" -not -path '.' -maxdepth 1 -exec rm -rf {} + | |
| # Bring back the release assets | |
| mv /tmp/release-assets dist | |
| # "Restore" the contents of action-template from the previous ref | |
| git restore --source "${{ github.sha }}" action-template/ | |
| # Copy the contents of action-template to the top of the repository | |
| cp action-template/* . && rm -rf action-template | |
| # Replace the VERSION in README.md | |
| sed -i "s/%%VERSION%%/$(cat dist/VERSION.txt)/g" README.md | |
| git add --all | |
| echo "Changes to commit.." | |
| git status | |
| # Create a commit | |
| # TODO: A merge should have the PR number in the commit headline, if we use the original | |
| # commit message it should back-link | |
| git config user.name "github-actions[bot]" | |
| git config user.email "41898282+github-actions[bot]@users.noreply.github.com>" | |
| git commit \ | |
| --message="Update action from ${{ github.sha }}" \ | |
| --allow-empty # sometimes we have nothing to change, so this ensures we can still commit | |
| REF=$(git rev-parse HEAD) | |
| echo "sha=${REF}" >> $GITHUB_OUTPUT | |
| echo "Created commit ${REF}" | |
| - name: push commits | |
| id: push-commits | |
| uses: ./ # use the action defined in the action branch | |
| with: | |
| branch: action | |
| command: push | |
| commits: ${{ steps.create-commit.outputs.sha }} | |
| - name: check release tag | |
| id: check-tag | |
| run: | | |
| TAG="action/v$(cat ./dist/VERSION.txt)" | |
| if git show-ref --tags --verify --quiet "refs/tags/${TAG}"; then | |
| echo "Release tag ${TAG} already exists. Not releasing." | |
| exit 1 | |
| fi | |
| echo "tag=${TAG}" >> $GITHUB_OUTPUT | |
| - name: make release | |
| uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 | |
| with: | |
| script: | | |
| github.rest.git.createRef({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| ref: 'refs/tags/${{ steps.check-tag.outputs.tag }}', | |
| sha: '${{ steps.push-commits.outputs.pushed_ref }}' | |
| }); |