name: CI pipeline

permissions:
  contents: write
  id-token: write
  issues: write
  pages: write

on:
  pull_request:
    branches:
    - main
  push:
    branches:
    - "main"
    tags:
      - '*'

jobs:
  tests-and-coverage:
    uses: ./.github/workflows/tests_and_coverage.yml
    secrets: inherit
  linting:
    uses: ./.github/workflows/linting.yml
  build:
    uses: ./.github/workflows/build.yml
    secrets: inherit
  publish-pypi:
    runs-on: ubuntu-latest
    if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags')
    needs:
      - tests-and-coverage
      - linting
      - build
    steps:
      - name: Download all artifacts
        uses: actions/download-artifact@v4
        with:
          name: dist
          path: dist

      - name: Publish package distributions to PyPI
        uses: pypa/gh-action-pypi-publish@release/v1
        env:
          url: https://pypi.org/p/vdoc

  publish-docker:
    runs-on: ubuntu-latest
    if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags')
    needs:
      - tests-and-coverage
      - linting
      - build
    env:
      IMAGE_NAME: voraus.jfrog.io/bypass-docker-public-production-cloud-local/vdoc
      JFROG_CLI_BUILD_NAME: vdoc/${{ github.ref_name }}
      JFROG_CLI_BUILD_NUMBER: '1'
    steps:
      - uses: jfrog/setup-jfrog-cli@v4
        env:
            JF_URL: https://voraus.jfrog.io
        with:
            oidc-provider-name: voraus-github-organization

      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
          fetch-tags: true

      - name: Download all artifacts
        uses: actions/download-artifact@v4
        with:
          name: dist
          path: dist

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3

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

      - name: Determine Docker tags
        id: set-tags
        run: |
          TAG="${{ github.ref_name }}"
          LATEST_TAG=$(git tag --sort=-v:refname | head -n 1)

          TAGS="${IMAGE_NAME}:${TAG}"
          if [[ "$TAG" == "$LATEST_TAG" ]]; then
            TAGS="${TAGS},${IMAGE_NAME}:latest"
          fi

          echo "tags=$TAGS" >> $GITHUB_OUTPUT

      - name: Build Docker Image(s)
        run: |
          IFS=',' read -ra TAG_ARRAY <<< "${{ steps.set-tags.outputs.tags }}"
          TAG_OPTIONS=$(printf " -t %s" "${TAG_ARRAY[@]}")
          jf docker build $TAG_OPTIONS .

      - name: Push and scan Docker Image(s)
        run: |
          IFS=',' read -ra TAG_ARRAY <<< "${{ steps.set-tags.outputs.tags }}"
          for TAG in "${TAG_ARRAY[@]}"; do
            jf docker push "$TAG"
            jf docker scan "$TAG"
          done

      - name: Publish Build Info
        run: |
          jf rt build-publish

  create-github-release:
    runs-on: ubuntu-latest
    if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags')
    needs:
      - publish-docker
      - publish-pypi
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Run JReleaser
        uses: jreleaser/release-action@v2
        with:
          version: 1.13.1
        env:
          JRELEASER_GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          JRELEASER_PROJECT_VERSION: ${{ github.ref_name }}

  publish-docs:
    runs-on: ubuntu-latest
    if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags')
    needs:
      - tests-and-coverage
      - linting
      - build
    steps:
      - name: Download all artifacts
        uses: actions/download-artifact@v4
        with:
          name: docs
          path: docs

      - name: Setup Pages
        uses: actions/configure-pages@v5

      - name: Upload artifact
        uses: actions/upload-pages-artifact@v3
        with:
          path: docs/

      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v4
