name: build and push
on:
  push:
    branches:
      - main
    tags:
      - "**"
  pull_request:

# Cancel in-progress runs on the same ref (PR or branch) when a new commit
# arrives, so we don't waste CI building superseded commits.
concurrency:
  group: build-and-push-${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

env:
  DOCKERHUB_IMAGE: netbirdio/dashboard
  GHCR_IMAGE: ghcr.io/netbirdio/dashboard-cloud

jobs:
  build_n_push:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5

      - name: setup-node
        uses: actions/setup-node@v5
        with:
          node-version: '20'
          cache: 'npm'

      - name: Install dependencies
        run: npm install

      - run: echo '{}' > .local-config.json

      - name: Download IronRDP release TS files
        uses: robinraju/release-downloader@v1.7
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          repository: netbirdio/IronRDP
          latest: true
          fileName: "*.ts"
          out-file-path: 'public/ironrdp-pkg'

      - name: Download IronRDP release JS files
        uses: robinraju/release-downloader@v1.7
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          repository: netbirdio/IronRDP
          latest: true
          fileName: "*.js"
          out-file-path: 'public/ironrdp-pkg'

      - name: Download IronRDP release WASM file
        uses: robinraju/release-downloader@v1.7
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          repository: netbirdio/IronRDP
          latest: true
          fileName: "ironrdp_web_bg.wasm"
          out-file-path: 'public/ironrdp-pkg'

      - name: Get version from tag
        id: version
        run: |
          if [[ "${{ github.ref }}" == refs/tags/* ]]; then
            echo "version=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT
          else
            echo "version=development" >> $GITHUB_OUTPUT
          fi

      - name: Build
        run: npm run build
        env:
          NEXT_PUBLIC_DASHBOARD_VERSION: ${{ steps.version.outputs.version }}
      -
        name: Set up QEMU
        uses: docker/setup-qemu-action@v3
      -
        name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
      -
        name: Docker meta
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: |
            ${{ env.DOCKERHUB_IMAGE }}
            ${{ env.GHCR_IMAGE }}
          flavor: |
            latest=false
          tags: |
            type=schedule
            type=ref,event=branch
            type=ref,event=tag
            type=raw,value=latest,enable=${{ startsWith(github.ref, 'refs/tags/') }}
            type=ref,event=pr
            type=sha

      - name: Log in to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.NB_DOCKER_USER }}
          password: ${{ secrets.NB_DOCKER_TOKEN }}

      - name: Log in to the GitHub Container registry
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build and push Docker image
        uses: docker/build-push-action@v5
        with:
          context: .
          file: docker/Dockerfile
          push: true
          platforms: linux/amd64,linux/arm64,linux/arm
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}

      - run: |
          echo '### Pushed tags' >> $GITHUB_STEP_SUMMARY
          echo '${{ steps.meta.outputs.tags }}' >> $GITHUB_STEP_SUMMARY
