{{=[[ ]]=}}name: [[ OS_NAME ]] CI

env:
  CLOUDSMITH_API_KEY: ${{ secrets.CLOUDSMITH_API_KEY }}
  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

on:
  push:
    branches: ["main"]
    tags: ["**"]
  pull_request:
    branches: ["main"]

jobs:
  build-and-test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        include:
          [[#strategy]]
          - OS_NAME: "[[ OS_NAME ]]"
            OS_VERSION: "[[ OS_VERSION ]]"
            LLVM_VERSION: [[ LLVM_VERSION ]]
          [[/strategy]]
    name: [[ OS_NAME ]] ${{ matrix.OS_VERSION }} - LLVM ${{ matrix.LLVM_VERSION }}
    steps:
      - uses: actions/checkout@v4
      - uses: ./actions/detect-package-metadata
        id: metadata
      - uses: docker/login-action@v2
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - name: Build, test, package
        uses: devcontainers/ci@v0.3
        with:
          imageName: ghcr.io/mull-project/internal-${{ matrix.OS_NAME }}_${{ matrix.OS_VERSION }}_${{ matrix.LLVM_VERSION }}-ci
          configFile: ./.devcontainer/${{ matrix.OS_NAME }}_${{ matrix.OS_VERSION }}_${{ matrix.LLVM_VERSION }}/devcontainer.json
          runCmd: |
            cmake --preset mull -DMULL_VERSION=${{ steps.metadata.outputs.version }}${{ steps.metadata.outputs.suffix }}
            cmake --workflow --preset build-and-test
            cmake --workflow --preset package
            ./end2end-tests/run_end2end_tests.sh ${{ matrix.LLVM_VERSION }}

      - name: Publish package
        uses: devcontainers/ci@v0.3
        if: env.CLOUDSMITH_API_KEY != null
        with:
          imageName: ghcr.io/mull-project/internal-${{ matrix.OS_NAME }}_${{ matrix.OS_VERSION }}_${{ matrix.LLVM_VERSION }}-ci
          configFile: ./.devcontainer/${{ matrix.OS_NAME }}_${{ matrix.OS_VERSION }}_${{ matrix.LLVM_VERSION }}/devcontainer.json
          push: never
          runCmd: |
            UBUNTU_CODENAME=$(. /etc/os-release; echo "${UBUNTU_CODENAME/*, /}")
            env CLOUDSMITH_API_KEY=${{ secrets.CLOUDSMITH_API_KEY }} cloudsmith push deb \
              mull-project/mull-${{ steps.metadata.outputs.channel }}/${{ matrix.OS_NAME }}/${UBUNTU_CODENAME} \
              build.dir/*.deb

      - name: Move package
        run: |
          mkdir -p /tmp/packages
          sudo mv build.dir/*.deb /tmp/packages/

      - uses: ./actions/attach-package
        with:
          GH_API_KEY: ${{ secrets.GITHUB_TOKEN }}
