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

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

jobs:
  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: Install pandoc
        run: |
          cd /tmp
          git clone https://github.com/smimram/ocaml-pandoc.git
          opam pin -y add ocaml-pandoc
      - 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 submodule foreach 'git reset --hard'
          git pull
          cp PACKAGES.minimal PACKAGES
          opam update
          opam pin -yn .
          opam install -y saturn_lockfree.0.4.1 ppx_hash
          opam info -f "depopts:" liquidsoap-core | grep -v osx-secure-transport | xargs opam remove -y inotify ffmpeg-avutil cohttp-lwt-unix prometheus-app ${{ needs.build_details.outputs.minimal_exclude_deps }}
          opam install -y mem_usage
          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)"
          opam update
          opam install -y saturn_lockfree.0.4.1
          dune build --profile release ./src/js/interactive_js.bc.js

  tree_sitter_parse:
    runs-on: ubuntu-latest
    needs: build_details
    steps:
      - name: Checkout latest code
        uses: actions/checkout@v3
      - name: Setup node
        uses: actions/setup-node@v3
        with:
          node-version: latest
      - name: Parse using tree-sitter
        run: |
          git clone https://github.com/savonet/tree-sitter-liquidsoap.git
          cd tree-sitter-liquidsoap
          npm install
          npm exec tree-sitter -- parse -q -s ../../**/*.liq

  lezer_parse:
    runs-on: ubuntu-latest
    needs: build_details
    steps:
      - name: Checkout latest code
        uses: actions/checkout@v3
      - name: Setup node
        uses: actions/setup-node@v3
        with:
          node-version: latest
      - name: Parse using liquidsoap-lezer-print-tree
        run: |
          # This one has unicode variable name that isn't supported yet.
          rm -rf src/libs/list.liq
          git clone https://github.com/savonet/codemirror-lang-liquidsoap.git
          cd codemirror-lang-liquidsoap
          npm install
          npm exec liquidsoap-lezer-print-tree -- -q ../../**/*.liq

  update_doc:
    runs-on: ubuntu-latest
    needs: build_details
    if: github.event_name != 'pull_request' && 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: Install node
        run: |
          apt-get update
          apt-get install -y npm
      - name: Install pandoc
        run: |
          cd /tmp
          sudo -u opam -E git clone https://github.com/smimram/ocaml-pandoc.git
          sudo -u opam -E opam pin -y add ocaml-pandoc
      - name: Build doc
        env:
          CPU_CORES: ${{ steps.cpu_cores.outputs.count }}
        run: |
          cd /tmp/liquidsoap-full/liquidsoap
          # TMP
          sudo -u opam -E ./.github/scripts/add-local-opam-packages.sh
          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 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@v4
        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: ubuntu-latest
    needs: build_details
    container:
      image: savonet/liquidsoap-ci:debian_bookworm_amd64
      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", "@doctest", "@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: Install sqlite
        run: |
          sudo apt-get -y install libsqlite3-dev
          sudo -u opam -E opam install -y sqlite3
      - name: Install pandoc
        run: |
          cd /tmp
          if [ ! -d ocaml-pandoc ]; then
            mkdir -p ocaml-pandoc;
            chown -R opam ocaml-pandoc;
            sudo -u opam -E git clone https://github.com/smimram/ocaml-pandoc.git ocaml-pandoc
          fi
          sudo -u opam -E opam pin -y add ocaml-pandoc
      - name: Build
        env:
          CPU_CORES: ${{ steps.cpu_cores.outputs.count }}
        run: |
          cd /tmp/liquidsoap-full/liquidsoap
          # TMP
          sudo -u opam -E ./.github/scripts/add-local-opam-packages.sh
          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: Install additional packages
        if: matrix.target == '@doctest'
        run: |
          apt-get -y update
          apt-get -y install frei0r-plugins
          sudo -u opam -E opam install -y prometheus-liquidsoap
      - name: Run tests
        env:
          CPU_CORES: ${{ steps.cpu_cores.outputs.count }}
        run: |
          cd /tmp/liquidsoap-full/liquidsoap
          sudo -u opam -E ./.github/scripts/test-posix.sh ${{ matrix.target }}
      - name: Finalize metrics
        run: |
          cd /tmp/liquidsoap-full/liquidsoap
          mkdir -p "/tmp/${{ github.run_number }}/metrics"
          chown -R opam "/tmp/${{ github.run_number }}/metrics"
          sudo -u opam -E ./.github/scripts/export-metrics.sh "${{ needs.build_details.outputs.branch }}" "/tmp/${{ github.run_number }}/metrics"
      - name: Upload metrics artifact
        uses: actions/upload-artifact@v4
        if: needs.build_details.outputs.is_fork != 'true' && matrix.target == '@citest'
        with:
          name: metrics
          path: ${{ github.workspace }}/${{ github.run_number }}/metrics
      - name: Upload metrics
        if: needs.build_details.outputs.is_fork != 'true' && matrix.target == '@citest'
        uses: peaceiris/actions-gh-pages@v3
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_branch: metrics
          publish_dir: ${{ github.workspace }}/${{ github.run_number }}/metrics
          keep_files: true
      - 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@v4
        with:
          name: traces-${{ matrix.target }}
          path: ${{ github.workspace }}/${{ github.run_number }}/traces/${{ matrix.target }}
      - name: Export potential core dumps
        uses: actions/upload-artifact@v4
        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
    strategy:
      fail-fast: false
      matrix:
        ocaml-compiler:
          - 4.14.x
          - 5.2.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 .
      - name: Install ocamlformat
        if: matrix.ocaml-compiler == '4.14.x'
        run: |
          opam install ocamlformat=0.25.1
      - name: Set PY env variable.
        if: matrix.ocaml-compiler == '4.14.x'
        run: echo "PY=$(python -VV | sha256sum | cut -d' ' -f1)" >> $GITHUB_ENV
      - name: Restore pre-commit cache
        uses: actions/cache@v3
        with:
          path: ~/.cache/pre-commit
          key: pre-commit|${{ env.PY }}|${{ hashFiles('.pre-commit-config.yaml') }}
      - name: Run pre-commit
        if: matrix.ocaml-compiler == '4.14.x'
        uses: pre-commit/action@v3.0.0

  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) }}
    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
          sudo apt-get -y install libsqlite3-dev
      - name: Update alpine packages
        if: matrix.os == 'alpine'
        run: |
          apk -U --force-overwrite upgrade
          apk add -i sqlite-dev
      - name: Install additional libraries
        run: |
          sudo -u opam -E opam install -y sqlite3
      - name: Install pandoc
        run: |
          cd /tmp
          sudo -u opam -E git clone https://github.com/smimram/ocaml-pandoc.git ocaml-pandoc
          sudo -u opam -E opam pin -y add ocaml-pandoc
      - 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 ./.github/scripts/build-posix.sh "${{ steps.cpu_cores.outputs.count }}"
      - 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'))
        uses: actions/upload-artifact@v4
        with:
          name: ${{ steps.build_deb.outputs.basename }}
          path: ${{ github.workspace }}/${{ github.run_number }}/${{ matrix.os }}_${{ matrix.platform }}/debian
          if-no-files-found: error
      - 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@v4
        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,re-windows,portaudio-windows,samplerate-windows,sedlex-windows,ssl-windows,srt-windows,winsvc-windows,mem_usage-windows
      IS_SNAPSHOT: ${{ needs.build_details.outputs.is_snapshot == 'true' }}
      TOOLPREF64: /usr/src/mxe/usr/bin/x86_64-w64-mingw32.static-
    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@v4
        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@v4
        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, 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@v4
        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@v4
        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@v4
        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@v4
        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, run_tests, 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
