name: Publish New Version

on:
  workflow_dispatch:


jobs:
  build:
    permissions: write-all
    runs-on: ubuntu-latest
    steps:
    - name: 1 Check main
      uses: actions/checkout@v3
      with:
       fetch-depth: 0   
    
    - name: 2 Set up JDK 16
      uses: actions/setup-java@v3
      with:
        java-version: '16'
        distribution: 'corretto'
        cache: maven
    - name: 2.5 Create Versions
      id: version
      uses: PaulHatch/semantic-version@v4.0.2
      with:
          tag_prefix: ""
          major_pattern: "MAJOR"
          minor_pattern: "MINOR"
          format: "${major}.${minor}.${patch}"
          namespace: Release
          change_path: "."
          short_tags: false
          bump_each_commit: false
       
   
    - name: 2.7 Update version in pom.xml (Release only)
      run: mvn versions:set -DnewVersion=${{steps.version.outputs.version_tag}} 
           
    - name: 3 Build with Maven
      run: mvn package -P publish --file pom.xml  
    - name: 3.1 Show files
      run: find . -type f -ls
      
    - name: 4.1 copy jar files
      run: mkdir staging && cp Client/target/Client-${{steps.version.outputs.version_tag}}-all.jar staging
      
    - name: 5 set up a cache for maven
      uses: actions/cache@v2
      with:
         path: ~/.m2
         key: ${{runner.os}}-m2-${{hashFiles('**/pom.xml')}}
         restore-keys: ${{runner.os}}-m2

    - name: 7 Create Artifacts
      uses: actions/upload-artifact@v2
      with:
         name: ${{ steps.version.outputs.version_tag }}
         path: staging
 
    - name:  8 Create Release
      uses: "marvinpinto/action-automatic-releases@latest"
      with:
          repo_token: "${{ secrets.GITHUB_TOKEN }}"
          automatic_release_tag: ${{ steps.version.outputs.version_tag }}
          prerelease: false
          title: ${{ steps.version.outputs.version_tag }}
          files: |
              staging/*.jar
             
    - name: 9 Ovverride pom
      run: |
            git config --local user.email "action@github.com"
            git config --local user.name "GitHub Action"
            git add README.md
            git add pom.xml && git add '**/pom.xml'
            git commit -m "Update version in pom.xml"
    - name: Push changes
      uses: ad-m/github-push-action@master
      with:
       branch: master            
