name: Deploy App
on: [push]

jobs:
  build:
    runs-on: ubuntu-22.04
    container:
      volumes:
        - /var/docker-web:/var/docker-web
    steps:
      - uses: actions/checkout@v4
      - name: Prepare and build app
        run: |
          REPO_NAME="${GITHUB_REPOSITORY##*/}"
          APP_DIR="/var/docker-web/apps/${REPO_NAME}"
          bash /var/docker-web/src/cli.sh down "${REPO_NAME}"
          rm -rf "$APP_DIR"
          mkdir "$APP_DIR"
          cp -a $(find . -mindepth 1 -maxdepth 1 ! -name '.git' ! -name 'node_modules') "$APP_DIR/"
          export COMPOSE_BAKE=false
          docker rmi "local/${REPO_NAME}" 2>/dev/null || true
          bash /var/docker-web/src/cli.sh build "${REPO_NAME}"

  deploy:
    runs-on: ubuntu-22.04
    needs: build
    container:
      volumes:
        - /var/docker-web:/var/docker-web
    steps:
      - uses: actions/checkout@v4
      - name: Deploy with docker-web
        run: |
          REPO_NAME="${GITHUB_REPOSITORY##*/}"
          bash /var/docker-web/src/cli.sh up "${REPO_NAME}"
