4

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

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
user8152821
  • 157
  • 2
  • 10
  • The first challenge with this is that you will need to perform a non-interactive release. Where would the version for the release be extracted from by the workflow? https://maven.apache.org/maven-release/maven-release-plugin/examples/non-interactive-release.html – peterevans Oct 11 '19 at 12:29
  • Related https://stackoverflow.com/questions/57711558/deploy-to-github-package-registry-from-github-action – OneCricketeer Nov 06 '19 at 06:23
  • 2
    In the Github Actions market place, you can find some templates, here is one: https://github.com/marketplace/actions/maven-release not sure if this is what you are looking for. – Javier Montón Mar 06 '20 at 14:33

2 Answers2

8

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 "actions@github.com"
          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.

Henrique Prange
  • 251
  • 3
  • 8
  • How can I get the password for actions@github.com? CI_DEPLOY_PASSWORD? I will like to do it through ssh – lak Jan 04 '22 at 19:50
0

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 "actions@github.com"
          git config user.name "GitHub Actions"
      - name: Publish JAR
        run: ./mvnw -B release:prepare release:perform
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
The Tran
  • 400
  • 4
  • 6