.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
    - 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:
    DOCKER_IMAGE: "${ARTIFACT_DOCKER_URL}/flexbotics-base-devel:${ROS_DISTRO}"
    ADDITIONAL_DEBS: curl
    PARALLEL_BUILDS: 2 # keep this for now, since there seems to be memory limit issues in the ci VM
    AFTER_SETUP_TARGET_WORKSPACE: "rosenv && bash <(curl -Ls https://git.code.tecnalia.com/tecnalia_robotics-public/gitlab_templates/raw/master/scripts/ci_run_entry_points.sh) $$current_ws"
    CMAKE_ARGS: -DCMAKE_CXX_FLAGS=-Wno-ignored-attributes -Wno-int-in-bool-context


.ddeploy:
  stage: deploy
  variables:
    DOCKER_PUSH_REGISTRY: ${ARTIFACT_DOCKER_URL}
    DOCKER_PUSH_NAME: ${CI_PROJECT_PATH}
    DOCKER_PUSH_TAG: ${CI_COMMIT_REF_SLUG}
    DDEPLOY_YAML: ddeploy.yaml
  before_script:
    - apk add --update python3 git py3-pip git-lfs
    # forward the SSH authentication into the Docker executor
    - "which ssh-agent || ( apk update && apk add openssh-client )"
    - eval $(ssh-agent -s)
    - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
    - mkdir -p ~/.ssh
    - echo "$SSH_SERVER_HOSTKEYS" > ~/.ssh/known_hosts
    - chmod 700 ~/.ssh
    # setup LFS authentication
    - git lfs install
    - git config --global credential.helper store
    - echo https://$ARTIFACT_CI_USER:$ARTIFACT_CI_TOKEN@artifact.tecnalia.com > ~/.git-credentials
    # install ddeploy
    - pip install git+ssh://git@git.code.tecnalia.com/tecnalia_robotics/flexbotics/flexbotics_utils/ddeploy.git@v0.1.0
    - docker login ${ARTIFACT_DOCKER_URL} -u ${ARTIFACT_CI_USER} -p ${ARTIFACT_CI_TOKEN}
  script:
    # Run ddeploy
    - ddeploy --yaml ${DDEPLOY_YAML}
    # Get ID of image created by ddeploy
    - 'DOCKER_ID=$(docker images --format="{{.ID}}" | head -1)'
    - bash -xv <(curl -Ls https://git.code.tecnalia.com/tecnalia_robotics-public/gitlab_templates/-/raw/43-force-ddeploy-options-e-g-image-retention/scripts/enforce_labels.bash) "${DOCKER_ID}"
    # Tag and push with the branch or tag name.
    - echo "Pushing to ${DOCKER_PUSH_REGISTRY}/${DOCKER_PUSH_NAME}:${DOCKER_PUSH_TAG}"
    - docker tag ${DOCKER_ID} ${DOCKER_PUSH_REGISTRY}/${DOCKER_PUSH_NAME}:${DOCKER_PUSH_TAG}
    - docker push ${DOCKER_PUSH_REGISTRY}/${DOCKER_PUSH_NAME}:${DOCKER_PUSH_TAG}
    # Tag and push with commit sha
    - echo "Pushing to ${DOCKER_PUSH_REGISTRY}/${DOCKER_PUSH_NAME}:${CI_COMMIT_SHA}"
    - docker tag ${DOCKER_ID} ${DOCKER_PUSH_REGISTRY}/${DOCKER_PUSH_NAME}:${CI_COMMIT_SHA}
    - docker push ${DOCKER_PUSH_REGISTRY}/${DOCKER_PUSH_NAME}:${CI_COMMIT_SHA}
    # Tag and push with latest only if it's on the default branch
    - >
      if [ "$CI_COMMIT_BRANCH" == "$CI_DEFAULT_BRANCH" ]; then
        echo "Pushing to ${DOCKER_PUSH_REGISTRY}/${DOCKER_PUSH_NAME}:latest"
        docker tag ${DOCKER_ID} ${DOCKER_PUSH_REGISTRY}/${DOCKER_PUSH_NAME}:latest &&
        docker push ${DOCKER_PUSH_REGISTRY}/${DOCKER_PUSH_NAME}:latest;
      fi
  rules:
    - if: $CI_COMMIT_TAG
      when: always
    - when: manual
      allow_failure: true
