stages:
  - host_test
  - deploy

image: ${CI_DOCKER_REGISTRY}/esp-env-v5.0:2

variables:
  CMAKE_SH: cmake-3.22.3-linux-x86_64.sh
  CC: cc

before_script:
  # Use CI Tools
  - curl -sSL ${CIT_LOADER_URL} | sh
  - source citools/import_functions

.get_cmake: &get_cmake |
  wget -q https://github.com/Kitware/CMake/releases/download/v3.22.3/${CMAKE_SH}
  mkdir -p /opt/cmake && sh ${CMAKE_SH} --prefix=/opt/cmake --skip-license
  export PATH=/opt/cmake/bin:$PATH

run_unittests:
  stage: host_test
  tags:
    - host_test
  dependencies: []
  script:
    - ./test/ci/unit_tests.sh

build_all:
  stage: host_test
  tags:
    - host_test
  script:
    - cp contrib/examples/example_app/lwipcfg.h.ci contrib/examples/example_app/lwipcfg.h
    - mkdir build && cd build && cmake .. -G Ninja
    - cmake --build .
    - cmake --build . --target lwipdocs

validate_opts:
  stage: host_test
  tags:
    - host_test
  script:
    - ./test/ci/validate_opts.sh

run_test_apps:
  stage: host_test
  tags:
    - host_test
  script:
    - *get_cmake
    - ./test/ci/test_apps.sh

.add_gh_key_remote: &add_gh_key_remote |
  command -v ssh-agent >/dev/null || exit 1
  eval $(ssh-agent -s)
  printf '%s\n' "${GH_PUSH_KEY}" | tr -d '\r' | ssh-add - > /dev/null
  mkdir -p ~/.ssh && chmod 700 ~/.ssh
  [[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config || ssh-keyscan -t rsa github.com >> ~/.ssh/known_hosts
  git remote remove github || true
  git remote add github ${GH_PUSH_REPO}

push_master_to_github:
  stage: deploy
  only:
    - 2.1.2-esp
    - 2.1.3-esp
  when: on_success
  variables:
    GIT_STRATEGY: clone
  script:
    - *add_gh_key_remote
    - "[ -n \"${CI_COMMIT_TAG:-}\" ] && git push github ${CI_COMMIT_TAG}"
    - "[ -z \"${CI_COMMIT_TAG:-}\" ] && git push github ${CI_COMMIT_SHA}:refs/heads/${CI_COMMIT_REF_NAME}"

