image: tecnalia-docker-dev.artifact.tecnalia.com/docker:git

variables:
  TMPDIR: /builds/${CI_PROJECT_NAMESPACE}
  GIT_LFS_SKIP_SMUDGE: 1 # Avoid fetching the files from LFS when doing git fetch

services:
  - name: tecnalia-docker-dev.artifact.tecnalia.com/docker:dind
    alias: docker

stages:
  - build
  - postcheck

before_script:
  - apk add --update bash coreutils tar wget
  - docker login tecnalia-docker-dev.artifact.tecnalia.com -u ${ARTIFACT_CI_USER} -p ${ARTIFACT_CI_TOKEN}

.industrial_ci:
  stage: build
  before_script:
    - apk add --update bash coreutils tar
    - git clone --quiet --depth 1 https://github.com/ros-industrial/industrial_ci.git .ci_config -b legacy
    - docker login ${ARTIFACT_DOCKER_URL} -u ${ARTIFACT_CI_USER} -p ${ARTIFACT_CI_TOKEN}
    - docker login tecnalia-docker-dev.artifact.tecnalia.com -u ${ARTIFACT_CI_USER} -p ${ARTIFACT_CI_TOKEN}
  script: .ci_config/gitlab.sh
  variables:
    ADDITIONAL_DEBS: curl
    ROS_PARALLEL_JOBS: "-j2" # keep this for now, since there seems to be memory limit issues in the ci VM
    UPSTREAM_WORKSPACE: file
    ROSINSTALL_FILENAME: ".rosinstall"
    BEFORE_SCRIPT: "bash <(curl -Ls https://git.code.tecnalia.com/tecnalia_robotics-public/gitlab_templates/raw/master/scripts/ci_run_entry_points.sh) $$CATKIN_WORKSPACE"
    CATKIN_CONFIG: '--cmake-args -DCMAKE_CXX_FLAGS=-Wno-ignored-attributes -Wno-int-in-bool-context --'
  when: always
  tags:
    - docker

industrial_ci_kinetic:
  extends: .industrial_ci
  variables:
    ROS_DISTRO: kinetic
    DOCKER_IMAGE: "${ARTIFACT_DOCKER_URL}/flexbotics-base-devel:${ROS_DISTRO}"
  except:
    - tags
    - melodic-devel

industrial_ci_melodic:
  extends: .industrial_ci
  variables:
    ROS_DISTRO: melodic
    DOCKER_IMAGE: "${ARTIFACT_DOCKER_URL}/flexbotics-base-devel:${ROS_DISTRO}"
  only:
    - melodic-devel
  except:
    - tags

industrial_ci_kinetic_deploy:
  extends: .industrial_ci
  variables:
    ROS_DISTRO: kinetic
    DOCKER_IMAGE: "${ARTIFACT_DOCKER_URL}/flexbotics-base-devel:${ROS_DISTRO}"
    AFTER_SCRIPT: "wget -q -O - https://git.code.tecnalia.com/tecnalia_robotics-public/gitlab_templates/raw/master/scripts/pre-deploy-cleanup.bash | bash"
    DOCKER_COMMIT: industrial_ci_image
  script:
    - .ci_config/gitlab.sh
    - cd $(mktemp -d)
    - wget https://git.code.tecnalia.com/tecnalia_robotics-public/gitlab_templates/-/archive/master/gitlab_templates-master.zip
    - unzip gitlab_templates-master.zip && cd gitlab_templates-master/deploy
    - cd base
    - chmod a+x ros_entrypoint.bash
    - docker build -t ${ARTIFACT_DOCKER_URL}/${CI_PROJECT_NAME}:${CI_COMMIT_REF_NAME} .
    - docker push ${ARTIFACT_DOCKER_URL}/${CI_PROJECT_NAME}:${CI_COMMIT_REF_NAME}
    - cd ..
    - cd dev
    - docker build --build-arg APPLICATION_IMAGE=${ARTIFACT_DOCKER_URL}/${CI_PROJECT_NAME}:${CI_COMMIT_REF_NAME} -t ${ARTIFACT_DOCKER_URL}/${CI_PROJECT_NAME}:${CI_COMMIT_REF_NAME}-dev .
    - docker push ${ARTIFACT_DOCKER_URL}/${CI_PROJECT_NAME}:${CI_COMMIT_REF_NAME}-dev
    - cd ..
  only:
    - tags

