name: CI

on:
  push:
    branches: 
      - master
      - integration
      - experimental*
      - release_*
      - ci*
      - pci* 
    tags:
      - v*
  pull_request:
  repository_dispatch:
    types:
      - metwork-trigger*

env:


    DOCKER_RUN_OPTIONS: "-v ${{ github.workspace }}:/src -e GITHUB_REF -e GITHUB_RUN_NUMBER -e GITHUB_SHA -e GITHUB_REPOSITORY"

    WORKFLOW_SCRIPTS_DIR: .github/workflows
    DOCKER_WORKFLOW_SCRIPTS_DIR: /src/.github/workflows
    PAYLOAD_BRANCH: ${{ github.event.client_payload.branch}}
    PAYLOAD_OS: ${{ github.event.client_payload.os}}

jobs:
  ci_build:


    runs-on: ubuntu-latest

    steps:
      - name: checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
          ref: ${{ github.event.client_payload.branch || github.ref }}
      - name: set variables
        id: SetVariables
        run: |
          ${WORKFLOW_SCRIPTS_DIR}/set_variables.sh
      - name: debug env
        run: |
          env
      - name: bootstrap
        if: ${{ steps.SetVariables.outputs.branch != 'null' }}
        run: |
          TARGET_DIR=${{ steps.SetVariables.outputs.target_dir }}
          BRANCH=${{ steps.SetVariables.outputs.branch }}
          OS_VERSION=${{ steps.SetVariables.outputs.os }}
          TAG=${{ steps.SetVariables.outputs.tag }}
          DEP_DIR=${{ steps.SetVariables.outputs.dep_dir }}
          BUILDIMAGE=${{ steps.SetVariables.outputs.buildimage }}
          docker pull ${BUILDIMAGE}
          docker run ${DOCKER_RUN_OPTIONS} -e TARGET_DIR=${TARGET_DIR} -e BRANCH=${BRANCH} -e TAG=${TAG} -e DEP_DIR=${DEP_DIR} -e OS_VERSION=${OS_VERSION} --rm ${BUILDIMAGE} /bin/bash -c "${DOCKER_WORKFLOW_SCRIPTS_DIR}/bootstrap_ci.sh"
      - name: build
        if: ${{ steps.SetVariables.outputs.branch != 'null' }}
        id: BuildStep
        run: |
          TARGET_DIR=${{ steps.SetVariables.outputs.target_dir }}
          BRANCH=${{ steps.SetVariables.outputs.branch }}
          OS_VERSION=${{ steps.SetVariables.outputs.os }}
          TAG=${{ steps.SetVariables.outputs.tag }}
          DEP_BRANCH=${{ steps.SetVariables.outputs.dep_branch }}
          BUILDIMAGE=${{ steps.SetVariables.outputs.buildimage }}
          docker pull ${BUILDIMAGE}
          docker run ${DOCKER_RUN_OPTIONS} -e TARGET_DIR=${TARGET_DIR} -e BRANCH=${BRANCH} -e DEP_BRANCH=${DEP_BRANCH} -e TAG=${TAG} -e OS_VERSION=${OS_VERSION} --rm ${BUILDIMAGE} /bin/bash -c "${DOCKER_WORKFLOW_SCRIPTS_DIR}/build.sh"
          cat  ${{ github.workspace }}/github_output >> ${GITHUB_OUTPUT}

      - name: publish buildlogs
        if: ${{ github.event_name != 'pull_request' && steps.SetVariables.outputs.branch != 'null' }}
        uses: thebaptiste/ssh-scp-ssh-pipelines@metwork
        env:
          REMOTE: ${{ steps.SetVariables.outputs.buildlog_dir }}
          LOCAL: ${{ github.workspace }}/buildlogs
        with:
          host: ${{ secrets.METWORKPUB_HOST }}
          user: ${{ secrets.METWORKPUB_USERNAME }}
          pass: ${{ secrets.METWORKPUB_PASSWORD }}
          port: 22
          first_ssh: |
            mkdir -p ${REMOTE}
          scp: |
            '${LOCAL}/*' => ${REMOTE}
      - name: integration tests
        if: ${{ steps.SetVariables.outputs.branch != 'null' && steps.BuildStep.outputs.bypass == 'false' }}
        run: |
          TESTIMAGE=${{ steps.SetVariables.outputs.testimage }}
          DEP_BRANCH=${{ steps.SetVariables.outputs.dep_branch }}
          BRANCH=${{ steps.SetVariables.outputs.branch }}
          OS_VERSION=${{ steps.SetVariables.outputs.os }}
          docker pull ${TESTIMAGE}
          docker run ${DOCKER_RUN_OPTIONS} -e DEP_BRANCH=${DEP_BRANCH} -e BRANCH=${BRANCH} -e OS_VERSION=${OS_VERSION} --rm ${TESTIMAGE} /bin/bash -c "${DOCKER_WORKFLOW_SCRIPTS_DIR}/integration_tests.sh"
      - name: publish rpms
        if: ${{ github.event_name != 'pull_request' && steps.BuildStep.outputs.bypass == 'false' && steps.SetVariables.outputs.branch != 'null' }}
        uses: thebaptiste/ssh-scp-ssh-pipelines@metwork
        env:
          REMOTE: ${{ steps.SetVariables.outputs.rpm_dir }}
          LOCAL: ${{ github.workspace }}/rpms
        with:
          host: ${{ secrets.METWORKPUB_HOST }}
          user: ${{ secrets.METWORKPUB_USERNAME }}
          pass: ${{ secrets.METWORKPUB_PASSWORD }}
          port: 22
          first_ssh: |
            mkdir -p ${REMOTE}
          scp: |
            '${LOCAL}/metwork-*.rpm' => ${REMOTE}
      - name: createrepo
        if: ${{ github.event_name != 'pull_request' && steps.BuildStep.outputs.bypass == 'false' && steps.SetVariables.outputs.branch != 'null' }}
        uses: thebaptiste/ssh-scp-ssh-pipelines@metwork
        env:
          REMOTE: ${{ steps.SetVariables.outputs.rpm_dir }}
          LOCAL: ${{ github.workspace }}/.github/workflows
        with:
          host: ${{ secrets.METWORKPUB_HOST }}
          user: ${{ secrets.METWORKPUB_USERNAME }}
          pass: ${{ secrets.METWORKPUB_PASSWORD }}
          port: 22
          scp: |
            '${LOCAL}/createrepo.sh' => ${REMOTE}
          last_ssh: |
            cd ${REMOTE}
            chmod +x createrepo.sh
            ./createrepo.sh .
      - name: publish doc
        if: ${{ github.event_name != 'pull_request' && steps.BuildStep.outputs.bypass == 'false' && steps.SetVariables.outputs.branch != 'null' }}
        uses: thebaptiste/ssh-scp-ssh-pipelines@metwork
        env:
          REMOTE: ${{ steps.SetVariables.outputs.doc_dir }}
          LOCAL: ${{ github.workspace }}/html_doc
        with:
          host: ${{ secrets.METWORKPUB_HOST }}
          user: ${{ secrets.METWORKPUB_USERNAME }}
          pass: ${{ secrets.METWORKPUB_PASSWORD }}
          port: 22
          first_ssh: |
            rm -rf ${REMOTE}
            mkdir -p ${REMOTE}
          scp: |
            '${LOCAL}/*' => ${REMOTE}