Skip to content

Commit

Permalink
Split out snapshot workflow from release workflow and make them reusable
Browse files Browse the repository at this point in the history
  • Loading branch information
markpatton committed Dec 12, 2023
1 parent 1d10f29 commit 5fb9e8f
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 61 deletions.
137 changes: 76 additions & 61 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: "Publish: manual full release OR automatic snapshot"
name: "Publish: manual full release"

on:
workflow_dispatch:
Expand All @@ -9,36 +9,23 @@ on:
nextversion:
description: 'Next dev version'
required: true
push:
branches:
- 'main'
workflow_call:
inputs:
releaseversion:
description: 'Release version'
required: true
type: string
nextversion:
description: 'Next dev version'
required: true
type: string

jobs:
setup:
runs-on: ubuntu-latest
outputs:
# Output project version from the POM to conditionally run dependent steps
project-version: ${{ steps.project_version.outputs.version }}
steps:
- name: Checkout latest code
uses: actions/checkout@v3

- name: Setup Java & Maven
uses: actions/setup-java@v3
with:
java-version: 17
distribution: 'temurin'
cache: 'maven'

- name: Get project version from POM
id: project_version
run: echo "VERSION=`mvn help:evaluate -Dexpression=project.version -q -DforceStdout`" >> $GITHUB_OUTPUT

# Run only if project POM has version ending in "-SNAPSHOT"
snapshot:
needs: setup
if: github.event_name == 'push' && endsWith(needs.setup.outputs.project-version, '-SNAPSHOT')
runs-on: ubuntu-latest
steps:
- name: Checkout latest code
uses: actions/checkout@v3
Expand All @@ -55,64 +42,92 @@ jobs:
gpg-private-key: ${{ secrets.MAVEN_GPG_KEY }}
gpg-passphrase: MAVEN_GPG_PASSPHRASE

- name: Publish SNAPSHOT
run: mvn -B --no-transfer-progress clean deploy
env:
MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }}
MAVEN_PASSWORD: ${{ secrets.OSSRH_PASSWORD }}
MAVEN_GPG_PASSPHRASE: ${{ secrets.MAVEN_GPG_PASSPHRASE }}
- name: Get the artifact from POM
id: project_version
run: |
echo "ARTIFACT=`mvn help:evaluate -Dexpression=project.artifactId -q -DforceStdout`" >> $GITHUB_OUTPUT
# Run for manual trigger (workflow dispatch), since you'll have release and next dev versions specified
# All commits will have a -SNAPSHOT project version anyway, since the releases will be handled here
release:
needs: setup
if: github.event_name == 'workflow_dispatch'
runs-on: ubuntu-latest
env:
RELEASE: ${{ inputs.releaseversion }}
NEXT: ${{ inputs.nextversion }}
steps:
- name: Checkout latest code
uses: actions/checkout@v3

- name: Config git user
run: |
git config user.name ${{ github.actor }}
git config user.email "${{ github.actor }}@users.noreply.github.com"
- name: Setup Java & Maven
uses: actions/setup-java@v3
with:
java-version: 17
distribution: 'temurin'
cache: 'maven'
server-id: ossrh
server-username: MAVEN_USERNAME
server-password: MAVEN_PASSWORD
gpg-private-key: ${{ secrets.MAVEN_GPG_KEY }}
gpg-passphrase: MAVEN_GPG_PASSPHRASE
# =============================================================================
# Start the release
# =============================================================================
- name: Release main POM
- name: Set the release version, commit the change, and tag it
run: |
mvn versions:set -B -ntp -DnewVersion=$RELEASE
git commit -am "Update version to $RELEASE"
git tag $RELEASE
# Build and deploy to sonatype if the release does not already exist.
# Attempt to handle case of Sonatype failing to close the repository, but still succeeding
- name: Release Java modules
working-directory: combined
run: |
mvn -B -U -V -ntp release:prepare -DreleaseVersion=$RELEASE -Dtag=$RELEASE -DdevelopmentVersion=$NEXT -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn
mvn -B -U -V -ntp release:perform -P release -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn
goal="deploy"
if curl -f -s https://oss.sonatype.org/service/local/repositories/releases/content/org/eclipse/pass/$ARTIFACT/$RELEASE/ > /dev/null; then
echo "Release $RELEASE already exists"
goal="install"
fi
mvn -B -U -V -ntp -P release -DstagingProgressTimeoutMinutes=15 clean $goal | tee release.log
code=${PIPESTATUS[0]}
marker1="Remote staging repositories are being released"
marker2="Failed to execute goal org.sonatype.plugins:nexus-staging-maven-plugin"
if [ "$code" -ne "0" ]; then
if grep -q "$marker1" release.log && grep -q "$marker2" release.log; then
echo "Failed cleaning up after pushing to Sonatype, but it may have succeeded anyway."
else
exit "$code"
fi
fi
env:
MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }}
MAVEN_PASSWORD: ${{ secrets.OSSRH_PASSWORD }}
MAVEN_GPG_PASSPHRASE: ${{ secrets.MAVEN_GPG_PASSPHRASE }}

