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