diff --git a/.github/workflows/release-notes.yml b/.github/workflows/release-notes.yml index ef2fae6..5d32e22 100644 --- a/.github/workflows/release-notes.yml +++ b/.github/workflows/release-notes.yml @@ -47,3 +47,19 @@ jobs: fi gh release create "$TAG" --verify-tag --generate-notes --title "$TAG" "${flags[@]}" + + # repository_dispatch is one of the events that may start another workflow when it is sent + # with GITHUB_TOKEN. The receiving workflow is loaded from the default branch and validates + # both values before it obtains publication credentials. + - name: Request package publication + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + REPOSITORY: ${{ github.repository }} + TAG: ${{ github.ref_name }} + run: | + set -euo pipefail + release_sha="$(git rev-parse "${TAG}^{commit}")" + gh api --method POST "repos/$REPOSITORY/dispatches" \ + -F event_type=publish-release \ + -F "client_payload[tag]=$TAG" \ + -F "client_payload[sha]=$release_sha" diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 1ddb6f3..b773043 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,9 +1,11 @@ name: Create Release on: - push: - tags: - - 'v*' # Push events to matching v*, i.e. v1.0, v20.15.10 + # Repository-dispatch workflows are loaded from the default branch. A tag therefore cannot + # select a modified copy of this workflow and remove the checks below. release-notes.yml sends + # this event after creating the GitHub Release for a pushed tag. + repository_dispatch: + types: [publish-release] # The workflow never writes to the repository. Packages are published through nuget.org Trusted # Publishing: the job proves its identity with a GitHub OIDC token and receives an API key that is @@ -27,6 +29,24 @@ jobs: contents: read id-token: write # lets the job request the OIDC token that nuget.org exchanges for an API key steps: + - name: Validate release tag + id: release + env: + TAG: ${{ github.event.client_payload.tag }} + RELEASE_SHA: ${{ github.event.client_payload.sha }} + run: | + set -euo pipefail + semver='^v(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(-((0|[1-9][0-9]*|[0-9]*[A-Za-z-][0-9A-Za-z-]*)(\.(0|[1-9][0-9]*|[0-9]*[A-Za-z-][0-9A-Za-z-]*))*))?(\+([0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*))?$' + if [[ ! "$TAG" =~ $semver ]]; then + echo "::error::Release tag '$TAG' is not an exact SemVer tag (for example, v2.1.0 or v3.0.0-preview.1)." + exit 1 + fi + if [[ ! "$RELEASE_SHA" =~ ^[0-9a-f]{40}$ ]]; then + echo "::error::The release request did not contain a full commit SHA." + exit 1 + fi + echo "version=${TAG#v}" >> "$GITHUB_OUTPUT" + # Fail in seconds, not after the build, when publishing is not configured. - name: Check publishing configuration env: @@ -40,8 +60,30 @@ jobs: - name: Checkout code uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: + ref: ${{ github.event.client_payload.tag }} + # Fetch every branch so the tag's commit can be tied to a reviewed release line. + fetch-depth: 0 persist-credentials: false + - name: Verify release commit provenance + env: + TAG: ${{ github.event.client_payload.tag }} + RELEASE_SHA: ${{ github.event.client_payload.sha }} + run: | + set -euo pipefail + if [ "$(git rev-parse HEAD)" != "$RELEASE_SHA" ]; then + echo "::error::Release tag '$TAG' no longer resolves to the commit that requested publication." + exit 1 + fi + + trusted_ref="$(git for-each-ref --count=1 --format='%(refname)' --contains="$RELEASE_SHA" \ + refs/remotes/origin/master 'refs/remotes/origin/release/*')" + if [ -z "$trusted_ref" ]; then + echo "::error::Release tag '$TAG' does not point to a commit from master or a release/* branch." + exit 1 + fi + echo "Release commit is reachable from ${trusted_ref#refs/remotes/origin/}." + # The 11.0 SDK builds both target frameworks (net10.0 and net11.0). The tests run on net10.0 and # net11.0, so the 10.0 runtime has to be present as well - it comes with the 10.0 SDK. - name: Setup .NET @@ -56,10 +98,8 @@ jobs: - name: Build and pack env: - # Passed through the environment, never interpolated into the script: a tag name may - # contain shell metacharacters. - TAG: ${{ github.ref_name }} - run: dotnet build TailoredApps.Shared.slnx -c Release -p:Version="${TAG#v}" + PACKAGE_VERSION: ${{ steps.release.outputs.version }} + run: dotnet build TailoredApps.Shared.slnx -c Release -p:Version="$PACKAGE_VERSION" - name: Test run: dotnet test TailoredApps.Shared.slnx -c Release --no-build