diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 7c278e7e..cff8fec2 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -225,6 +225,9 @@ jobs: (inputs.publish_tag != '' || needs.release.outputs.release_created == 'true') runs-on: { group: github-public-amd64 } timeout-minutes: 30 + # The signing key and the Maven Central credentials belong in this environment's secrets, + # not in org or repository secrets that a workflow on any branch can read. + environment: release permissions: contents: read outputs: @@ -241,11 +244,41 @@ jobs: - uses: actions/checkout@v5.0.1 with: - ref: ${{ steps.target.outputs.tag }} + # refs/tags/ so that a publish_tag naming a branch or a SHA is not checked out. + ref: refs/tags/${{ steps.target.outputs.tag }} + # Full history, for the ancestry check below. + fetch-depth: 0 # The Gradle build runs project code, and nothing here writes to the # repository, so do not leave GITHUB_TOKEN in .git/config. persist-credentials: false + # publish_tag is free text, so without this any tag, including one pushed on a commit + # that was never reviewed, could be signed and published. Only publish a tag whose + # commit is already on the release branch this run belongs to. + - name: Verify the tag is on this release branch + env: + TAG: ${{ steps.target.outputs.tag }} + DEFAULT_BRANCH: ${{ github.event.repository.default_branch }} + run: | + # The environment's deployment branches are what enforce this; the check here only + # gives a clear error before they are configured. + if [ "$GITHUB_REF" != "refs/heads/${DEFAULT_BRANCH}" ] && [[ ! "$GITHUB_REF" =~ ^refs/heads/[0-9]+(\.[0-9]+)*\.x$ ]]; then + echo "::error::Publishing runs only from ${DEFAULT_BRANCH} or an N.x release branch, not ${GITHUB_REF}." + exit 1 + fi + # Exactly a tag. Revision syntax such as v1.2.3~1 would otherwise resolve to a commit + # that was never tagged. + if ! git show-ref --verify --quiet "refs/tags/${TAG}"; then + echo "::error::${TAG} is not a tag of this repository." + exit 1 + fi + commit="$(git rev-parse "refs/tags/${TAG}^{commit}")" + if ! git merge-base --is-ancestor "$commit" "$GITHUB_SHA"; then + echo "::error::Tag ${TAG} points at ${commit}, which is not on ${GITHUB_REF_NAME}. Dispatch the workflow from the branch that release was cut from." + exit 1 + fi + echo "Tag ${TAG} (${commit}) is on ${GITHUB_REF_NAME}." + - name: Setup JDK 17 uses: actions/setup-java@v5.1.0 with: @@ -304,6 +337,9 @@ jobs: if: ${{ !cancelled() && needs.publish.result == 'success' && contains(inputs.publish-targets || 'streamRepo', 'streamRepo') }} runs-on: { group: github-public-amd64 } timeout-minutes: 30 + # The R2 credential belongs in this environment's secrets, for the same reason as the + # signing key above. + environment: release permissions: contents: read steps: