How to run a github-action conditional based on the maven pom version?

Viewed 426

We are trying to move CI from Jenkins to Github actions for a Maven project. Maven repositories allow to push a POM version exactly ONCE, unless the version ends in -SNAPSHOT. So in our current flow we distinguish between push to develop, where we push to the Maven repository only if the project version ends with -SNAPSHOT and push to main where we only push to Maven if it NOT ends with -SNAPSHOT.

In Jenkins it looks like this:

stage("on Merge to develop") {
            when {
                anyOf {
                    branch "develop"
                }
                expression {
                    return env.POM_VERSION ==~ /.*-SNAPSHOT/
                }
                not {
                    changeRequest()
                }
            }
            steps {
                echo "======== Merge into Develop ========"
            }
        }
        stage("on publish release (merge to main)") {
            when {
                anyOf {
                    branch "master"
                    branch "main"
                }
                not {
                    expression {
                        return env.POM_VERSION ==~ /.*-SNAPSHOT/  
                    }
                }
                not {
                    changeRequest()
                }
            }
            steps {
                echo "======== Merge into Master & PUSH ========"
            }
       }

In GitHub actions, we know how to run an action on a specific branch, but how do we:

  • get the POM version into an env (or other variable)?
  • conditionally run the step based on the POM version?

Of course - if there is a better approach, we remodel our flow

1 Answers

We figured it out:

  • to get the version into the environment use mvn help:evaluate
  • condition in steps uses the if: directive with endsWith(...)

A complete example: push to master, deploy if not a snapshot:

name: Build on master

on:
  push:
    branches:
      - main
      - master

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
      - name: Set up JDK 11
        uses: actions/setup-java@v2
        with:
          java-version: '11'
          distribution: 'adopt'
          server-id: JNX
          server-username: MAVEN_USERNAME
          server-password: MAVEN_PASSWORD
      - name: Cache Maven packages
        uses: actions/cache@v2
        with:
         path: ~/.m2
         key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
         restore-keys: ${{ runner.os }}-m2
      - name: Extract Project version
        id: project
        run: echo ::set-output name=version::$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout)
      - name: Build with Maven deploy
        if:  ${{ !endsWith(steps.project.outputs.version, '-SNAPSHOT') }}
        run: mvn --batch-mode --update-snapshots clean deploy
        env:
          MAVEN_USERNAME: ${{secrets.MAVEN_USERNAME}}
          MAVEN_PASSWORD: ${{secrets.MAVEN_TOKEN}}
Related