# In order to make "docker build" use cache as most as possible, there
# are three "prepare" stages.
# - prepare distribution images: build one container by distribution (for now,
#   debian and opensuse/leap, both with OCaml 4.09)
# - prepare environment images: build debian containers for 3.12,
#   4.09 with parmap, 4.09 with menhir, 4.09 with pyml. It is worth noticing
#   that all these buildings implicitly use the cached container built on the
#   previous stage.
# - prepare image with all dependencies: build debian container with parmap,
#   menhir and pyml. It is worth noticing that this building use implicitly
#   the cached container with pyml built on the previous stage.

# We begin by trace-cmd because the triggered pipeline will be run in parallel.

stages:
  - trace-cmd
  - prepare distribution images
  - prepare environment images
  - prepare image with all dependencies
  - build
  - clone reference scores
  - test

# Dockerfiles are split in two:
# - a part specific to the distribution that installs package and adds
#   a user called "ci"
# - a part common to both distributions that makes "ci" sudoer and installs
#   opam
# "docker build" is run twice, once to make the specific part and once to
# make the common part starting from the container prepared by the first part.

.prepare docker image: &prepare_docker_image_def
  script:
    - docker build --tag $image_name-prepare ci/distributions/$distribution
    - docker build --tag $image_name ci/distributions/common
        --build-arg from=$image_name-prepare
        --build-arg ocaml_version=$ocaml_version
        --build-arg stdcompat=$stdcompat --build-arg pyml=$pyml
        --build-arg menhir=$menhir --build-arg parmap=$parmap

prepare debian docker image with 4.09.0:
  <<: *prepare_docker_image_def
  stage: prepare distribution images
  variables:
    image_name: debian-4.09.0
    distribution: debian
    ocaml_version: 4.09.0

prepare opensuse/leap docker image with 4.09.0:
  <<: *prepare_docker_image_def
  stage: prepare distribution images
  variables:
    image_name: opensuse-leap-4.09.0
    distribution: opensuse-leap
    ocaml_version: 4.09.0

prepare opensuse/leap docker image with 4.14.1:
  <<: *prepare_docker_image_def
  stage: prepare distribution images
  variables:
    image_name: opensuse-leap-4.14.1
    distribution: opensuse-leap
    ocaml_version: 4.14.1

prepare opensuse/leap docker image with 5.0.0:
  <<: *prepare_docker_image_def
  stage: prepare distribution images
  variables:
    image_name: opensuse-leap-5.0.0
    distribution: opensuse-leap
    ocaml_version: 5.0.0

prepare debian docker image with 3.12.1:
  <<: *prepare_docker_image_def
  stage: prepare environment images
  variables:
    image_name: debian-3.12.1
    distribution: debian
    ocaml_version: 3.12.1

prepare debian docker image with 4.14.1:
  <<: *prepare_docker_image_def
  stage: prepare distribution images
  variables:
    image_name: debian-4.14.1
    distribution: debian
    ocaml_version: 4.14.1

prepare debian docker image with 5.0.0:
  <<: *prepare_docker_image_def
  stage: prepare environment images
  variables:
    image_name: debian-5.0.0
    distribution: debian
    ocaml_version: 5.0.0

# Note: "yes" should be quoted in variable values (yes is parsed as a keyword)

prepare debian docker image with 4.09.0 and menhir:
  <<: *prepare_docker_image_def
  stage: prepare environment images
  variables:
    image_name: debian-4.09.0-menhir
    distribution: debian
    ocaml_version: 4.09.0
    menhir: "yes"

prepare debian docker image with 4.09.0 and parmap:
  <<: *prepare_docker_image_def
  stage: prepare environment images
  variables:
    image_name: debian-4.09.0-parmap
    distribution: debian
    ocaml_version: 4.09.0
    parmap: "yes"

prepare debian docker image with 4.09.0 and pyml:
  <<: *prepare_docker_image_def
  stage: prepare environment images
  variables:
    image_name: debian-4.09.0-pyml
    distribution: debian
    ocaml_version: 4.09.0
    pyml: "yes"

prepare debian docker image with 4.09.0 and all dependencies:
  <<: *prepare_docker_image_def
  stage: prepare image with all dependencies
  variables:
    image_name: debian-4.09.0-all-dependencies
    distribution: debian
    ocaml_version: 4.09.0
    pyml: "yes"
    parmap: "yes"
    menhir: "yes"

.build coccinelle: &build_coccinelle_def
  stage: build
  script:
    - docker build --tag coccinelle-with-$from . -f ci/Dockerfile.build
        --build-arg from=$from --build-arg configure_options=$configure_options

build with debian 3.12:
  <<: *build_coccinelle_def
  variables:
    from: debian-3.12.1

