|
| 1 | +name: Publish |
| 2 | +run-name: Publishing com.convertapi.client:convertapi ${{ github.event.release.tag_name }} to Maven Central |
| 3 | + |
| 4 | +# A release is a GitHub Release, never a merge. Merging to main only builds (build.yml); publishing the |
| 5 | +# Release v{semver} runs this file, which tests and builds that tag's commit with the tag's version, signs |
| 6 | +# it and publishes it to Maven Central through the Central Portal. Maven Central versions are public and |
| 7 | +# permanent - a published version can never be replaced or deleted - so nothing here retries, overwrites |
| 8 | +# or skips a duplicate. |
| 9 | +# |
| 10 | +# gh release create v2.13.1 --target main --generate-notes -> 2.13.1 |
| 11 | +# gh release create v2.14.0-beta.1 --target main --prerelease ... -> 2.14.0-beta.1 |
| 12 | +# |
| 13 | +# A stable version must be a commit on main. A pre-release may be any commit, so a branch can be tried |
| 14 | +# out as a preview version before it is merged. Maven Central has no channels: a pre-release is public |
| 15 | +# like any other version, and Maven orders it below the stable version of the same number. |
| 16 | +on: |
| 17 | + release: |
| 18 | + types: [published] |
| 19 | + |
| 20 | +permissions: |
| 21 | + contents: read |
| 22 | + |
| 23 | +env: |
| 24 | + # Only this account may publish a Release that reaches Maven Central. GitHub cannot express this as a |
| 25 | + # permission on the Team plan, so it is checked here; the real controls are the v* tag ruleset (only |
| 26 | + # repository admins create v* tags) and the maven-central environment, which accepts only v* tags and |
| 27 | + # holds the only credentials that can publish. |
| 28 | + PUBLISHER: tomasr78 |
| 29 | + GROUP_PATH: com/convertapi/client |
| 30 | + ARTIFACT: convertapi |
| 31 | + CONVERT_API_BASE_URI: https://stag-v2.convertapi.com/ |
| 32 | + MAVEN_ARGS: --batch-mode --no-transfer-progress |
| 33 | + |
| 34 | +jobs: |
| 35 | + publish: |
| 36 | + name: publish |
| 37 | + runs-on: ubuntu-latest |
| 38 | + timeout-minutes: 90 |
| 39 | + environment: |
| 40 | + name: maven-central |
| 41 | + url: https://central.sonatype.com/artifact/com.convertapi.client/convertapi/${{ steps.tag.outputs.version }} |
| 42 | + concurrency: |
| 43 | + group: publish-maven-central |
| 44 | + cancel-in-progress: false |
| 45 | + steps: |
| 46 | + - name: Check who published the release |
| 47 | + run: | |
| 48 | + # A re-run keeps GITHUB_ACTOR as the original user; GITHUB_TRIGGERING_ACTOR is whoever pressed |
| 49 | + # "Re-run", so both must be the publisher or anyone with write access could replay a release. |
| 50 | + if [ "$GITHUB_ACTOR" != "$PUBLISHER" ] || [ "${GITHUB_TRIGGERING_ACTOR:-$GITHUB_ACTOR}" != "$PUBLISHER" ]; then |
| 51 | + echo "::error::Only $PUBLISHER may publish to Maven Central - this release was published by $GITHUB_ACTOR and run by ${GITHUB_TRIGGERING_ACTOR:-$GITHUB_ACTOR}." |
| 52 | + exit 1 |
| 53 | + fi |
| 54 | + echo "Published by $GITHUB_ACTOR" |
| 55 | +
|
| 56 | + # Maven Central has no OIDC trusted publishing, so this job needs a Central Portal user token and a |
| 57 | + # GPG signing key, held as secrets of the maven-central environment (see RELEASING.md). |
| 58 | + - name: Check the credentials |
| 59 | + env: |
| 60 | + CENTRAL_TOKEN_USERNAME: ${{ secrets.CENTRAL_TOKEN_USERNAME }} |
| 61 | + CENTRAL_TOKEN_PASSWORD: ${{ secrets.CENTRAL_TOKEN_PASSWORD }} |
| 62 | + GPG_PRIVATE_KEY: ${{ secrets.GPG_PRIVATE_KEY }} |
| 63 | + GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }} |
| 64 | + CONVERT_API_SECRET: ${{ secrets.CONVERTAPI_STAGING_TOKEN }} |
| 65 | + run: | |
| 66 | + MISSING="" |
| 67 | + for name in CENTRAL_TOKEN_USERNAME CENTRAL_TOKEN_PASSWORD GPG_PRIVATE_KEY GPG_PASSPHRASE; do |
| 68 | + [ -n "${!name}" ] || MISSING="$MISSING $name" |
| 69 | + done |
| 70 | + [ -n "$CONVERT_API_SECRET" ] || MISSING="$MISSING CONVERTAPI_STAGING_TOKEN" |
| 71 | + if [ -n "$MISSING" ]; then |
| 72 | + echo "::error::Missing secrets in the maven-central environment or the repository:$MISSING" |
| 73 | + exit 1 |
| 74 | + fi |
| 75 | +
|
| 76 | + # The release event runs this file from the tag, so the workflow and the code come from the same commit. |
| 77 | + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 |
| 78 | + with: |
| 79 | + fetch-depth: 0 |
| 80 | + persist-credentials: false |
| 81 | + |
| 82 | + - name: Check the tag and the version |
| 83 | + id: tag |
| 84 | + env: |
| 85 | + TAG: ${{ github.event.release.tag_name }} |
| 86 | + PRERELEASE: ${{ github.event.release.prerelease }} |
| 87 | + run: | |
| 88 | + set -euo pipefail |
| 89 | + # SemVer 2.0 without build metadata, as the v* tag ruleset enforces. |
| 90 | + SEMVER='^v(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(-[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?$' |
| 91 | + if ! [[ "$TAG" =~ $SEMVER ]]; then |
| 92 | + echo "::error::Tag '$TAG' is not v{major}.{minor}.{patch} or v{major}.{minor}.{patch}-{pre-release}, e.g. v2.13.1 or v2.14.0-beta.1" |
| 93 | + exit 1 |
| 94 | + fi |
| 95 | + if [ "$GITHUB_REF" != "refs/tags/$TAG" ]; then |
| 96 | + echo "::error::The run is on $GITHUB_REF, not on the release tag refs/tags/$TAG" |
| 97 | + exit 1 |
| 98 | + fi |
| 99 | + VERSION="${TAG#v}" |
| 100 | + SHA=$(git rev-parse "refs/tags/$TAG^{commit}") |
| 101 | + if [ "$(git rev-parse HEAD)" != "$SHA" ]; then |
| 102 | + echo "::error::Checked out $(git rev-parse HEAD), but $TAG points at $SHA" |
| 103 | + exit 1 |
| 104 | + fi |
| 105 | +
|
| 106 | + # The GitHub "pre-release" tick and the version must agree, so a preview cannot pass as a |
| 107 | + # stable release by a forgotten tick, and a stable version cannot hide as a pre-release. |
| 108 | + if [[ "$VERSION" == *-* ]]; then |
| 109 | + if [ "$PRERELEASE" != true ]; then |
| 110 | + echo "::error::$TAG is a pre-release version, so the GitHub Release must be marked as a pre-release (gh release create --prerelease)" |
| 111 | + exit 1 |
| 112 | + fi |
| 113 | + else |
| 114 | + if [ "$PRERELEASE" = true ]; then |
| 115 | + echo "::error::$TAG is a stable version, but the GitHub Release is marked as a pre-release. Use a pre-release version such as ${TAG}-beta.1, or untick pre-release." |
| 116 | + exit 1 |
| 117 | + fi |
| 118 | + if ! git merge-base --is-ancestor "$SHA" origin/main; then |
| 119 | + echo "::error::$TAG ($SHA) is not on main. Stable versions are released from main only; release a branch as a pre-release instead." |
| 120 | + exit 1 |
| 121 | + fi |
| 122 | + fi |
| 123 | +
|
| 124 | + # Maven Central never forgets a version. (The Portal would also reject the upload, but only |
| 125 | + # after the tests and the signing.) |
| 126 | + POM="https://repo1.maven.org/maven2/$GROUP_PATH/$ARTIFACT/$VERSION/$ARTIFACT-$VERSION.pom" |
| 127 | + CODE=$(curl -sS -o /dev/null -w '%{http_code}' --retry 3 "$POM" || true) |
| 128 | + if [ "$CODE" = 200 ]; then |
| 129 | + echo "::error::$ARTIFACT $VERSION already exists on Maven Central and can never be published again. Release the next version instead." |
| 130 | + exit 1 |
| 131 | + elif [ "$CODE" != 404 ]; then |
| 132 | + echo "::error::Could not check whether $VERSION is on Maven Central ($POM answered $CODE)" |
| 133 | + exit 1 |
| 134 | + fi |
| 135 | +
|
| 136 | + { |
| 137 | + echo "version=$VERSION" |
| 138 | + echo "sha=$SHA" |
| 139 | + } >> "$GITHUB_OUTPUT" |
| 140 | + echo "Publishing $ARTIFACT $VERSION from $SHA" |
| 141 | +
|
| 142 | + # Writes ~/.m2/settings.xml with the server `central` (credentials read from the environment at |
| 143 | + # run time) and imports the signing key into a temporary keyring that is deleted after the job. |
| 144 | + # No dependency cache: a publishing job must not restore anything another run could have written. |
| 145 | + - uses: actions/setup-java@b843ebf9b913146b38da2eaa7aeddf76074ae1ad # v6.1.0 |
| 146 | + with: |
| 147 | + distribution: temurin |
| 148 | + java-version: 17 |
| 149 | + server-id: central |
| 150 | + server-username-env-var: CENTRAL_TOKEN_USERNAME |
| 151 | + server-password-env-var: CENTRAL_TOKEN_PASSWORD |
| 152 | + gpg-private-key: ${{ secrets.GPG_PRIVATE_KEY }} |
| 153 | + gpg-passphrase-env-var: MAVEN_GPG_PASSPHRASE |
| 154 | + |
| 155 | + - name: Set the version |
| 156 | + env: |
| 157 | + VERSION: ${{ steps.tag.outputs.version }} |
| 158 | + run: | |
| 159 | + set -euo pipefail |
| 160 | + ./mvnw versions:set -DnewVersion="$VERSION" -DgenerateBackupPoms=false |
| 161 | + [ "$(./mvnw -q help:evaluate -Dexpression=project.version -DforceStdout)" = "$VERSION" ] || { echo "::error::pom.xml does not carry version $VERSION"; exit 1; } |
| 162 | +
|
| 163 | + - name: Test |
| 164 | + env: |
| 165 | + CONVERT_API_SECRET: ${{ secrets.CONVERTAPI_STAGING_TOKEN }} |
| 166 | + # Surefire reruns a failed test twice; the staging API also answers 503 in bursts of up to a |
| 167 | + # minute, so the tests get up to three attempts, 30 s apart. |
| 168 | + run: | |
| 169 | + for attempt in 1 2 3; do |
| 170 | + ./mvnw verify && exit 0 |
| 171 | + echo "::warning::Attempt $attempt: the tests failed, see above" |
| 172 | + sleep 30 |
| 173 | + done |
| 174 | + echo "::error::The tests failed three times" |
| 175 | + exit 1 |
| 176 | +
|
| 177 | + # Signs the jar, the sources, the javadoc and the pom, uploads them as one deployment, and waits |
| 178 | + # until the Central Portal has validated and published it (autoPublish, waitUntil=published). |
| 179 | + # No retry: a version that already exists must fail loudly, never pass as published. |
| 180 | + - name: Publish to Maven Central |
| 181 | + env: |
| 182 | + CENTRAL_TOKEN_USERNAME: ${{ secrets.CENTRAL_TOKEN_USERNAME }} |
| 183 | + CENTRAL_TOKEN_PASSWORD: ${{ secrets.CENTRAL_TOKEN_PASSWORD }} |
| 184 | + MAVEN_GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }} |
| 185 | + run: ./mvnw -Prelease deploy -DskipTests |
| 186 | + |
| 187 | + # The Portal marks a deployment PUBLISHED before repo1.maven.org serves it, sometimes by half an |
| 188 | + # hour. A slow mirror is not a failed release, so this only warns. |
| 189 | + - name: Wait for Maven Central to serve the version |
| 190 | + env: |
| 191 | + VERSION: ${{ steps.tag.outputs.version }} |
| 192 | + run: | |
| 193 | + POM="https://repo1.maven.org/maven2/$GROUP_PATH/$ARTIFACT/$VERSION/$ARTIFACT-$VERSION.pom" |
| 194 | + for _ in $(seq 1 40); do |
| 195 | + if curl -fsS -o /dev/null "$POM"; then |
| 196 | + echo "$ARTIFACT $VERSION is available on Maven Central" |
| 197 | + exit 0 |
| 198 | + fi |
| 199 | + sleep 30 |
| 200 | + done |
| 201 | + echo "::warning::$ARTIFACT $VERSION was published but repo1.maven.org has not served it after 20 minutes. Check https://central.sonatype.com/artifact/com.convertapi.client/convertapi/$VERSION" |
| 202 | +
|
| 203 | + - name: Summary |
| 204 | + env: |
| 205 | + VERSION: ${{ steps.tag.outputs.version }} |
| 206 | + SHA: ${{ steps.tag.outputs.sha }} |
| 207 | + run: | |
| 208 | + { |
| 209 | + echo "| | |" |
| 210 | + echo "|---|---|" |
| 211 | + echo "| Package | [\`com.convertapi.client:convertapi:$VERSION\`](https://central.sonatype.com/artifact/com.convertapi.client/convertapi/$VERSION) |" |
| 212 | + echo "| Commit | \`$SHA\` |" |
| 213 | + echo "| Published with | Central Portal user token, GPG-signed |" |
| 214 | + } >> "$GITHUB_STEP_SUMMARY" |
0 commit comments