clang_format:
  stage: postcheck
  before_script:
    - apk add --update bash coreutils tar wget
    - git clone --quiet --depth 1 https://github.com/ros-industrial/industrial_ci.git .ci_config -b legacy
    - wget https://git.code.tecnalia.com/tecnalia_robotics-public/gitlab_templates/raw/master/clang-format -O .clang-format
  script: .ci_config/gitlab.sh
  variables:
    ROS_DISTRO: kinetic
    CLANG_FORMAT_CHECK: file
    CLANG_FORMAT_VERSION: "3.8"
  when: always
  tags:
    - docker

##########################################################
## Simple check for basic Python syntax errors; this is much less thorough than
## other options like pylint, but much faster and less strict
##########################################################
python_syntax:
  before_script:
    - apk add --update python2
  stage: build
  script: python -m compileall -q .
  when: always
  tags:
    - docker

##########################################################
## Check for errors in bash/sh scripts
## Uses https://github.com/koalaman/shellcheck
##########################################################
bash_syntax:
  before_script:
    - apk add --update shellcheck
  stage: postcheck
  script:
    - RET=0
    - find . -type f -name '*.sh' -exec shellcheck -s sh {} + || RET=1
    - find . -type f -name '*.bash' -exec shellcheck -s bash {} + || RET=1
    - exit $RET
  when: always
  tags:
    - docker

##########################################################
## Check for errors in markdown files
## Uses https://github.com/cytopia/awesome-ci
## Disables the line length rule with --custom="-r ..."
## See other rules that can be configured in
## https://github.com/markdownlint/markdownlint/blob/master/docs/RULES.md
##########################################################
markdown_syntax:
  stage: postcheck
  script: docker run -v ${PWD}:/ac tecnalia-docker-dev.artifact.tecnalia.com/cytopia/awesome-ci syntax-markdown --path=/ac --extension=md --ignore=.ci_config --custom="-r ~MD013,~MD014"
  when: always
  tags:
    - docker

##########################################################
## Check for various syntax issues in files; currently
##  trailing white spaces
##  utf8 formatting
## Uses https://github.com/cytopia/awesome-ci
##########################################################
file_syntax:
  stage: postcheck
  script:
    - RET=0
    - docker run -v ${PWD}:/ac tecnalia-docker-dev.artifact.tecnalia.com/cytopia/awesome-ci file-trailing-space --path=/ac --extension=py,txt,cpp,h,md,sh,bash,xml,launch --ignore=.ci_config || RET=1
    - docker run -v ${PWD}:/ac tecnalia-docker-dev.artifact.tecnalia.com/cytopia/awesome-ci file-utf8 --path=/ac --extension=py,txt,cpp,h,md,sh,bash,xml,launch --ignore=.ci_config || RET=1
    - exit $RET
  when: always
  tags:
    - docker

##########################################################
## Check for errors in Asciidoctor files
## Could not find a proper linter for Asciidoctor format,
## so this is a custom-made "dirty" script
##########################################################
adoc_syntax:
  stage: postcheck
  image: tecnalia-docker-dev.artifact.tecnalia.com/alpine
  before_script:
    - apk add --update curl asciidoctor bash
  script:
    - curl -Ls https://git.code.tecnalia.com/tecnalia_robotics-public/gitlab_templates/raw/master/scripts/asciidoctor_syntax_check.bash | bash -s -- .
  tags:
    - docker
  when: always
