|
| 1 | +name: SDK Release |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main] |
| 6 | + paths-ignore: |
| 7 | + - '.github/**' |
| 8 | + workflow_dispatch: |
| 9 | + |
| 10 | +permissions: |
| 11 | + contents: read |
| 12 | + |
| 13 | +concurrency: |
| 14 | + group: sdk-release |
| 15 | + cancel-in-progress: false |
| 16 | + |
| 17 | +jobs: |
| 18 | + release: |
| 19 | + name: Publish com.cloudpdf:sdk |
| 20 | + if: github.event_name == 'workflow_dispatch' || vars.SDK_AUTO_PUBLISH_ENABLED == 'true' |
| 21 | + runs-on: ubuntu-latest |
| 22 | + environment: release |
| 23 | + permissions: |
| 24 | + contents: write |
| 25 | + steps: |
| 26 | + - uses: actions/checkout@v4 |
| 27 | + with: |
| 28 | + fetch-depth: 0 |
| 29 | + |
| 30 | + - uses: actions/setup-java@v4 |
| 31 | + with: |
| 32 | + distribution: temurin |
| 33 | + java-version: '17' |
| 34 | + cache: gradle |
| 35 | + |
| 36 | + - name: Verify generated release version |
| 37 | + id: version |
| 38 | + run: | |
| 39 | + node <<'NODE' |
| 40 | + const fs = require('node:fs'); |
| 41 | + const generation = JSON.parse(fs.readFileSync('cloudpdf-generation.json', 'utf8')); |
| 42 | + const build = fs.readFileSync('build.gradle', 'utf8'); |
| 43 | + if (generation.language !== 'java') throw new Error('generation language is not java'); |
| 44 | + const group = build.match(/^group = '([^']+)'$/m)?.[1]; |
| 45 | + const projectVersion = build.match(/^version = '([^']+)'$/m)?.[1]; |
| 46 | + const artifact = build.match(/^\s*artifactId = '([^']+)'$/m)?.[1]; |
| 47 | + if (group !== 'com.cloudpdf' || artifact !== 'sdk') { |
| 48 | + throw new Error(`unexpected Maven coordinates ${group}:${artifact}`); |
| 49 | + } |
| 50 | + if (projectVersion !== generation.sdkVersion) { |
| 51 | + throw new Error(`project version ${projectVersion} does not match generated version ${generation.sdkVersion}`); |
| 52 | + } |
| 53 | + const prerelease = generation.canonicalVersion.includes('-'); |
| 54 | + fs.appendFileSync(process.env.GITHUB_OUTPUT, `version=${projectVersion}\n`); |
| 55 | + fs.appendFileSync(process.env.GITHUB_OUTPUT, `tag=v${projectVersion}\n`); |
| 56 | + fs.appendFileSync(process.env.GITHUB_OUTPUT, `prerelease=${prerelease}\n`); |
| 57 | + NODE |
| 58 | +
|
| 59 | + - name: Build signed Maven Central bundle |
| 60 | + env: |
| 61 | + MAVEN_GPG_PRIVATE_KEY: ${{ secrets.MAVEN_GPG_PRIVATE_KEY }} |
| 62 | + MAVEN_GPG_PASSPHRASE: ${{ secrets.MAVEN_GPG_PASSPHRASE }} |
| 63 | + VERSION: ${{ steps.version.outputs.version }} |
| 64 | + CENTRAL_BUNDLE: ${{ runner.temp }}/cloudpdf-central-bundle.zip |
| 65 | + run: | |
| 66 | + ./gradlew clean test publishMavenPublicationToCentralStagingRepository --no-daemon |
| 67 | + release_directory="build/central-staging/com/cloudpdf/sdk/$VERSION" |
| 68 | + required_artifacts=( |
| 69 | + "sdk-$VERSION.pom" |
| 70 | + "sdk-$VERSION.jar" |
| 71 | + "sdk-$VERSION-sources.jar" |
| 72 | + "sdk-$VERSION-javadoc.jar" |
| 73 | + ) |
| 74 | + for artifact in "${required_artifacts[@]}"; do |
| 75 | + if [ ! -f "$release_directory/$artifact" ]; then |
| 76 | + echo "::error::Maven Central bundle is missing $artifact." |
| 77 | + exit 1 |
| 78 | + fi |
| 79 | + if [ ! -f "$release_directory/$artifact.asc" ]; then |
| 80 | + echo "::error::Maven artifact $artifact is not signed." |
| 81 | + exit 1 |
| 82 | + fi |
| 83 | + done |
| 84 | +
|
| 85 | + python3 <<'PY' |
| 86 | + import os |
| 87 | + import xml.etree.ElementTree as ET |
| 88 | +
|
| 89 | + version = os.environ['VERSION'] |
| 90 | + pom = ET.parse(f'build/central-staging/com/cloudpdf/sdk/{version}/sdk-{version}.pom').getroot() |
| 91 | + namespace = {'m': 'http://maven.apache.org/POM/4.0.0'} |
| 92 | + expected = { |
| 93 | + 'm:groupId': 'com.cloudpdf', |
| 94 | + 'm:artifactId': 'sdk', |
| 95 | + 'm:version': version, |
| 96 | + 'm:name': 'CloudPDF', |
| 97 | + 'm:url': 'https://www.cloudpdf.com', |
| 98 | + 'm:scm/m:url': 'https://github.com/embedpdf/cloudpdf-sdk-java', |
| 99 | + } |
| 100 | + for path, value in expected.items(): |
| 101 | + actual = pom.findtext(path, namespaces=namespace) |
| 102 | + if actual != value: |
| 103 | + raise RuntimeError(f'POM {path} is {actual!r}, expected {value!r}') |
| 104 | + PY |
| 105 | +
|
| 106 | + bundle_root="$RUNNER_TEMP/cloudpdf-central-bundle" |
| 107 | + bundle_release="$bundle_root/com/cloudpdf/sdk/$VERSION" |
| 108 | + mkdir -p "$bundle_release" |
| 109 | + for artifact in "${required_artifacts[@]}"; do |
| 110 | + cp "$release_directory/$artifact" "$bundle_release/$artifact" |
| 111 | + cp "$release_directory/$artifact.asc" "$bundle_release/$artifact.asc" |
| 112 | + md5sum "$release_directory/$artifact" | awk '{ print $1 }' > "$bundle_release/$artifact.md5" |
| 113 | + sha1sum "$release_directory/$artifact" | awk '{ print $1 }' > "$bundle_release/$artifact.sha1" |
| 114 | + done |
| 115 | +
|
| 116 | + cd "$bundle_root" |
| 117 | + zip -q -r "$CENTRAL_BUNDLE" com |
| 118 | +
|
| 119 | + - name: Protect immutable release tag |
| 120 | + id: release-tag |
| 121 | + env: |
| 122 | + RELEASE_TAG: ${{ steps.version.outputs.tag }} |
| 123 | + run: | |
| 124 | + git fetch --force --tags origin |
| 125 | + if git rev-parse --quiet --verify "refs/tags/$RELEASE_TAG" >/dev/null; then |
| 126 | + tagged_commit="$(git rev-list -n 1 "$RELEASE_TAG")" |
| 127 | + if [ "$tagged_commit" != "$GITHUB_SHA" ]; then |
| 128 | + if git diff --quiet "$RELEASE_TAG" "$GITHUB_SHA" -- . ':(exclude).github/**'; then |
| 129 | + echo "::notice::Reusing $RELEASE_TAG after a workflow-only change." |
| 130 | + else |
| 131 | + echo "::error::Release tag $RELEASE_TAG already points to different package source at $tagged_commit; bump the SDK version." |
| 132 | + exit 1 |
| 133 | + fi |
| 134 | + fi |
| 135 | + echo "existed=true" >> "$GITHUB_OUTPUT" |
| 136 | + else |
| 137 | + git config user.name cloudpdf-sdk-bot |
| 138 | + git config user.email hello@cloudpdf.com |
| 139 | + git tag -a "$RELEASE_TAG" -m "CloudPDF Java SDK $RELEASE_TAG" |
| 140 | + git push origin "refs/tags/$RELEASE_TAG" |
| 141 | + echo "existed=false" >> "$GITHUB_OUTPUT" |
| 142 | + fi |
| 143 | +
|
| 144 | + - name: Check Maven Central publication state |
| 145 | + id: registry |
| 146 | + env: |
| 147 | + VERSION: ${{ steps.version.outputs.version }} |
| 148 | + TAG_EXISTED: ${{ steps.release-tag.outputs.existed }} |
| 149 | + run: | |
| 150 | + package_url="https://repo1.maven.org/maven2/com/cloudpdf/sdk/$VERSION/sdk-$VERSION.pom" |
| 151 | + http_status="$(curl --silent --show-error --output /dev/null --write-out '%{http_code}' "$package_url")" |
| 152 | + case "$http_status" in |
| 153 | + 200) |
| 154 | + if [ "$TAG_EXISTED" != 'true' ]; then |
| 155 | + echo "::error::com.cloudpdf:sdk:$VERSION exists on Maven Central but its release tag did not exist." |
| 156 | + exit 1 |
| 157 | + fi |
| 158 | + echo "publish=false" >> "$GITHUB_OUTPUT" |
| 159 | + ;; |
| 160 | + 404) |
| 161 | + echo "publish=true" >> "$GITHUB_OUTPUT" |
| 162 | + ;; |
| 163 | + *) |
| 164 | + echo "::error::Maven Central returned HTTP $http_status while checking com.cloudpdf:sdk:$VERSION." |
| 165 | + exit 1 |
| 166 | + ;; |
| 167 | + esac |
| 168 | +
|
| 169 | + - name: Upload to Maven Central |
| 170 | + if: steps.registry.outputs.publish == 'true' |
| 171 | + id: central |
| 172 | + env: |
| 173 | + MAVEN_CENTRAL_USERNAME: ${{ secrets.MAVEN_CENTRAL_USERNAME }} |
| 174 | + MAVEN_CENTRAL_PASSWORD: ${{ secrets.MAVEN_CENTRAL_PASSWORD }} |
| 175 | + CENTRAL_BUNDLE: ${{ runner.temp }}/cloudpdf-central-bundle.zip |
| 176 | + run: | |
| 177 | + central_auth="$(printf '%s:%s' "$MAVEN_CENTRAL_USERNAME" "$MAVEN_CENTRAL_PASSWORD" | base64 --wrap=0)" |
| 178 | + echo "::add-mask::$central_auth" |
| 179 | + deployment_id="$(curl --fail-with-body --silent --show-error \ |
| 180 | + --request POST \ |
| 181 | + --header "Authorization: Bearer $central_auth" \ |
| 182 | + --form "bundle=@$CENTRAL_BUNDLE;type=application/octet-stream" \ |
| 183 | + 'https://central.sonatype.com/api/v1/publisher/upload?publishingType=AUTOMATIC')" |
| 184 | + if ! printf '%s' "$deployment_id" | grep -Eq '^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$'; then |
| 185 | + echo "::error::Maven Central returned an invalid deployment ID." |
| 186 | + exit 1 |
| 187 | + fi |
| 188 | + echo "deployment_id=$deployment_id" >> "$GITHUB_OUTPUT" |
| 189 | +
|
| 190 | + - name: Wait for Maven Central publication |
| 191 | + if: steps.registry.outputs.publish == 'true' |
| 192 | + env: |
| 193 | + MAVEN_CENTRAL_USERNAME: ${{ secrets.MAVEN_CENTRAL_USERNAME }} |
| 194 | + MAVEN_CENTRAL_PASSWORD: ${{ secrets.MAVEN_CENTRAL_PASSWORD }} |
| 195 | + DEPLOYMENT_ID: ${{ steps.central.outputs.deployment_id }} |
| 196 | + run: | |
| 197 | + central_auth="$(printf '%s:%s' "$MAVEN_CENTRAL_USERNAME" "$MAVEN_CENTRAL_PASSWORD" | base64 --wrap=0)" |
| 198 | + echo "::add-mask::$central_auth" |
| 199 | + status_file="$(mktemp)" |
| 200 | + for attempt in $(seq 1 120); do |
| 201 | + curl --fail-with-body --silent --show-error \ |
| 202 | + --request POST \ |
| 203 | + --header "Authorization: Bearer $central_auth" \ |
| 204 | + --output "$status_file" \ |
| 205 | + "https://central.sonatype.com/api/v1/publisher/status?id=$DEPLOYMENT_ID" |
| 206 | + deployment_state="$(jq --raw-output '.deploymentState' "$status_file")" |
| 207 | + case "$deployment_state" in |
| 208 | + PUBLISHED) |
| 209 | + exit 0 |
| 210 | + ;; |
| 211 | + FAILED) |
| 212 | + cat "$status_file" |
| 213 | + echo "::error::Maven Central rejected deployment $DEPLOYMENT_ID." |
| 214 | + exit 1 |
| 215 | + ;; |
| 216 | + PENDING|VALIDATING|VALIDATED|PUBLISHING) |
| 217 | + echo "Maven Central deployment is $deployment_state (attempt $attempt/120)." |
| 218 | + ;; |
| 219 | + *) |
| 220 | + cat "$status_file" |
| 221 | + echo "::error::Maven Central returned unexpected deployment state $deployment_state." |
| 222 | + exit 1 |
| 223 | + ;; |
| 224 | + esac |
| 225 | + sleep 10 |
| 226 | + done |
| 227 | + echo "::error::Maven Central deployment $DEPLOYMENT_ID did not finish within 20 minutes." |
| 228 | + exit 1 |
| 229 | +
|
| 230 | + - name: Create GitHub release |
| 231 | + env: |
| 232 | + GH_TOKEN: ${{ github.token }} |
| 233 | + RELEASE_TAG: ${{ steps.version.outputs.tag }} |
| 234 | + PRERELEASE: ${{ steps.version.outputs.prerelease }} |
| 235 | + run: | |
| 236 | + if gh release view "$RELEASE_TAG" >/dev/null 2>&1; then |
| 237 | + exit 0 |
| 238 | + fi |
| 239 | + args=(--verify-tag --generate-notes --title "CloudPDF Java SDK $RELEASE_TAG") |
| 240 | + if [ "$PRERELEASE" = 'true' ]; then args+=(--prerelease); fi |
| 241 | + gh release create "$RELEASE_TAG" "${args[@]}" |
0 commit comments