Skip to content

Commit 2407583

Browse files
authored
Merge pull request #49 from ConvertAPI/ci/trunk-based-release
Trunk-based release: build on JDK 17/21/25, publish to Maven Central from a GitHub Release
2 parents cc1a2f7 + 54d62b4 commit 2407583

20 files changed

Lines changed: 1330 additions & 183 deletions

File tree

‎.github/dependabot.yml‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Weekly dependency updates, each opened as a pull request against main that must pass the `build` check
2+
# and is merged like any other change (merge commit). Merging releases nothing: a new Maven Central
3+
# version still needs a GitHub Release (see RELEASING.md).
4+
version: 2
5+
updates:
6+
# The workflows pin every action to a commit SHA with a `# vX.Y.Z` comment; Dependabot updates both.
7+
- package-ecosystem: github-actions
8+
directory: /
9+
schedule:
10+
interval: weekly
11+
day: monday
12+
groups:
13+
github-actions:
14+
patterns: ["*"]
15+
commit-message:
16+
prefix: ci
17+
labels: [dependencies]
18+
19+
# gson and okhttp bumps raise the version every consumer gets, so they belong in the CHANGELOG of the
20+
# next release. Plugin and test bumps only affect the build. Major versions are left to a person:
21+
# okhttp 5 and JUnit 6 drop Java 8, which the library still supports.
22+
- package-ecosystem: maven
23+
directory: /
24+
schedule:
25+
interval: weekly
26+
day: monday
27+
groups:
28+
maven-minor-and-patch:
29+
update-types: [minor, patch]
30+
ignore:
31+
- dependency-name: "*"
32+
update-types: [version-update:semver-major]
33+
commit-message:
34+
prefix: deps
35+
labels: [dependencies]
36+
open-pull-requests-limit: 5

