# 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://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-maven

# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

name: Java CI with Maven

# The API requires write permission on the repository to submit dependencies
permissions:
  contents: write
  packages: write

on:
  push:
    tags:
      - '*'  # 只有推送tag时才会触发构建
    branches-ignore:
      - '*'  # 排除所有分支的提交
    paths-ignore:
      - 'bin/**'
      - '.github/**'
      - '.mvn/**'
      - '.run/**'
      - '.vscode/**'
      - '*.txt'
      - '*.md'
  pull_request:
    branches:
      - "main"

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3

    - uses: actions/setup-node@v4
      with:
        node-version: '18'

    - name: Set up JDK 17
      uses: actions/setup-java@v3
      with:
        java-version: '17'
        distribution: 'temurin'
        cache: maven

    - name: Build Frontend
      run: cd web-front && yarn install && yarn run build

    - name: Build with Maven
      run: mvn -B package -DskipTests --file pom.xml

    # Optional: Uploads the full dependency graph to GitHub to improve the quality of Dependabot alerts this repository can receive
    - name: Update dependency graph
      uses: advanced-security/maven-dependency-submission-action@v3
      if: github.event_name != 'pull_request'
      continue-on-error: true
      with:
        ignore-maven-wrapper: true

#    - uses: release-drafter/release-drafter@v5
#      env:
#        GITHUB_TOKEN: ${{ secrets.ACCESS_TOKEN }}

    - name: Upload Artifact
      uses: actions/upload-artifact@v4
      with:
        path: web-service/target/netdisk-fast-download-bin.zip

    - name: Login to GitHub Container Registry
      if: github.event_name != 'pull_request'
      uses: docker/login-action@v3
      with:
        registry: ghcr.io
        username: ${{ github.actor }}
        password: ${{ secrets.GITHUB_TOKEN }}

    - name: Set up Docker Buildx
      uses: docker/setup-buildx-action@v3

    - name: Extract git tag
      id: tag
      run: |
        GIT_TAG=$(git tag --points-at HEAD | head -n 1)
        echo "tag=$GIT_TAG" >> $GITHUB_OUTPUT

    - name: Build and push Docker image
      if: github.event_name != 'pull_request'
      uses: docker/build-push-action@v5
      with:
        context: .
        push: true
        platforms: linux/amd64,linux/arm64,linux/arm/v7
        tags: |
          ghcr.io/${{ github.repository }}:${{ steps.tag.outputs.tag }}
          ghcr.io/${{ github.repository }}:latest
