---
variables:
  E3_CI_MODIFIED_BASE_VERSION: "7.0.8.1-68332f4-20240917T140931"  # strip the first part of the tag name - see E3-1529

stages:
  - check
  - build
  - test

default:
  tags:
    - docker
  image: registry.esss.lu.se/ics-docker/centos-e3:latest

include:
  - project: "ics-infrastructure/gitlab-ci-yml"
    file: "PreCommit.gitlab-ci.yml"

shellcheck:
  image: pipelinecomponents/shellcheck:latest
  stage: check
  before_script:
    - shellcheck --version
  script:
    # anything ending on .*sh, should be shell script
    - |
      find . -name .git -type d -prune -o -type f -name \*.\*sh -print0 |
      xargs -0 -P $(nproc) -r -n1 shellcheck --format=gcc -x
    # magic, any file with a valid shebang should be scanned aswell
    - |
      find . -name .git -type d -prune -o -type f -regex '.*/[^.]*$' -print0 |
      xargs -0 -P $(nproc) -r -n1 sh -c 'FILE="$0"; if head -n1 "$FILE" |grep -q "^#\\! \?/.\+\(ba|d|k\)\?sh" ; then shellcheck "$FILE" --format=gcc -x ; else /bin/true ; fi '

shfmt:
  image: mvdan/shfmt:v3.2.0-alpine
  stage: check
  before_script:
    - shfmt -version
  script:
    - shfmt -i 2 -ci -d . # 2 spaces indent, indent switch-case statements, show diff (don't apply change)

build require:
  stage: build
  before_script:
    - curl -L -o epics.tar.gz https://artifactory.esss.lu.se/artifactory/e3/epics-base-$E3_CI_MODIFIED_BASE_VERSION.tar.gz
    - tar -zxvf epics.tar.gz
    - export BASE_PATH=$(ls -d $(pwd)/epics/base-*)
    - echo "EPICS_BASE:=${BASE_PATH}" >> configure/RELEASE.local
    - echo "E3_REQUIRE_VERSION:=${CI_COMMIT_SHORT_SHA}" >> configure/CONFIG_MODULE.local
    - chmod +w "${BASE_PATH}"/configure/CONFIG_SITE
    - echo 'INSTALL_LOCATION:=$(TOP)' >> "${BASE_PATH}"/configure/CONFIG_SITE
    - export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:${BASE_PATH}/lib/linux-x86_64"
    - make vars
  script:
    - make build
    - make install
  artifacts:
    paths:
      - epics
      - configure/*.local

test require:
  stage: test
  before_script:
    - source $(pwd)/epics/base-*/require/*/bin/activate
    # We need to allow file:// protocol for all of the tests to work
    - git config --global procotol.file.allow always
  script:
    - make test
  needs:
    - build require
