name: CI
on:
  merge_group:
  pull_request:
  push:
    branches:
      - main
      - rolling-release-*
      - v[0-9]+.[0-9]+.[0-9]+

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  pre-commit:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-python@v4
        with:
          python-version: "3.x"
      - uses: ocaml/setup-ocaml@v2
        with:
          ocaml-compiler: 4.14.x
          opam-pin: false
          opam-depext: false
      - name: Install ocamlformat
        run: |
          opam install -y ocamlformat=0.25.1
      - uses: pre-commit/action@v3.0.0

  build_details:
    runs-on: ubuntu-latest
    outputs:
      branch: ${{ steps.build_details.outputs.branch }}
      sha: ${{ steps.build_details.outputs.sha }}
      is_release: ${{ steps.build_details.outputs.is_release }}
      is_rolling_release: ${{ steps.build_details.outputs.is_rolling_release }}
      is_fork: ${{ steps.build_details.outputs.is_fork }}
      build_os: ${{ steps.build_details.outputs.build_os }}
      build_platform: ${{ steps.build_details.outputs.build_platform }}
      build_include: ${{ steps.build_details.outputs.build_include }}
      docker_release: ${{ steps.build_details.outputs.docker_release }}
      s3-artifact-basepath: ${{ steps.build_details.outputs.s3-artifact-basepath }}
      minimal_exclude_deps: ${{ steps.build_details.outputs.minimal_exclude_deps }}
      save_traces: ${{ steps.build_details.outputs.save_traces }}
      is_snapshot: ${{ steps.build_details.outputs.is_snapshot }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
      - name: Get build details
        env:
          IS_FORK: ${{ github.event.pull_request.head.repo.fork == true }}
        run: .github/scripts/build-details.sh
        id: build_details

  build_no_depopts:
    runs-on: ubuntu-latest
    needs: build_details
    container:
      image: savonet/liquidsoap-ci:debian_bookworm_amd64
      options: --user opam
    env:
      HOME: /home/opam
    steps:
      - name: Get number of CPU cores
        uses: savonet/github-actions-cpu-cores-docker@v1
        id: cpu_cores
      - name: Checkout code
        run: |
          cd /tmp/liquidsoap-full/liquidsoap
          git remote set-url origin https://github.com/savonet/liquidsoap.git
          git fetch origin ${{ github.sha }}
          git checkout ${{ github.sha }}
      - name: Build
        run: |
          echo "::group::Preparing build"
          cd /tmp/liquidsoap-full
          git remote set-url origin https://github.com/savonet/liquidsoap-full.git
          git fetch --recurse-submodules=no && git checkout origin/master -- Makefile.git
          make public
          git reset --hard
          git pull
          cp PACKAGES.minimal PACKAGES
          eval $(opam env)
          opam pin -yn .
          opam info -f "depopts:" liquidsoap-core | grep -v osx-secure-transport | xargs opam remove --force -y ffmpeg-avutil cohttp-lwt-unix prometheus-app ${{ needs.build_details.outputs.minimal_exclude_deps }}
          echo "::endgroup::"
          cd liquidsoap
          ./.github/scripts/build-posix.sh "${{ steps.cpu_cores.outputs.count }}"
        env:
          LIQ_BUILD_MIN: true
      - name: Build doc
        run: |
          cd /tmp/liquidsoap-full/liquidsoap
          ./.github/scripts/build-doc.sh

  build_js:
    runs-on: ubuntu-latest
    container:
      image: savonet/liquidsoap-ci:debian_bookworm_amd64
      options: --user opam
    env:
      HOME: /home/opam
    steps:
      - name: Checkout code
        run: |
          cd /tmp/liquidsoap-full/liquidsoap
          git remote set-url origin https://github.com/savonet/liquidsoap.git
          git fetch origin ${{ github.sha }}
          git checkout ${{ github.sha }}
          mv .git /tmp
          rm -rf ./*
          mv /tmp/.git .
          git reset --hard
      - name: Build JS
        run: |
          cd /tmp/liquidsoap-full/liquidsoap
          eval "$(opam config env)"
          dune build --profile release ./src/js/interactive_js.bc.js

  update_doc:
    runs-on: ubuntu-latest
    needs: build_details
    if: github.repository_owner == 'savonet' && needs.build_details.outputs.branch == 'main'
    container:
      image: savonet/liquidsoap-ci:debian_bookworm_amd64
      options: --user root -v ${{ github.workspace }}/${{ github.run_number }}:/tmp/${{ github.run_number }}
    env:
      HOME: /home/opam
    steps:
      - name: Get number of CPU cores
        uses: savonet/github-actions-cpu-cores-docker@v1
        id: cpu_cores
      - name: Checkout code
        run: |
          cd /tmp/liquidsoap-full/liquidsoap
          rm -rf doc/content/build.md doc/content/install.md
          sudo -u opam -E git remote set-url origin https://github.com/savonet/liquidsoap.git
          sudo -u opam -E git fetch origin ${{ github.sha }}
          sudo -u opam -E git checkout ${{ github.sha }}
      - name: Build doc
        run: |
          cd /tmp/liquidsoap-full/liquidsoap
          export CPU_CORES=${{ steps.cpu_cores.outputs.count }}
          # TMP
          sudo -u opam -E ./.github/scripts/add-local-opam-packages.sh
          sudo -u opam -E opam install -y ssl.0.7.0
          sudo -u opam -E ./.github/scripts/build-posix.sh "${{ steps.cpu_cores.outputs.count }}"
          cd /tmp/liquidsoap-full/liquidsoap/
          eval $(opam config env)
          dune build --release src/js
          cd /tmp/liquidsoap-full/website
          make dist
          mkdir -p /tmp/${{ github.run_number }}/html
          cp -rf html/* /tmp/${{ github.run_number }}/html
      - name: Push content
        if: success() && github.repository_owner == 'savonet'
        uses: crazy-max/ghaction-github-pages@v2
        with:
          repo: savonet/savonet.github.io
          target_branch: master
          build_dir: ${{ github.run_number }}/html
          fqdn: www.liquidsoap.info
        env:
          GH_PAT: ${{ secrets.WEBSITE_TOKEN }}

  run_tests:
    runs-on: [self-hosted, citest]
    needs: build_details
    container:
      image: savonet/liquidsoap-ci:debian_bookworm_arm64
      options: --user root --privileged --ulimit core=-1 --security-opt seccomp=unconfined -v ${{ github.workspace }}/${{ github.run_number }}:/tmp/${{ github.run_number }}
    strategy:
      fail-fast: false
      matrix:
        target: ["@citest", "@mediatest"]
    env:
      HOME: /home/opam
    steps:
      - name: Get number of CPU cores
        uses: savonet/github-actions-cpu-cores-docker@v1
        id: cpu_cores
      - name: Enable core dump
        run: |
          ulimit -c unlimited
          mkdir -p /tmp/${{ github.run_number }}/core
          chown -R opam /tmp/${{ github.run_number }}/core
          echo /tmp/${{ github.run_number }}/core/core.%h.%e.%t > /proc/sys/kernel/core_pattern
      - name: Checkout code
        run: |
          cd /tmp/liquidsoap-full/liquidsoap
          rm -rf doc/content/build.md doc/content/install.md
          sudo -u opam -E git remote set-url origin https://github.com/savonet/liquidsoap.git
          sudo -u opam -E git fetch origin ${{ github.sha }}
          sudo -u opam -E git checkout ${{ github.sha }}
      - name: Build
        run: |
          cd /tmp/liquidsoap-full/liquidsoap
          export CPU_CORES=${{ steps.cpu_cores.outputs.count }}
          # TMP
          sudo -u opam -E ./.github/scripts/add-local-opam-packages.sh
          sudo -u opam -E opam install -y ssl.0.7.0
          sudo -u opam -E ./.github/scripts/build-posix.sh "${{ steps.cpu_cores.outputs.count }}"
          cp /tmp/liquidsoap-full/liquidsoap/_build/default/src/bin/liquidsoap.exe /tmp/${{ github.run_number }}/core/liquidsoap
      - name: Compute stats
        run: |
          cd /tmp/liquidsoap-full/liquidsoap
          sudo -u opam -E ./.github/scripts/stats-posix.sh
      - name: Run tests
        run: |
          cd /tmp/liquidsoap-full/liquidsoap
          sudo -u opam -E ./.github/scripts/test-posix.sh ${{ matrix.target }}
      - name: Save traces
        if: (success() || failure()) && ${{ needs.build_details.outputs.save_traces == 'true' }}
        run: |
          mkdir -p /tmp/${{ github.run_number }}/traces/${{ matrix.target }}
          cd /tmp/liquidsoap-full/liquidsoap
          find _build/default/tests | grep '\.trace$' | while read i; do mv "$i" /tmp/${{ github.run_number }}/traces/${{ matrix.target }}; done
      - name: Upload traces
        if: (success() || failure()) && ${{ needs.build_details.outputs.save_traces == 'true' }}
        uses: actions/upload-artifact@v3
        with:
          name: traces-${{ matrix.target }}
          path: ${{ github.workspace }}/${{ github.run_number }}/traces/${{ matrix.target }}
      - name: Export potential core dumps
        uses: actions/upload-artifact@v3
        if: failure()
        with:
          name: core-dump-${{ matrix.os }}-${{ matrix.platform }}-${{ matrix.target }}
          path: ${{ github.workspace }}/${{ github.run_number }}/core
      - name: Cleanup
        if: ${{ always() }}
        run: |
          rm -rf /tmp/${{ github.run_number }}/core

  build_opam:
    runs-on: ubuntu-latest
    needs: build_details
    strategy:
      fail-fast: false
      matrix:
        ocaml-compiler:
          - 4.14.x
          - 5.0.x
    steps:
      - name: Checkout latest code
        uses: actions/checkout@v3
      - name: Update packages
        run: |
          sudo apt-get update
      - name: Setup OCaml
        uses: ocaml/setup-ocaml@v2
        with:
          ocaml-compiler: ${{ matrix.ocaml-compiler }}
          opam-pin: false
          opam-depext: false
      - name: Add local packages
        run: |
          ./.github/scripts/add-local-opam-packages.sh
      - name: Install liquidsoap
        run: |
          opam install --cli=2.1 --confirm-level=unsafe-yes .

  build_posix:
    runs-on: ${{ matrix.runs-on }}
    needs: build_details
    strategy:
      fail-fast: false
      matrix:
        os: ${{ fromJson(needs.build_details.outputs.build_os) }}
        platform: ${{ fromJson(needs.build_details.outputs.build_platform) }}
        include: ${{ fromJson(needs.build_details.outputs.build_include) }}
        exclude:
          - os: debian_bullseye
            platform: arm64
          - os: debian_bullseye
            platform: amd64
          - os: debian_bookworm
            platform: armhf
          - os: debian_trixie
            platform: armhf
          - os: ubuntu_jammy
            platform: armhf
          - os: ubuntu_lunar
            platform: armhf
    container:
      image: savonet/liquidsoap-ci:${{ matrix.os }}_${{ matrix.platform }}
      options: --user root --privileged -v ${{ github.workspace }}/${{ github.run_number }}:/tmp/${{ github.run_number }}
    env:
      HOME: /home/opam
      IS_SNAPSHOT: ${{ needs.build_details.outputs.is_snapshot == 'true' }}
    steps:
      - name: Get number of CPU cores
        uses: savonet/github-actions-cpu-cores-docker@v1
        id: cpu_cores
      - name: Checkout code
        run: |
          cd /tmp/liquidsoap-full/liquidsoap
          rm -rf doc/content/build.md doc/content/install.md
          sudo -u opam -E git remote set-url origin https://github.com/savonet/liquidsoap.git
          sudo -u opam -E git fetch origin ${{ github.sha }}
          sudo -u opam -E git checkout ${{ github.sha }}
      - name: Update debian packages
        if: contains(matrix.os, 'debian') || contains(matrix.os, 'ubuntu')
        run: |
          echo "opam hold" | sudo dpkg --set-selections
          sudo apt-get update
          sudo apt-get -y dist-upgrade
          sudo apt-get -y autoremove
      - name: Update alpine packages
        if: matrix.os == 'alpine'
        run: |
          apk -U --force-overwrite upgrade
      - name: Build
        run: |
          cd /tmp/liquidsoap-full/liquidsoap
          export CPU_CORES=${{ steps.cpu_cores.outputs.count }}
          # TMP
          sudo -u opam -E ./.github/scripts/add-local-opam-packages.sh
          sudo -u opam -E opam install -y ssl.0.7.0
          sudo -u opam -E ./.github/scripts/build-posix.sh "${{ steps.cpu_cores.outputs.count }}" ${{ matrix.platform }}
      - name: Build doc
        if: contains(matrix.os, 'debian') || contains(matrix.os, 'ubuntu')
        run: |
          cd /tmp/liquidsoap-full/liquidsoap
          sudo -u opam -E ./.github/scripts/build-doc.sh
      - name: Build debian package
        if: contains(matrix.os, 'debian') || contains(matrix.os, 'ubuntu')
        id: build_deb
        run: |
          mkdir -p /tmp/${{ github.run_number }}/${{ matrix.os }}_${{ matrix.platform }}/debian
          chown -R opam /tmp/${{ github.run_number }}/${{ matrix.os }}_${{ matrix.platform }}/debian
          chown -R opam "${GITHUB_OUTPUT}"
          cd /tmp/liquidsoap-full/liquidsoap
          sudo -u opam -E ./.github/scripts/build-deb.sh ${{ github.sha }} ${{ needs.build_details.outputs.branch }} ${{ matrix.os }} ${{ matrix.platform }} "${{ needs.build_details.outputs.is_rolling_release }}" "${{ needs.build_details.outputs.is_release }}" "${{ needs.build_details.outputs.minimal_exclude_deps }}"
      - name: Upload debian packages artifacts
        if: (contains(matrix.os, 'debian') || contains(matrix.os, 'ubuntu')) && matrix.platform != 'armhf'
        uses: actions/upload-artifact@v3
        with:
          name: ${{ steps.build_deb.outputs.basename }}
          path: ${{ github.workspace }}/${{ github.run_number }}/${{ matrix.os }}_${{ matrix.platform }}/debian
          if-no-files-found: error
      - name: Upload debian armhf packages artifacts
        if: contains(matrix.os, 'debian') && matrix.platform == 'armhf'
        uses: savonet/aws-s3-docker-action@master
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          SOURCE: ${{ github.workspace }}/${{ github.run_number }}/${{ matrix.os }}_${{ matrix.platform }}/debian
          TARGET: ${{ needs.build_details.outputs.s3-artifact-basepath }}
      - name: Build alpine package
        if: matrix.os == 'alpine'
        id: build_apk
        run: |
          cd /tmp/liquidsoap-full/liquidsoap
          apk add alpine-sdk
          adduser opam abuild
          mkdir -p /tmp/${{ github.run_number }}/${{ matrix.os }}_${{ matrix.platform }}/alpine
          chown -R opam /tmp/${{ github.run_number }}/${{ matrix.os }}_${{ matrix.platform }}/alpine
          chown -R opam "${GITHUB_OUTPUT}"
          sudo -u opam -E ./.github/scripts/build-apk.sh ${{ needs.build_details.outputs.branch }} ${{ matrix.os }} ${{ matrix.platform }} ${{ matrix.alpine-arch }} "${{ needs.build_details.outputs.is_rolling_release }}" "${{ needs.build_details.outputs.is_release }}" "${{ needs.build_details.outputs.minimal_exclude_deps }}"
      - name: Upload alpine packages artifacts
        if: needs.build_details.outputs.is_fork != 'true' && matrix.os == 'alpine'
        uses: savonet/aws-s3-docker-action@master
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          SOURCE: ${{ github.workspace }}/${{ github.run_number }}/${{ matrix.os }}_${{ matrix.platform }}/alpine
          TARGET: ${{ needs.build_details.outputs.s3-artifact-basepath }}
      - name: Cleanup
        if: ${{ always() }}
        run: |
          rm -rf /tmp/${{ github.run_number }}/${{ matrix.os }}_${{ matrix.platform }}

  fetch_s3_artifacts:
    runs-on: ubuntu-latest
    needs: [build_details, build_posix]
    steps:
      - name: Prepare directory
        run: |
          rm -rf ${{ github.workspace }}/${{ github.run_number }}/s3-artifacts
          mkdir -p ${{ github.workspace }}/${{ github.run_number }}/s3-artifacts
      - name: Fetch S3 artifacts
        if: needs.build_details.outputs.is_fork != 'true'
        uses: savonet/aws-s3-docker-action@master
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          SOURCE: ${{ needs.build_details.outputs.s3-artifact-basepath }}
          TARGET: ${{ github.workspace }}/${{ github.run_number }}/s3-artifacts
      - name: Upload S3 artifacts
        uses: actions/upload-artifact@v3
        if: needs.build_details.outputs.is_fork != 'true'
        with:
          name: alpine-packages
          path: ${{ github.workspace }}/${{ github.run_number }}/s3-artifacts

  build_win32:
    runs-on: ubuntu-latest
    needs: build_details
    strategy:
      fail-fast: false
      matrix:
        system: [x64]
    container:
      image: savonet/liquidsoap-win32-deps-${{ matrix.system }}
      options: --user root -v ${{ github.workspace }}/${{ github.run_number }}:/tmp/${{ github.run_number }}
    env:
      OPAM_DEPS: ao-windows,lastfm-windows,camomile-windows,cry-windows,dtools-windows,duppy-windows,ffmpeg-windows,ffmpeg-avutil-windows,mm-windows,pcre-windows,portaudio-windows,samplerate-windows,sedlex-windows,ssl-windows,srt-windows,taglib-windows,winsvc-windows,mem_usage-windows,memtrace-windows
      IS_SNAPSHOT: ${{ needs.build_details.outputs.is_snapshot == 'true' }}
    steps:
      - name: Get number of CPU cores
        uses: savonet/github-actions-cpu-cores-docker@v1
        id: cpu_cores
      - name: Checkout code
        run: |
          mkdir -p /tmp/${{ github.run_number }}/win32/liquidsoap
          cd /tmp/${{ github.run_number }}/win32/liquidsoap
          git init
          git remote add origin https://github.com/${{ github.repository }}.git
          git fetch origin ${{ github.sha }}
          git checkout ${{ github.sha }}
          chown -R opam /tmp/${{ github.run_number }}/win32
      - name: Add local packages
        run: |
          cd /tmp/${{ github.run_number }}/win32/liquidsoap/
          gosu opam:root ./.github/scripts/add-local-opam-packages.sh
      - name: Build windows binary
        run: |
          mkdir -p /tmp/${{ github.run_number }}/win32/dist
          chown -R opam /tmp/${{ github.run_number }}/win32/dist
          chown -R opam "${GITHUB_OUTPUT}"
          cd /tmp/${{ github.run_number }}/win32/liquidsoap
          gosu opam:root ./.github/scripts/build-win32.sh ${{ matrix.system }} ${{ needs.build_details.outputs.branch }} "${{ steps.cpu_cores.outputs.count }}" "${{ needs.build_details.outputs.is_rolling_release }}" "${{ needs.build_details.outputs.is_release }}" ${{ github.sha }}
        id: build
      - name: Upload artifact
        uses: actions/upload-artifact@v3
        with:
          name: ${{ steps.build.outputs.basename }}
          path: ${{ github.workspace }}/${{ github.run_number }}/win32/dist
          if-no-files-found: error
      - name: Cleanup
        if: ${{ always() }}
        run: |
          rm -rf /tmp/${{ github.run_number }}/win32

  update_release:
    runs-on: ubuntu-latest
    needs:
      [
        build_details,
        build_no_depopts,
        build_js,
        run_tests,
        build_posix,
        build_win32,
        fetch_s3_artifacts,
      ]
    if: needs.build_details.outputs.is_release
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
      - name: Tag commit
        uses: savonet/latest-tag@any-context
        with:
          description: Liquidsoap ${{ needs.build_details.outputs.branch }}
          tag-name: ${{ needs.build_details.outputs.branch }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - name: Download all artifact
        uses: actions/download-artifact@v3
        with:
          path: artifacts/${{ needs.build_details.outputs.sha }}
      - name: List assets to upload
        run: |
          echo "RELEASE_ASSETS<<EOF" >> $GITHUB_ENV
          find artifacts/${{ needs.build_details.outputs.sha }} -type f | egrep '\.apk$|\.deb$|\.config|\.zip$' | sort -u >> $GITHUB_ENV
          echo "EOF" >> $GITHUB_ENV
      - name: Generate release notes
        run: |
          echo "RELEASE_NOTES<<EOF" >> $GITHUB_ENV
          if [ ${{ needs.build_details.outputs.is_rolling_release }} = "true" ]; then
            cat doc/content/rolling-release.md >> $GITHUB_ENV
          fi
          echo "EOF" >> $GITHUB_ENV
      - name: Generate changelog
        run: |
          echo "CHANGELOG<<EOF" >> $GITHUB_ENV
          cat CHANGES.md | sed -e "/---/,\$d" >> $GITHUB_ENV
          echo "EOF" >> $GITHUB_ENV
      - name: Delete old release assets
        uses: mknejp/delete-release-assets@v1
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          tag: ${{ needs.build_details.outputs.branch }}
          assets: "*"
          fail-if-no-release: false
          fail-if-no-assets: false
      - name: Upload assets to main repo release
        uses: softprops/action-gh-release@v1
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          tag_name: ${{ needs.build_details.outputs.branch }}
          files: ${{ env.RELEASE_ASSETS }}
          prerelease: ${{ needs.build_details.outputs.is_rolling_release }}
          body: "${{ env.RELEASE_NOTES}}\n\n${{ env.CHANGELOG }}"
          draft: true
      - name: Upload assets to release repo
        uses: softprops/action-gh-release@v1
        with:
          token: ${{ secrets.LIQUIDSOAP_RELEASE_ASSETS_TOKEN }}
          tag_name: ${{ needs.build_details.outputs.branch }}
          files: ${{ env.RELEASE_ASSETS }}
          repository: savonet/liquidsoap-release-assets
          prerelease: ${{ needs.build_details.outputs.is_rolling_release }}
          body_path: doc/content/release-assets.md
          draft: true

  build_docker:
    runs-on: ${{ matrix.runs-on }}
    needs: [build_details, run_tests, build_posix, fetch_s3_artifacts]
    if: needs.build_details.outputs.is_fork != 'true' && github.event_name != 'merge_group'
    strategy:
      fail-fast: false
      matrix:
        platform: ${{ fromJson(needs.build_details.outputs.build_platform) }}
        include: ${{ fromJson(needs.build_details.outputs.build_include) }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1
      - name: Download all artifact
        uses: actions/download-artifact@v3
        with:
          path: artifacts/${{ needs.build_details.outputs.sha }}
      - name: Get debian package
        run: |
          echo "deb-file=$(find artifacts/${{ needs.build_details.outputs.sha }} -type f | grep ${{ matrix.docker-debian-os }} | grep -v minimal | grep '${{ matrix.platform }}\.deb$' | grep -v dbgsym | grep deb)" >> "${GITHUB_OUTPUT}"
        id: debian_package
      - name: Get debian debug package
        run: |
          echo "deb-file=$(find artifacts/${{ needs.build_details.outputs.sha }} -type f | grep ${{ matrix.docker-debian-os }} | grep -v minimal | grep '${{ matrix.platform }}\.deb$' | grep dbgsym | grep deb)" >> "${GITHUB_OUTPUT}"
        id: debian_debug_package
      - name: Log in to the github registry
        run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin
      - name: Build docker image
        run: .github/scripts/build-docker.sh ${{ steps.debian_package.outputs.deb-file }} ${{ steps.debian_debug_package.outputs.deb-file }} ${{ needs.build_details.outputs.branch }} ${{ secrets.DOCKERHUB_USER }} ${{ secrets.DOCKERHUB_PASSWORD }} ${{ matrix.platform }} ${{ matrix.docker-platform }}

  build_docker_alpine:
    runs-on: ${{ matrix.runs-on }}
    needs: [build_details, run_tests, build_posix, fetch_s3_artifacts]
    if: needs.build_details.outputs.is_fork != 'true' && github.event_name != 'merge_group'
    strategy:
      fail-fast: false
      matrix:
        platform: ${{ fromJson(needs.build_details.outputs.build_platform) }}
        include: ${{ fromJson(needs.build_details.outputs.build_include) }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1
      - name: Download all artifact
        uses: actions/download-artifact@v3
        with:
          path: artifacts/${{ needs.build_details.outputs.sha }}
      - name: Get alpine package
        run: |
          echo "apk-file=$(find artifacts/${{ needs.build_details.outputs.sha }} -type f | grep -v minimal | grep 'apk$' | grep -v dbg | grep ${{ matrix.alpine-arch }})" >> "${GITHUB_OUTPUT}"
        id: alpine_package
      - name: Log in to the github registry
        run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin
      - name: Build docker image
        run: .github/scripts/build-docker-alpine.sh ${{ steps.alpine_package.outputs.apk-file }} ${{ needs.build_details.outputs.branch }} ${{ secrets.DOCKERHUB_USER }} ${{ secrets.DOCKERHUB_PASSWORD }} ${{ matrix.platform }} ${{ matrix.docker-platform }}

  build_docker_minimal:
    runs-on: ${{ matrix.runs-on }}
    needs: [build_details, run_tests, build_posix, fetch_s3_artifacts]
    if: needs.build_details.outputs.is_fork != 'true' && github.event_name != 'merge_group'
    strategy:
      fail-fast: false
      matrix:
        platform: ${{ fromJson(needs.build_details.outputs.build_platform) }}
        include: ${{ fromJson(needs.build_details.outputs.build_include) }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1
      - name: Download all artifact
        uses: actions/download-artifact@v3
        with:
          path: artifacts/${{ needs.build_details.outputs.sha }}
      - name: Get debian package
        run: |
          echo "deb-file=$(find artifacts/${{ needs.build_details.outputs.sha }} -type f | grep ${{ matrix.docker-debian-os }} | grep minimal | grep '${{ matrix.platform }}\.deb$' | grep -v dbgsym | grep deb)" >> "${GITHUB_OUTPUT}"
        id: debian_package
      - name: Get debian debug package
        run: |
          echo "deb-file=$(find artifacts/${{ needs.build_details.outputs.sha }} -type f | grep ${{ matrix.docker-debian-os }} | grep minimal | grep '${{ matrix.platform }}\.deb$' | grep dbgsym | grep deb)" >> "${GITHUB_OUTPUT}"
        id: debian_debug_package
      - name: Log in to the github registry
        run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin
      - name: Build docker image
        run: .github/scripts/build-docker.sh ${{ steps.debian_package.outputs.deb-file }} ${{ steps.debian_debug_package.outputs.deb-file }} ${{ needs.build_details.outputs.branch }}-minimal ${{ secrets.DOCKERHUB_USER }} ${{ secrets.DOCKERHUB_PASSWORD }} ${{ matrix.platform }} ${{ matrix.docker-platform }}

  build_docker_alpine_minimal:
    runs-on: ${{ matrix.runs-on }}
    needs: [build_details, run_tests, build_posix, fetch_s3_artifacts]
    if: needs.build_details.outputs.is_fork != 'true' && github.event_name != 'merge_group'
    strategy:
      fail-fast: false
      matrix:
        platform: ${{ fromJson(needs.build_details.outputs.build_platform) }}
        include: ${{ fromJson(needs.build_details.outputs.build_include) }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1
      - name: Download all artifact
        uses: actions/download-artifact@v3
        with:
          path: artifacts/${{ needs.build_details.outputs.sha }}
      - name: Get alpine package
        run: |
          echo "apk-file=$(find artifacts/${{ needs.build_details.outputs.sha }} -type f | grep minimal | grep 'apk$' | grep -v dbg | grep ${{ matrix.alpine-arch }})" >> "${GITHUB_OUTPUT}"
        id: alpine_package
      - name: Get alpine debug package
        run: |
          echo "apk-file=$(find artifacts/${{ needs.build_details.outputs.sha }} -type f | grep minimal | grep 'apk$' | grep dbg | grep ${{ matrix.alpine-arch }})" >> "${GITHUB_OUTPUT}"
        id: alpine_dbg_package
      - name: Log in to the github registry
        run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin
      - name: Build docker image
        run: .github/scripts/build-docker-alpine.sh ${{ steps.alpine_package.outputs.apk-file }} ${{ steps.alpine_dbg_package.outputs.apk-file }} ${{ needs.build_details.outputs.branch }}-minimal ${{ secrets.DOCKERHUB_USER }} ${{ secrets.DOCKERHUB_PASSWORD }} ${{ matrix.platform }} ${{ matrix.docker-platform }}

  build_docker_release:
    runs-on: ubuntu-latest
    needs: [build_details, build_docker, build_docker_alpine]
    if: needs.build_details.outputs.docker_release
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
      - name: Push consolidated manifest
        run: .github/scripts/push-docker.sh ${{ needs.build_details.outputs.branch }} ${{ secrets.DOCKERHUB_USER }} ${{ secrets.DOCKERHUB_PASSWORD }} ${{ github.actor }} ${{ secrets.GITHUB_TOKEN }} ${{ github.sha }}

  build_docker_release-minimal:
    runs-on: ubuntu-latest
    needs: [build_details, build_docker_minimal, build_docker_alpine_minimal]
    if: needs.build_details.outputs.docker_release
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
      - name: Push consolidated manifest
        run: .github/scripts/push-docker.sh ${{ needs.build_details.outputs.branch }}-minimal ${{ secrets.DOCKERHUB_USER }} ${{ secrets.DOCKERHUB_PASSWORD }} ${{ github.actor }} ${{ secrets.GITHUB_TOKEN }} ${{ github.sha }}-minimal
