name: 'ROOT CI'

on:
  schedule:
    - cron: '0 1 * * *'

  # https://github.com/root-project/root/pull/12112#issuecomment-1411004278
  pull_request:
    branches:
      - '**'
    paths-ignore:
      - 'doc/**'
      - 'documentation/**'
  
  push:
    branches: 
      - 'master'
      - 'v*-*-*-patches'

  # Allows nightly builds to trigger one run for each branch easily
  workflow_call:
    inputs:
      head_ref:
        type: string
        required: true
        default: master
      base_ref:
        type: string
        required: true
        default: master
      incremental:
        type: boolean
        required: true
        default: true

  # Enables manual start of workflow
  workflow_dispatch:
    inputs:
      head_ref:
        description: rebase from ...
        type: string
        required: true
        default: master
      base_ref:
        description: ... to ... (can have same value)
        type: string
        required: true
        default: master
      incremental:
        description: 'Do incremental build'
        type: boolean
        required: true
        default: true

env:
  PYTHONUNBUFFERED: true
  OS_APPLICATION_CREDENTIAL_ID: '7f5b64a265244623a3a933308569bdba'
  OS_APPLICATION_CREDENTIAL_SECRET: ${{ secrets.OS_APPLICATION_CREDENTIAL_SECRET }}
  OS_AUTH_TYPE: 'v3applicationcredential'
  OS_AUTH_URL: 'https://keystone.cern.ch/v3'
  OS_IDENTITY_API_VERSION: 3
  OS_INTERFACE: 'public'
  OS_REGION_NAME: 'cern'

jobs:
  build-linux:
    if: github.repository_owner == 'root-project'
    permissions:
      contents: read

    strategy:
      matrix:
        image: ["fedora37", "centos8", "ubuntu18", "ubuntu20", "ubuntu22"]
        config: ["Release"]
        #, "Debug", "RelWithDebInfo"]
      fail-fast: false

    runs-on: [self-hosted, linux, x64]

    container:
      image: registry.cern.ch/root-ci/${{ matrix.image }}:buildready
      options: '--security-opt label=disable'
      env:
        OS_APPLICATION_CREDENTIAL_ID: '7f5b64a265244623a3a933308569bdba'
        OS_APPLICATION_CREDENTIAL_SECRET: ${{ secrets.OS_APPLICATION_CREDENTIAL_SECRET }}
        OS_AUTH_TYPE: 'v3applicationcredential'
        OS_AUTH_URL: 'https://keystone.cern.ch/v3'
        OS_IDENTITY_API_VERSION: 3
        OS_INTERFACE: 'public'
        OS_REGION_NAME: 'cern'
        PYTHONUNBUFFERED: true

    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Dump GitHub context
        env:
          GITHUB_CONTEXT: ${{ toJSON(github) }}
        run: echo "$GITHUB_CONTEXT"

      - name: Print debug info
        run:  'printf "%s@%s\\n" "$(whoami)" "$(hostname)";
               ls -la
              '

      - name: Pull Request Build
        if:   github.event_name == 'pull_request' || github.event_name == 'pull_request_target'
        run: ".github/workflows/root-ci-config/build_root.py
                    --buildtype      ${{ matrix.config }}
                    --platform       ${{ matrix.image }}
                    --incremental    true
                    --base_ref       ${{ github.base_ref }}
                    --head_ref       refs/pull/${{ github.event.pull_request.number }}/head
                    --repository     ${{ github.server_url }}/${{ github.repository }}
              "

      - name: Workflow dispatch/call
        if:   github.event_name == 'workflow_dispatch'
        run: ".github/workflows/root-ci-config/build_root.py
                    --buildtype      ${{ matrix.config }}
                    --platform       ${{ matrix.image }}
                    --incremental    ${{ inputs.incremental }}
                    --base_ref       ${{ inputs.base_ref }}
                    --head_ref       ${{ inputs.head_ref }}
                    --repository     ${{ github.server_url }}/${{ github.repository }}
              "

      - name: Nightly build
        if:   github.event_name == 'schedule'
        run: ".github/workflows/root-ci-config/build_root.py
                    --buildtype      ${{ matrix.config }}
                    --platform       ${{ matrix.image }}
                    --incremental    false
                    --base_ref       ${{ github.base_ref }}
                    --repository     ${{ github.server_url }}/${{ github.repository }}
              "

      - name: Update artifacts after push to release branch
        if:   github.event_name == 'push'
        run: ".github/workflows/root-ci-config/build_root.py
                    --buildtype      ${{ matrix.config }}
                    --platform       ${{ matrix.image }}
                    --incremental    true
                    --base_ref       ${{ github.ref_name }}
                    --repository     ${{ github.server_url }}/${{ github.repository }}
              "
        
      - name: Upload test results
        uses: actions/upload-artifact@v3
        with:
          name:  Test Results ${{ matrix.image }} ${{ matrix.config }}
          path: /tmp/workspace/build/TestResults.xml

  event_file:
    name: "Upload Event Payload"
    runs-on: ubuntu-latest
    steps:
    - name: Upload
      uses: actions/upload-artifact@v3
      with:
        name: Event File
        path: ${{ github.event_path }}