name: "Release pipeline"

on:
  release:
    types: [published]
  workflow_dispatch:

permissions:
  packages: write

jobs:
  api:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v6

      - uses: docker/metadata-action@v5
        id: meta
        with:
          images: |
            ${{ github.repository }}-api
            ghcr.io/${{ github.repository }}-api
          tags: |
            latest
            ${{ github.ref_name }}

      - name: "Login to Docker Hub"
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: "Login to GitHub Container Registry"
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - uses: docker/build-push-action@v6
        with:
          context: ./api
          push: true
          tags: ${{ steps.meta.outputs.tags }}

  app:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v6

      - uses: docker/metadata-action@v5
        id: meta
        with:
          images: |
            ${{ github.repository }}-app
            ghcr.io/${{ github.repository }}-app
          tags: |
            latest
            ${{ github.ref_name }}

      - name: "Login to Docker Hub"
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: "Login to GitHub Container Registry"
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - uses: docker/build-push-action@v6
        with:
          context: ./app
          push: true
          tags: ${{ steps.meta.outputs.tags }}

  pages:
    runs-on: ubuntu-latest
    container: node:latest

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

    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}

    steps:
      - uses: actions/checkout@v6

      - run: npm install
        working-directory: ./app

      - run: npm run build
        env:
          VITE_BACKEND_HOST: https://api.bonch.xfox111.net
        working-directory: ./app

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

      - uses: actions/upload-pages-artifact@v4
        with:
          path: "./app/dist"

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