Skip to content

Publish to Maven Central #19

Publish to Maven Central

Publish to Maven Central #19

Workflow file for this run

name: Publish to Maven Central
on:
workflow_run:
workflows: ["Java CI", "E2E Tests"]
types: [completed]
branches: [master]
permissions:
contents: read
actions: read
# Prevent duplicate publish attempts for the same commit
concurrency:
group: publish-${{ github.event.workflow_run.head_sha }}
cancel-in-progress: false
jobs:
publish:
runs-on: ubuntu-latest
# Only consider successful completions on master from push events
if: >
github.event.workflow_run.conclusion == 'success' &&
github.event.workflow_run.head_branch == 'master' &&
github.event.workflow_run.event == 'push'
steps:
- name: Gate on Java CI + E2E success for same commit
id: gate
uses: actions/github-script@v7
with:
script: |
const sha = context.payload.workflow_run.head_sha;
console.log(`Checking workflow status for commit: ${sha}`);
async function getWorkflowStatus(workflowFile) {
const runs = await github.rest.actions.listWorkflowRuns({
owner: context.repo.owner,
repo: context.repo.repo,
workflow_id: workflowFile,
head_sha: sha,
per_page: 20,
});
const anySuccess = runs.data.workflow_runs.some(r => r.conclusion === 'success');
const anyInProgress = runs.data.workflow_runs.some(r => r.status !== 'completed');
return { anySuccess, anyInProgress, count: runs.data.workflow_runs.length };
}
const build = await getWorkflowStatus('build.yml');
const e2e = await getWorkflowStatus('e2e.yml');
console.log(`Java CI: success=${build.anySuccess}, inProgress=${build.anyInProgress}, runs=${build.count}`);
console.log(`E2E Tests: success=${e2e.anySuccess}, inProgress=${e2e.anyInProgress}, runs=${e2e.count}`);
if (!build.anySuccess || !e2e.anySuccess) {
// Not ready yet - exit gracefully so the next workflow completion can trigger publish
if (build.anyInProgress || e2e.anyInProgress) {
core.notice(`Not publishing yet for ${sha} - workflows still in progress. buildSuccess=${build.anySuccess}, e2eSuccess=${e2e.anySuccess}`);
} else {
core.notice(`Not publishing for ${sha} - one or more workflows failed. buildSuccess=${build.anySuccess}, e2eSuccess=${e2e.anySuccess}`);
}
core.setOutput('ready', 'false');
return;
}
console.log(`Both workflows passed for ${sha} - proceeding with publish`);
core.setOutput('ready', 'true');
- uses: actions/checkout@v4
if: steps.gate.outputs.ready == 'true'
with:
# Checkout the exact commit that was tested, not latest master
ref: ${{ github.event.workflow_run.head_sha }}
# Set up JDK 17 for Fabric 1.20.x builds (Gradle toolchain will use this)
- name: Set up JDK 17
if: steps.gate.outputs.ready == 'true'
uses: actions/setup-java@v4
with:
java-version: 17
distribution: "temurin"
# Set up JDK 21 as the primary JDK
- name: Set up JDK 21
if: steps.gate.outputs.ready == 'true'
uses: actions/setup-java@v4
with:
java-version: 21
distribution: "temurin"
- name: Setup Gradle
if: steps.gate.outputs.ready == 'true'
uses: gradle/actions/setup-gradle@v4
with:
cache-read-only: true
- name: Cache Loom
if: steps.gate.outputs.ready == 'true'
uses: actions/cache@v4
with:
path: |
.gradle/loom-cache
key: ${{ runner.os }}-loom-${{ hashFiles('**/libs.versions.*', '**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: ${{ runner.os }}-loom-
- name: Publish to Maven Central
if: steps.gate.outputs.ready == 'true'
env:
ORG_GRADLE_PROJECT_mavenCentralUsername: ${{ secrets.ORG_GRADLE_PROJECT_SONATYPEUSERNAME }}
ORG_GRADLE_PROJECT_mavenCentralPassword: ${{ secrets.ORG_GRADLE_PROJECT_SONATYPEPASSWORD }}
ORG_GRADLE_PROJECT_signingInMemoryKey: ${{ secrets.ORG_GRADLE_PROJECT_SIGNINGINMEMORYKEY }}
ORG_GRADLE_PROJECT_signingInMemoryKeyPassword: ${{ secrets.ORG_GRADLE_PROJECT_SIGNINGINMEMORYKEYPASSWORD }}
run: |
VERSION=$(grep "^version=" gradle.properties | cut -d'=' -f2)
echo "Publishing version: $VERSION (commit: ${{ github.event.workflow_run.head_sha }})"
if [[ "$VERSION" == *"SNAPSHOT"* ]]; then
# SNAPSHOT: publish to Central Portal snapshot repository
./gradlew publishToMavenCentral
else
# Release: publish and release to Maven Central
./gradlew publishAndReleaseToMavenCentral
fi