# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven

name: Deploy & Upload

on:
  # 支持手动触发构建
  workflow_dispatch:
  release:
    # 创建release的时候触发
    types: [ published ]

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
      - name: "Set up JDK"
        uses: actions/setup-java@v2
        with:
          java-version: '11'
          distribution: 'adopt'
          cache: maven
          server-id: github
          server-username: MAVEN_USERNAME
          server-password: MAVEN_TOKEN

      - name: "Maven Deploy"
        run: mvn -B deploy --file pom.xml -DskipTests
        env:
          MAVEN_USERNAME: ${{ github.repository_owner }}
          MAVEN_TOKEN: ${{secrets.GITHUB_TOKEN}}

      - name: "Release Asset Upload"
        id: upload-release-asset
        uses: shogo82148/actions-upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ github.event.release.upload_url }}
          asset_path: asset/*.jar
          asset_content_type: application/java-archive

      - name: "Javadoc Deploy Staging"
        run: |
          rm -rf docs
          mkdir -vp docs
          cp -vrf target/apidocs/* docs/
          cp -vrf .documentation/JAVADOC-README.md docs/README.md

      - name: "Generate the Javadoc sitemap"
        id: sitemap
        uses: cicirello/generate-sitemap@v1
        with:
          base-url-path: https://${{ github.repository_owner }}.github.io/${{ github.event.repository.name }}
          path-to-root: docs

      - name: "Output Javadoc stats"
        run: |
          echo "sitemap-path = ${{ steps.sitemap.outputs.sitemap-path }}"
          echo "url-count = ${{ steps.sitemap.outputs.url-count }}"
          echo "excluded-count = ${{ steps.sitemap.outputs.excluded-count }}"

      - name: "Configure Git"
        env:
          DEPLOY_PRI: ${{secrets.DEPLOY_PRI}}
        run: |
          sudo timedatectl set-timezone "Asia/Shanghai"
          mkdir -p ~/.ssh/
          echo "$DEPLOY_PRI" > ~/.ssh/id_rsa
          chmod 600 ~/.ssh/id_rsa
          ssh-keyscan github.com >> ~/.ssh/known_hosts
          git config --global user.name 'CarmJos'
          git config --global user.email 'carm@carm.cc'

      - name: "Commit Javadocs"
        run: |
          cd docs
          git init
          git remote add origin git@github.com:${{ github.repository }}.git
          git checkout -b gh-pages
          git add -A
          git commit -m "API Document generated."

      - name: "Push javadocs"
        run: |
          cd docs
          git push origin HEAD:gh-pages --force