build with debian 4.09:
  <<: *build_coccinelle_def
  variables:
    from: debian-4.09.0

build with debian 4.14:
  <<: *build_coccinelle_def
  variables:
    from: debian-4.14.1

build with debian 5.0:
  <<: *build_coccinelle_def
  variables:
    from: debian-5.0.0

build with debian 4.09 with menhir:
  <<: *build_coccinelle_def
  variables:
    from: debian-4.09.0-menhir

build with debian 4.09 with parmap:
  <<: *build_coccinelle_def
  variables:
    from: debian-4.09.0-parmap

build with debian 4.09 with pyml:
  <<: *build_coccinelle_def
  variables:
    from: debian-4.09.0-pyml

build with debian 4.09 with all dependencies:
  <<: *build_coccinelle_def
  variables:
    from: debian-4.09.0-all-dependencies

build with opensuse/leap 4.09:
  <<: *build_coccinelle_def
  variables:
    from: opensuse-leap-4.09.0

build with opensuse/leap 4.14:
  <<: *build_coccinelle_def
  variables:
    from: opensuse-leap-4.14.1

build with opensuse/leap 5.0:
  <<: *build_coccinelle_def
  variables:
    from: opensuse-leap-5.0.0

clone reference scores:
  stage: clone reference scores
  script:
    - git clone git@gitlab.inria.fr:coccinelle/reference_scores.git
  artifacts:
    paths:
      - reference_scores/

# Note: use the shell command "test" instead of "[" since yaml wants to parse
# lines starting with square brackets as lists

.test coccinelle: &test_coccinelle_def
  stage: test
  script:
    - docker rm coccinelle-with-$from || true
    - docker create --name coccinelle-with-$from coccinelle-with-$from
        /home/ci/coccinelle/ci/test-script.sh
    - test ! -f reference_scores/$from || docker cp
        reference_scores/$from
        coccinelle-with-$from:/home/ci/coccinelle/tests/SCORE_expected.sexp
    - docker start --interactive coccinelle-with-$from
    - docker cp
        coccinelle-with-$from:/home/ci/coccinelle/tests/SCORE_expected.sexp
        reference_scores/$from
    - docker rm coccinelle-with-$from
  artifacts:
    paths:
      - reference_scores/$from

test with debian 3.12:
  <<: *test_coccinelle_def
  variables:
    from: debian-3.12.1

test with debian 4.09:
  <<: *test_coccinelle_def
  variables:
    from: debian-4.09.0

test with debian 4.14:
  <<: *test_coccinelle_def
  variables:
    from: debian-4.14.1

test with debian 5.0:
  <<: *test_coccinelle_def
  variables:
    from: debian-5.0.0

test with debian 4.09 with menhir:
  <<: *test_coccinelle_def
  variables:
    from: debian-4.09.0-menhir

test with debian 4.09 with parmap:
  <<: *test_coccinelle_def
  variables:
    from: debian-4.09.0-parmap

test with debian 4.09 with pyml:
  <<: *test_coccinelle_def
  variables:
    from: debian-4.09.0-pyml

test with debian 4.09 with all dependencies:
  <<: *test_coccinelle_def
  variables:
    from: debian-4.09.0-all-dependencies

test with debian 4.09 without python:
  <<: *test_coccinelle_def
  variables:
    from: debian-4.09.0
    configure_options: --disable-python

test with opensuse/leap 4.09:
  <<: *test_coccinelle_def
  variables:
    from: opensuse-leap-4.09.0

test with opensuse/leap 4.14:
  <<: *test_coccinelle_def
  variables:
    from: opensuse-leap-4.14.1

test with opensuse/leap 5.0:
  <<: *test_coccinelle_def
  variables:
    from: opensuse-leap-5.0.0

push reference scores:
  stage: .post
  only:
    - master@coccinelle/coccinelle
  script:
    - cd reference_scores &&
        git add * &&
        git commit -m "reference scores for commit $CI_COMMIT_SHORT_SHA" &&
        git push
      || true

#trace-cmd-check-changed:
#  stage: trace-cmd
#  image: "registry.gitlab.inria.fr/inria-ci/docker/python3-cloudstack"
#  tags:
#    - linux
#    - small
#  rules:
#    - if: $CLOUDSTACK_API_KEY
#  script:
#    - cd ci/trace-cmd
#    - python3 check-template-changed.py > ../../trace-cmd-config.yml
#  artifacts:
#    paths: [trace-cmd-config.yml]
#
#trace-cmd:
#  stage: trace-cmd
#  needs: [trace-cmd-check-changed]
#  rules:
#    - if: $CLOUDSTACK_API_KEY
#  trigger:
#    include:
#      - artifact: trace-cmd-config.yml
#        job: trace-cmd-check-changed
