# This workflow will build a package using Maven and then publish it to GitHub packages when a release is created
# For more information see: https://github.com/actions/setup-java/blob/main/docs/advanced-usage.md#apache-maven-with-a-settings-path

name: Maven Package

on:
  push:
    branches: [ "release" ]

jobs:
  build:

    runs-on: ubuntu-latest
    permissions:
      contents: write
      packages: write

    steps:
    - name: Checkout project
      uses: actions/checkout@v4

    - name: Set up JDK 21
      uses: actions/setup-java@v4
      with:
        java-version: '21'
        distribution: 'temurin'
        cache: maven
        server-id: github # Value of the distributionManagement/repository/id field of the pom.xml
        settings-path: ${{ github.workspace }} # location for the settings.xml file

    - name: Configure git user
      run: |
        git config user.name "Github Actions"
        git config user.email "actions@github.com"

    - name: Set release version
      run: ./mvnw build-helper:parse-version org.eclipse.tycho:tycho-versions-plugin:set-version -DnewVersion=\${parsedVersion.majorVersion}.\${parsedVersion.minorVersion}.\${parsedVersion.incrementalVersion}

    - name: Checkin changes
      run: ./mvnw scm:checkin -Dmessage='New release version' -Dgithub.repository=$GITHUB_REPO
      env:
        GITHUB_REPO: ${{ github.repository }}

    - name: Create a tag
      run: ./mvnw build-helper:parse-version scm:tag -Dgithub.repository=$GITHUB_REPO -Dtag=\${parsedVersion.majorVersion}.\${parsedVersion.minorVersion}.\${parsedVersion.incrementalVersion}
      env:
        GITHUB_REPO: ${{ github.repository }}

    - name: Publish to GitHub Packages Apache Maven
      run: ./mvnw -B -s $GITHUB_WORKSPACE/settings.xml -Dgithub.repository=$GITHUB_REPO -DskipTests=true -P release deploy
      env:
        GITHUB_TOKEN: ${{ github.token }}
        GITHUB_REPO: ${{ github.repository }}

    - name: Set next snapshot version
      run: ./mvnw build-helper:parse-version org.eclipse.tycho:tycho-versions-plugin:set-version -DnewVersion=\${parsedVersion.majorVersion}.\${parsedVersion.minorVersion}.\${parsedVersion.nextIncrementalVersion}-SNAPSHOT

    - name: Checkin next snapshot
      run: ./mvnw scm:checkin -Dmessage='New snapshot version' -Dgithub.repository=$GITHUB_REPO
      env:
        GITHUB_REPO: ${{ github.repository }}

    - name: Merge Fast Forward
      uses: MaximeHeckel/github-action-merge-fast-forward@v1.1.0
      with:
        # Branch to merge
          branchtomerge: release
          # Branch that will be updated
          branch: master
      env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