- name: Build and publish new dev version
run: mvn -B -U -V -ntp deploy -P release
- name: Wait for Sonatype artifacts
run: |
echo "Waiting for artifacts of $RELEASE to be released."
counter=0
until curl -f -s https://oss.sonatype.org/service/local/repositories/releases/content/org/eclipse/pass/$ARTIFACT/$RELEASE/ > /dev/null
do
sleep 60
echo "."
counter=$((counter+1))
if [ "$counter" -gt 20 ]; then
echo "Timed out waiting for release"
exit 1
fi
done
echo "Artifacts for $RELEASE have been released."
- name: Set the next dev version and commit the change
run: |
mvn versions:set -B -ntp -DnewVersion=$NEXT
git commit -am "Update version to $NEXT"
- name: Release dev Java modules
working-directory: combined
run: |
mvn -B -V -ntp clean deploy -DskipTests -DskipITs
env:
MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }}
MAVEN_PASSWORD: ${{ secrets.OSSRH_PASSWORD }}
MAVEN_GPG_PASSPHRASE: ${{ secrets.MAVEN_GPG_PASSPHRASE }}

- name: Push release plugin commits
if: github.ref_type == 'branch' && github.ref_protected == false
run: git push origin ${{ github.ref_name }}

- name: Push new release tag GH
run: git push origin --tags
- name: Push the commits and tags
run: |
git push origin
git push origin --tags
46 changes: 46 additions & 0 deletions .github/workflows/snapshot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: "Publish: manual OR automatic snapshot"

on:
workflow_call:
push:
branches:
- 'main'

jobs:
setup:
runs-on: ubuntu-latest
outputs:
# Output project version from the POM to conditionally run dependent steps
project-version: ${{ steps.project_version.outputs.version }}
steps:
- name: Checkout latest code
uses: actions/checkout@v3

- name: Setup Java & Maven
uses: actions/setup-java@v3
with:
java-version: 17
distribution: 'temurin'
cache: 'maven'
server-id: ossrh
server-username: MAVEN_USERNAME
server-password: MAVEN_PASSWORD
gpg-private-key: ${{ secrets.MAVEN_GPG_KEY }}
gpg-passphrase: MAVEN_GPG_PASSPHRASE

- name: Get project version from POM
id: project_version
run: echo "VERSION=`mvn help:evaluate -Dexpression=project.version -q -DforceStdout`" >> $GITHUB_OUTPUT

# Run only if project POM has version ending in "-SNAPSHOT"
snapshot:
needs: setup
if: endsWith(needs.setup.outputs.project-version, '-SNAPSHOT')
runs-on: ubuntu-latest
steps:
- name: Publish SNAPSHOT
run: mvn -B -ntp clean deploy
env:
MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }}
MAVEN_PASSWORD: ${{ secrets.OSSRH_PASSWORD }}
MAVEN_GPG_PASSPHRASE: ${{ secrets.MAVEN_GPG_PASSPHRASE }}

0 comments on commit 5fb9e8f

Please sign in to comment.