diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index aacaf60..3244a8d 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -4,6 +4,7 @@ on: push: branches: - main + pull_request: workflow_dispatch: permissions: @@ -16,11 +17,26 @@ concurrency: jobs: publish: runs-on: ubuntu-latest - if: github.repository == 'JabRef/html-to-node' + # Fork PRs get no secrets, so they can never publish - don't even start the job for them + if: github.repository == 'JabRef/html-to-node' && (github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository) steps: - uses: actions/checkout@v7 with: show-progress: 'false' + # Publish the branch as-is, not GitHub's main-merged ref, and keep enough + # history for the is-up-to-date check below + ref: ${{ github.event.pull_request.head.sha || github.sha }} + fetch-depth: 0 + - name: Skip if branch is behind main + id: uptodate + # A stale branch would publish a snapshot missing commits that are already on + # main, silently downgrading the artifact for everyone consuming it + run: | + git fetch --no-tags origin main + if [ "${{ github.event_name }}" = "pull_request" ] && ! git merge-base --is-ancestor FETCH_HEAD HEAD; then + echo "Branch is behind main - rebase/merge main before a snapshot is published." + echo "skip=true" >> "$GITHUB_OUTPUT" + fi - name: Setup JDK uses: actions/setup-java@v5 with: @@ -45,11 +61,14 @@ jobs: mavenCentralPassword=${{ secrets.KOPPOR_MAVENCENTRALPASSWORD }} EOF - name: Publish snapshot + if: steps.uptodate.outputs.skip != 'true' # Release versions are published by release.yml on tag push; this workflow only # handles the rolling snapshot run: | - if ! grep -q 'version = ".*-SNAPSHOT"' build.gradle.kts; then + if ! grep -q '^version = .*SNAPSHOT' build.gradle.kts; then echo "Version is not a SNAPSHOT - skipping (releases go through release.yml)." exit 0 fi - ./gradlew publishAllPublicationsToMavenCentralRepository + ./gradlew ${PR_NUMBER:+-PversionSuffix=PR$PR_NUMBER} publishAllPublicationsToMavenCentralRepository + env: + PR_NUMBER: ${{ github.event.pull_request.number }} diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index e2bd4a1..ac2ce98 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -45,7 +45,7 @@ jobs: EOF - name: Guard against SNAPSHOT releases run: | - if grep -q 'version = ".*-SNAPSHOT"' build.gradle.kts; then + if grep -q '^version = .*SNAPSHOT' build.gradle.kts; then echo "Tagged commit still has a SNAPSHOT version - refusing to release." exit 1 fi diff --git a/build.gradle.kts b/build.gradle.kts index 1ad37e9..8964eb5 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -8,7 +8,9 @@ plugins { } group = "org.jabref" -version = "0.2.0-SNAPSHOT" +// -PversionSuffix=PR17 turns 0.2.0-SNAPSHOT into 0.2.0-PR17-SNAPSHOT, so a pull request +// snapshot is identifiable and does not clobber the one built from main +version = "0.2.0" + (findProperty("versionSuffix")?.let { "-$it" } ?: "") + "-SNAPSHOT" repositories { mavenCentral()