user8152821
user8152821

Reputation: 187

Maven release from Github Actions

I wanted to know if we could use Github Actions for committing release commits after closing a milestone.

So what I've done previously is to commit mvn release:prepare && mvn release:perform on my local computer, then push it. However, with Github Actions, I was hoping that I could automate release commits from the workflow itself.

So far all the tutorials for using Maven with Github Actions are limited to running clean install or test checks or deployments, but never release.

Does anyone have any experience in setting up Maven release with Github Actions? Thank you

Upvotes: 6

Views: 9717

Answers (2)

The Tran
The Tran

Reputation: 470

You don't need to declare the server config manually. We can simply leverage the actions/setup-java to make it create the server config with server-id

      - name: Set up JDK 11
        uses: actions/setup-java@v3
        with:
          distribution: 'temurin'
          java-version: '11'
          cache: 'maven'
          server-id: github

Then we can view the settings.xml

      - name: View settings.xml
        run: cat /home/runner/.m2/settings.xml

And perform the release

      - name: Configure Git user
        run: |
          git config user.email "[email protected]"
          git config user.name "GitHub Actions"
      - name: Publish JAR
        run: ./mvnw -B release:prepare release:perform
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Upvotes: 3

Henrique Prange
Henrique Prange

Reputation: 281

With the addition of the workflow_dispatch event, you can now create workflows on GitHub Actions that are manually triggered. Also, you can specify inputs, which GitHub will present as form elements in the UI. This is particularly useful when cutting releases with Maven.

  1. Create a new workflow containing two input fields: releaseVersion and developmentVersion.
on:
  workflow_dispatch:
    inputs:
      releaseVersion:
        description: "Default version to use when preparing a release."
        required: true
        default: "X.Y.Z"
      developmentVersion:
        description: "Default version to use for new local working copy."
        required: true
        default: "X.Y.Z-SNAPSHOT"
  1. Set up a job to make Maven releases using the given releaseVersion and developmentVersion.
jobs:
  release:
    runs-on: ubuntu-latest

    steps:
      # Checkout source code, set up Java, etc. Then...

      - name: Configure Git User
        run: |
          git config user.email "[email protected]"
          git config user.name "GitHub Actions"

      - name: Maven Release
        run: mvn release:prepare release:perform -B -s .maven_settings.xml -DreleaseVersion=${{ github.event.inputs.releaseVersion }} -DdevelopmentVersion=${{ github.event.inputs.developmentVersion }}
        env:
          CI_DEPLOY_USERNAME: ${{ secrets.CI_DEPLOY_USERNAME }}
          CI_DEPLOY_PASSWORD: ${{ secrets.CI_DEPLOY_PASSWORD }}

You must have a .maven_settings.xml file saved at the root of your project, containing the username and password to authenticate before downloading or uploading artifacts to repositories configured in the repositories and distributionManagement sections.

<settings>
  ...
  <servers>
    <server>
      <id>server.id</id>
      <username>${env.CI_DEPLOY_USERNAME}</username>
      <password>${env.CI_DEPLOY_PASSWORD}</password>
    </server>
  </servers>
</settings>

You can configure both secrets CI_DEPLOY_USERNAME and CI_DEPLOY_PASSWORD on the project's settings page.

Here's a full example of a Maven release workflow configuration for reference.

Upvotes: 9

Related Questions