stages:
  - prebuild
  - build

workflow:
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
    - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
    - if: $CI_PIPELINE_SOURCE == "web"

variables:
  GIT_DEPTH: 0
  GIT_SUBMODULE_STRATEGY: recursive
  BASE_IMAGE: build-base:latest
  CARGO_INCREMENTAL: 0
  CARGO_HOME: ${CI_PROJECT_DIR}/.cargo
  DOCKER_AUTH_CONFIG: $DOCKER_AUTH_RW
  REGISTRY_IMAGE: docker-public.binary.picodata.io

  #
  # See also:
  #   GitLab CI/CD predefined variables
  #   https://docs.gitlab.com/ee/ci/variables/predefined_variables.html
  #
  # CI_COMMIT_SHA: The commit revision the project is built for
  # CI_REGISTRY: The address of the GitLab Container Registry
  # CI_REGISTRY_USER: The username to push containers to the project’s
  #   GitLab Container Registry
  # CI_REGISTRY_PASSWORD: The password to push containers to the project’s
  #   GitLab Container Registry
  # CI_REGISTRY_IMAGE: The address of the project’s Container Registry

docker-build-base:
  stage: prebuild
  image: docker:20
  tags:
    - dind
  rules:
    - # if
      changes:
        - docker-build-base/Dockerfile
  before_script:
    - mkdir -p $HOME/.docker
    - echo $DOCKER_AUTH_RW > $HOME/.docker/config.json
  script:
    - docker pull ${REGISTRY_IMAGE}/${BASE_IMAGE} || true
    - >
      docker build
      --pull
      --label GIT_COMMIT=${CI_COMMIT_SHA}
      --cache-from ${REGISTRY_IMAGE}/${BASE_IMAGE}
      -t ${REGISTRY_IMAGE}/${BASE_IMAGE}
      -f ./docker-build-base/Dockerfile
      ./docker-build-base
    - docker push ${REGISTRY_IMAGE}/${BASE_IMAGE}

build:
  image: ${REGISTRY_IMAGE}/${BASE_IMAGE}
  stage: build
  tags:
    - docker
  script:
    # - git submodule foreach --recursive 'git fetch --tags; git describe --always'
    - pushd tarantool-sys; git describe; popd
    - pushd tarantool-sys/third_party/luajit; git describe; popd
    - cargo -V
    - cargo build --locked
    - cargo test --locked
    - cargo fmt -- -v --check
    - cargo clippy --version
    - cargo clippy -- --deny clippy::all
    - PIPENV_VENV_IN_PROJECT=1 PIP_NO_CACHE_DIR=true python3.10 -m pipenv install --deploy
    - pipenv run pytest --numprocesses 2 -v
    - pipenv run lint
  cache:
    paths:
      - target
      - .cargo
      - .venv
