name: Build and Deploy Snapshot
on:
  push:
    branches:
    - master
    - "3.0"
    - "2.14"
    paths-ignore:
    - "README.md"
    - "release-notes/*"
  pull_request:
    branches:
    - master
    - "3.0"
    - "2.14"
    paths-ignore:
    - "README.md"
    - "release-notes/*"
permissions:
  contents: read

jobs:
  build:
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        # Alas, as long as JDK6 is target, cannot run against 14+
        java_version: ['8', '11']
        os: ['ubuntu-20.04']
    env:
      JAVA_OPTS: "-XX:+TieredCompilation -XX:TieredStopAtLevel=1"
    steps:
    - uses: actions/checkout@v3
    - name: Set up JDK
      uses: actions/setup-java@v3
      with:
        distribution: 'temurin'
        java-version: ${{ matrix.java_version }}
        cache: 'maven'
        server-id: sonatype-nexus-snapshots
        server-username: CI_DEPLOY_USERNAME
        server-password: CI_DEPLOY_PASSWORD
        # See https://github.com/actions/setup-java/blob/v2/docs/advanced-usage.md#Publishing-using-Apache-Maven
        # gpg-private-key: ${{ secrets.MAVEN_GPG_PRIVATE_KEY }} # Value of the GPG private key to import
        # gpg-passphrase: MAVEN_GPG_PASSPHRASE # env variable for GPG private key passphrase
    - name: Build
      run: ./mvnw -V -B -ff -ntp verify
    - name: Extract project Maven version
      id: projectVersion
      run: echo "version=$(./mvnw org.apache.maven.plugins:maven-help-plugin:3.2.0:evaluate -DforceStdout -Dexpression=project.version -q)" >> $GITHUB_OUTPUT
    - name: Deploy snapshot
      if: github.event_name != 'pull_request' && matrix.java_version == '8' && endsWith(steps.projectVersion.outputs.version, '-SNAPSHOT')
      env:
        CI_DEPLOY_USERNAME: ${{ secrets.CI_DEPLOY_USERNAME }}
        CI_DEPLOY_PASSWORD: ${{ secrets.CI_DEPLOY_PASSWORD }}
        # MAVEN_GPG_PASSPHRASE: ${{ secrets.MAVEN_GPG_PASSPHRASE }}
      run: ./mvnw -B -ff -DskipTests -ntp source:jar deploy
