name: Publish package to GitHub Packages and deploy documentation to GitHub pages
on:
  release:
    types: [created]
jobs:
  publish:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
        with:
          persist-credentials: false
      - name: Set up Java for publishing to GitHub Packages
        uses: actions/setup-java@v3
        with:
          distribution: 'corretto'
          java-version: 8
      - name: Load current version
        id: version
        run: echo "version=$(mvn --file abstracto-application/pom.xml -q -Dexec.executable="echo" -Dexec.args='${project.version}' --non-recursive exec:exec)" >> $GITHUB_ENV
      - name: Publish to GitHub Packages
        run: mvn --file abstracto-application/pom.xml -B deploy -P documentation,javadoc -Dmaven.wagon.http.pool=false -DskipTests=true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - name: Deploy documentation to GitHub pages
        uses: JamesIves/github-pages-deploy-action@v4
        with:
          repository-name: Sheldan/abstracto-docs
          target-folder: docs/${{ env.version }}
          branch: master
          ssh-key: ${{ secrets.ACTIONS_DEPLOY_KEY }}
          folder: abstracto-application/documentation/target/generated-docs
      - name: Deploy documentation to GitHub pages latest
        uses: JamesIves/github-pages-deploy-action@v4
        with:
          repository-name: Sheldan/abstracto-docs
          target-folder: docs/current
          branch: master
          ssh-key: ${{ secrets.ACTIONS_DEPLOY_KEY }}
          folder: abstracto-application/documentation/target/generated-docs
      - name: Login to GitHub Packages Docker Registry
        uses: docker/login-action@v2
        with:
          registry: docker.pkg.github.com
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - name: Push deployment container
        working-directory: ./abstracto-application/installer/src/main/docker/deployment
        run: docker-compose build && docker-compose push
        env:
          REGISTRY_PREFIX: docker.pkg.github.com/sheldan/abstracto/
          VERSION: ${{ env.version }}