‎.github/workflows/build.yml‎

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
name: Build
2+
run-name: >-
3+
${{ github.event_name == 'pull_request' && format('Checking that #{0} builds', github.event.pull_request.number)
4+
|| format('Building {0}', github.ref_name) }}
5+
6+
# Trunk-based: main is the only long-lived branch. Every pull request and every push to main compiles the
7+
# library for Java 8, tests it, builds the javadoc and sources jars and the examples on each supported JDK,
8+
# but nothing is published here: Maven Central versions are permanent, so a release is a deliberate act -
9+
# publishing a GitHub Release v{semver}, which runs publish.yml. The required status check is the last
10+
# job's name, `build`, which passes only when every JDK passed.
11+
on:
12+
pull_request:
13+
branches: [main]
14+
push:
15+
branches: [main]
16+
workflow_dispatch:
17+
18+
permissions:
19+
contents: read
20+
21+
# A newer push to the same pull request cancels the older build; builds of main never cancel each other.
22+
concurrency:
23+
group: build-${{ github.event_name == 'pull_request' && format('pr-{0}', github.event.pull_request.number) || github.run_id }}
24+
cancel-in-progress: true
25+
26+
env:
27+
# The live tests convert real files against the staging API with a staging token. Without the token
28+
# (a pull request from a fork gets no secrets) they are skipped, and the offline tests still run.
29+
CONVERT_API_BASE_URI: https://stag-v2.convertapi.com/
30+
MAVEN_ARGS: --batch-mode --no-transfer-progress
31+
32+
jobs:
33+
test:
34+
name: test (JDK ${{ matrix.java }})
35+
runs-on: ubuntu-latest
36+
timeout-minutes: 20
37+
strategy:
38+
fail-fast: false
39+
matrix:
40+
java: [17, 21, 25]
41+
steps:
42+
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
43+
with:
44+
persist-credentials: false
45+
46+
- uses: actions/setup-java@b843ebf9b913146b38da2eaa7aeddf76074ae1ad # v6.1.0
47+
with:
48+
distribution: temurin
49+
java-version: ${{ matrix.java }}
50+
cache: maven
51+
52+
- name: Test, javadoc and package
53+
env:
54+
CONVERT_API_SECRET: ${{ secrets.CONVERTAPI_STAGING_TOKEN }}
55+
run: |
56+
if [ -z "$CONVERT_API_SECRET" ]; then
57+
echo "::warning::The CONVERTAPI_STAGING_TOKEN secret is not available to this run (a pull request from a fork gets no secrets), so the live API tests are skipped"
58+
fi
59+
# Surefire reruns a failed test twice; the staging API also answers 503 (no healthy upstream) in
60+
# bursts of up to a minute, so the whole build gets up to three attempts, 30 s apart.
61+
for attempt in 1 2 3; do
62+
./mvnw install && exit 0
63+
echo "::warning::Attempt $attempt: the build failed, see above"
64+
sleep 30
65+
done
66+
echo "::error::The build failed three times"
67+
exit 1
68+
69+
# The examples are not run (they need input files and write to the temp directory), only compiled
70+
# against the library just built, so an API change that breaks them fails here.
71+
- name: Compile the examples
72+
run: ./mvnw -f examples/pom.xml -Dconvertapi.version=0.0.0-SNAPSHOT compile
73+
74+
build:
75+
name: build
76+
needs: test
77+
# Runs even when a test job failed, so that the required check reports a failure instead of being
78+
# skipped (a skipped required check counts as passed).
79+
if: ${{ !cancelled() }}
80+
runs-on: ubuntu-latest
81+
timeout-minutes: 15
82+
steps:
83+
- name: Check every JDK passed
84+
env:
85+
RESULT: ${{ needs.test.result }}
86+
run: |
87+
if [ "$RESULT" != success ]; then
88+
echo "::error::The tests did not pass on every JDK ($RESULT)"
89+
exit 1
90+
fi
91+
92+
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
93+
with:
94+
persist-credentials: false
95+
96+
- uses: actions/setup-java@b843ebf9b913146b38da2eaa7aeddf76074ae1ad # v6.1.0
97+
with:
98+
distribution: temurin
99+
java-version: 17
100+
cache: maven
101+
102+
# A pre-release version that sorts below any real release and names the run, so a jar downloaded
103+
# from this run can be tried out but never mistaken for a published one. The tests ran above.
104+
- name: Package
105+
id: package
106+
env:
107+
RUN: ${{ github.run_number }}
108+
SHORT_SHA: ${{ github.event.pull_request.head.sha || github.sha }}
109+
run: |
110+
set -euo pipefail
111+
VERSION="0.0.0-ci.$RUN.g${SHORT_SHA:0:7}"
112+
./mvnw versions:set -DnewVersion="$VERSION" -DgenerateBackupPoms=false
113+
./mvnw package -DskipTests
114+
for f in "convertapi-$VERSION.jar" "convertapi-$VERSION-sources.jar" "convertapi-$VERSION-javadoc.jar"; do
115+
test -f "target/$f" || { echo "::error::target/$f was not built"; exit 1; }
116+
done
117+
# Class file major version 52 = Java 8, which the library still supports.
118+
MAJOR=$(unzip -p "target/convertapi-$VERSION.jar" com/convertapi/client/ConvertApi.class | od -An -j7 -N1 -tu1 | tr -d ' ')
119+
[ "$MAJOR" = 52 ] || { echo "::error::The classes are compiled for class file version $MAJOR, not 52 (Java 8)"; exit 1; }
120+
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
121+
122+
- uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
123+
with:
124+
name: convertapi-${{ steps.package.outputs.version }}
125+
path: |
126+
target/convertapi-*.jar
127+
pom.xml
128+
retention-days: 14
129+
if-no-files-found: error
130+
131+
- name: Summary
132+
env:
133+
VERSION: ${{ steps.package.outputs.version }}
134+
run: |
135+
{
136+
echo "| | |"
137+
echo "|---|---|"
138+
echo "| Package | \`com.convertapi.client:convertapi:$VERSION\` (artifact only, not published) |"
139+
echo "| Runs on | Java 8 and later (built and tested on JDK 17, 21 and 25) |"
140+
} >> "$GITHUB_STEP_SUMMARY"

‎.github/workflows/publish.yml‎

Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
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"

‎.gitignore‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.idea
2+
*.iml
23
out
3-
/target/
4+
target/

0 commit comments

Comments
 (0)