name: Linux

on:
  schedule:
    - cron: '0 0 * * 1'
  push:
    branches: [ '*' ]
  pull_request:
    branches: [ master ]

  release:
    types:
      - published
      - prereleased

env:
  BUILD_TYPE: Release
  CLICKHOUSE_SERVER_IMAGE: "clickhouse/clickhouse-server:22.3"

jobs:
  build:
    strategy:
      fail-fast: false
      matrix:
        compiler: [clang-11, clang-14, clang-18, gcc-9, gcc-12, gcc-14]
        ssl: [ssl_ON, ssl_OFF]
        dependencies: [dependencies_BUILT_IN]

        include:
        - compiler: clang-11
          os: ubuntu-22.04
          COMPILER_INSTALL: clang-11 libc++-11-dev
          C_COMPILER: clang-11
          CXX_COMPILER: clang++-11

        - compiler: clang-14
          os: ubuntu-24.04
          COMPILER_INSTALL: clang-14 libc++-14-dev
          C_COMPILER: clang-14
          CXX_COMPILER: clang++-14

        - compiler: clang-18
          os: ubuntu-24.04
          COMPILER_INSTALL: clang-18 libc++-18-dev
          C_COMPILER: clang-18
          CXX_COMPILER: clang++-18

        - compiler: gcc-9
          os: ubuntu-22.04
          COMPILER_INSTALL: gcc-9 g++-9
          C_COMPILER: gcc-9
          CXX_COMPILER: g++-9

        - compiler: gcc-12
          os: ubuntu-24.04
          COMPILER_INSTALL: gcc-12 g++-12
          C_COMPILER: gcc-12
          CXX_COMPILER: g++-12

        - compiler: gcc-14
          os: ubuntu-24.04
          COMPILER_INSTALL: gcc-14 g++-14
          C_COMPILER: gcc-14
          CXX_COMPILER: g++-14

        - ssl: ssl_ON
          SSL_CMAKE_OPTION: -D WITH_OPENSSL=ON

        - dependencies: dependencies_SYSTEM
          compiler: compiler_SYSTEM
          os: ubuntu-24.04
          COMPILER_INSTALL: gcc g++
          C_COMPILER: gcc
          CXX_COMPILER: g++
          DEPENDENCIES_INSTALL: libabsl-dev liblz4-dev
          DEPENDENCIES_CMAKE_OPTIONS: >-
            -D WITH_SYSTEM_LZ4=ON
            -D WITH_SYSTEM_ABSEIL=ON

    runs-on: ${{matrix.os}}

    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 100
          fetch-tags: true

      - name: Install dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y \
            cmake \
            ${{matrix.COMPILER_INSTALL}} \
            ${{matrix.DEPENDENCIES_INSTALL}}

      - name: Install dependencies - Docker
        run: |
          sudo apt remove -y docker docker-engine docker.io containerd runc
          sudo apt install -y apt-transport-https ca-certificates curl gnupg lsb-release
          curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
          echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
          sudo apt update -q
          sudo apt install docker-ce docker-ce-cli containerd.io
          sudo docker run hello-world

      - name: Configure project
        run: |
          cmake \
            -D CMAKE_C_COMPILER=${{matrix.C_COMPILER}} \
            -D CMAKE_CXX_COMPILER=${{matrix.CXX_COMPILER}} \
            -D CMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} \
            -D BUILD_TESTS=ON \
            ${{matrix.SSL_CMAKE_OPTION}} \
            ${{matrix.DEPENDENCIES_CMAKE_OPTIONS}} \
            -S ${{github.workspace}} \
            -B ${{github.workspace}}/build

      - name: Build project
        run: |
          cmake \
            --build ${{github.workspace}}/build \
            --config ${{env.BUILD_TYPE}} \
            --target all

      - name: Test - Start ClickHouse server in background
        run: |
          docker pull ${CLICKHOUSE_SERVER_IMAGE}
          docker run -d --name clickhouse -p 9000:9000 ${CLICKHOUSE_SERVER_IMAGE}
          docker ps -a
          docker stats -a --no-stream
          ## Check and wait until CH is ready to accept connections
          docker exec clickhouse bash -c 'for i in {1..10}; do echo checking if clickhouse server is started  attempt \#$i; if ( grep -q "<Information> Application: Ready for connections." /var/log/clickhouse-server/clickhouse-server.log ); then echo seems like clickhouse server is started; exit 0; fi; sleep 1; done; exit -1'

      - name: Test
        working-directory: ${{github.workspace}}/build/ut
        run: ./clickhouse-cpp-ut
