# See https://github.com/containerd/nerdctl/blob/main/MAINTAINERS_GUIDE.md for how to make a release.
name: Release
on:
  push:
    tags:
    - 'v*'
    - 'test-action-release-*'
  pull_request:
    paths-ignore:
    - '**.md'

env:
  GOTOOLCHAIN: local

jobs:
  release:
    runs-on: ubuntu-24.04
    timeout-minutes: 40
    # The maximum access is "read" for PRs from public forked repos
    # https://docs.github.com/en/actions/security-guides/automatic-token-authentication#permissions-for-the-github_token
    permissions:
      contents: write  # for releases
      id-token: write  # for provenances
      attestations: write  # for provenances
    steps:
    - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683  # v4.2.2
    # FIXME: setup-qemu-action is depended by `gomodjail pack`
    - name: "Set up QEMU"
      uses: docker/setup-qemu-action@29109295f81e9208d7d86ff1c6c12d2833863392  # v3.6.0
    - name: "Install go"
      uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5  # v5.5.0
      with:
        go-version: "1.24"
        check-latest: true
    - name: "Compile binaries"
      run: make artifacts
    - name: "SHA256SUMS"
      run: |
        ( cd _output; sha256sum nerdctl-* ) | tee /tmp/SHA256SUMS
        mv /tmp/SHA256SUMS _output/SHA256SUMS
    - name: "The sha256sum of the SHA256SUMS file"
      run: (cd _output; sha256sum SHA256SUMS)
    - name: "Prepare the release note"
      run: |
        shasha=$(sha256sum _output/SHA256SUMS | awk '{print $1}')
        cat <<-EOF | tee /tmp/release-note.txt
        $(hack/generate-release-note.sh)
        - - -
        The binaries were built automatically on GitHub Actions.
        The build log is available for 90 days: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}

        The sha256sum of the SHA256SUMS file itself is \`${shasha}\` .
        - - -
        Release manager: [ADD YOUR NAME HERE] (@[ADD YOUR GITHUB ID HERE])
        EOF
    - name: "Generate artifact attestation"
      uses: actions/attest-build-provenance@db473fddc028af60658334401dc6fa3ffd8669fd  # v2.3.0
      if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')
      with:
        subject-path: _output/*
    - name: "Create release"
      if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      run: |
        tag="${GITHUB_REF##*/}"
        gh release create -F /tmp/release-note.txt --draft --title "${tag}" "${tag}" _output/*
