name: Run Docker

# Controls when the workflow will run
on:
  push:
    branches:
      - 'master'
      - 'v*.x'
    tags:
      - 'v*'
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
permissions:
  contents: read
  packages: write

jobs:
  build:
    strategy:
      matrix:
        include:
          - os: ubuntu-latest
            platforms: linux/amd64
            required: true
          - os: ubuntu-24.04-arm
            platforms: linux/arm64
            required: true
    continue-on-error: ${{ !matrix.required }}
    runs-on: ${{ matrix.os }}
    steps:
      - name: Prepare
        run: |
          platform=${{ matrix.platforms }}
          echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV
          echo "IMAGE=ghcr.io/${GITHUB_REPOSITORY@L}" >> $GITHUB_ENV
      - uses: actions/checkout@v6

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

      - name: Login to GitHub Container Registry
        uses: docker/login-action@v4
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - name: Docker meta
        id: meta
        uses: docker/metadata-action@v6
        with:
          images: ${{ env.IMAGE }}

      - name: Cache node_modules
        id: cache-node-modules
        uses: actions/cache@v5
        with:
          path: var-cache-node-modules
          key: var-cache-node-modules-${{ hashFiles('Dockerfile', 'install/package.json') }}

      - name: Build and push Docker images
        id: build
        uses: docker/build-push-action@v7
        with:
          cache-from: type=gha
          cache-to: type=gha,mode=min
          context: .
          file: ./Dockerfile
          platforms: ${{ matrix.platforms }}
          labels: ${{ steps.meta.outputs.labels }}
          tags: ${{ env.IMAGE }}
          outputs: type=image,push-by-digest=true,name-canonical=true,push=true
      - name: Export digest
        run: |
          mkdir -p ${{ runner.temp }}/digests
          digest="${{ steps.build.outputs.digest }}"
          touch "${{ runner.temp }}/digests/${digest#sha256:}"

      - name: Upload digest
        uses: actions/upload-artifact@v7
        with:
          name: digests-${{ env.PLATFORM_PAIR }}
          path: ${{ runner.temp }}/digests/*
          if-no-files-found: error
          retention-days: 1
  merge:
    runs-on: ubuntu-latest
    needs:
      - build
    steps:
      - name: Prepare
        run: |
          echo "IMAGE=ghcr.io/${GITHUB_REPOSITORY@L}" >> $GITHUB_ENV
          echo "CURRENT_DATE_NST=$(date +'%Y%m%d-%H%M%S' -d '-3 hours -30 minutes')" >> $GITHUB_ENV
      - name: Download digests
        uses: actions/download-artifact@v8
        with:
          path: ${{ runner.temp }}/digests
          pattern: digests-*
          merge-multiple: true
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v4

      - name: Login to GitHub Container Registry
        uses: docker/login-action@v4
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - name: Docker meta
        id: meta
        uses: docker/metadata-action@v6
        with:
          images: ${{ env.IMAGE }}
          tags: |
            type=semver,pattern={{version}}
            type=semver,pattern={{major}}.{{minor}}
            type=semver,pattern={{major}}.x
            type=raw,value=latest,enable={{is_default_branch}}
            type=ref,event=branch,enable=${{ github.event.repository.default_branch != github.ref }}
            type=raw,value=${{ env.CURRENT_DATE_NST }}
          flavor: |
            latest=true
      - name: Create manifest list and push
        working-directory: ${{ runner.temp }}/digests
        run: |
          docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
            $(printf '${{ env.IMAGE }}@sha256:%s ' *)
      - name: Inspect image
        run: |
          docker buildx imagetools inspect ${{ env.IMAGE }}:${{ steps.meta.outputs.version }}
