variables:
  CAISAR_VERSION: "0.2.0"
  TAG: "0.2"

default:
  interruptible: true
  tags: [ nix-v2 ]

stages:
  - build
  - test
  - doc
  - make_public

build:
  stage: build
  script:
    - nix --extra-experimental-features "nix-command flakes" develop --command make test-ci

# Optional and manual full rebuild using a clean docker image
full_rebuild_and_test:
  stage: build
  image: ocaml/opam@sha256:013a26ccbaa8344b63274e335e2492c432cec1c3526b9ba888ab151abb2b4c25
  cache:
    key: $CI_COMMIT_REF_SLUG
    paths:
      - _opam
  script:
    - if [ ! -d _opam ]; then echo "no local switch in the CI cache, we setup a new switch"; opam switch create --yes --no-install . ocaml-base-compiler.4.13.1; fi
    - opam switch 4.13
    - eval $(opam env --switch=4.13 --set-switch)
    - opam switch
    - sudo apt-get update
    - sudo apt install -y protobuf-compiler
    - opam repository add remote https://opam.ocaml.org
    - opam depext --yes ocplib-endian base fmt alt-ergo.2.4.0
    - opam install --jobs 2 . --deps-only --with-test --yes
    - make all-ci
    - make test-ci
  tags:
    - docker
  when: manual

test:
  stage: test
  script:
    - nix --extra-experimental-features "nix-command flakes" build
    - nix --extra-experimental-features "nix-command flakes" flake check

## Manual generation of the documentation

documentation:
  stage: doc
  script:
    - nix --extra-experimental-features "nix-command flakes" build
  artifacts:
   paths:
     - result/share/doc/ocaml4.14.1-caisar-${CAISAR_VERSION}/html
     - result/share/doc/ocaml4.14.1-caisar-${CAISAR_VERSION}/latex
  when: manual

################################################################################
### PUBLIC

# make_public stage is used to push the current master branch of CAISAR to
# public repositories.

# make_public targets the public repository git.frama-c.com/pub/caisar.
# For that, it uses the 'caisar to caisar-public' deploy key of the public
# repository. The latter is the public part of the key, whereas the private one
# is stored in the environment variable $CAISAR_PUBLIC_SSH_PRIVATE_KEY of the
# private repository.

make_public:
  stage: make_public
  image: ocaml/opam@sha256:013a26ccbaa8344b63274e335e2492c432cec1c3526b9ba888ab151abb2b4c25
  script:
    - echo "$CAISAR_PUBLIC_SSH_PRIVATE_KEY" | base64 -d > ci/caisar-public/id_ed25519
    - chmod 400 ci/caisar-public/id_ed25519
    - GIT_SSH=$PWD/ci/caisar-public/ssh.sh git push --atomic git@git.frama-c.com:pub/caisar.git origin/master:refs/heads/master $TAG
  tags:
    - docker
  rules:
    - if: $CI_PIPELINE_SOURCE == "schedule"

# make_public_confianceai targets the public repository
# git.irt-systemx.fr:confianceai/ec_1/fa09_caisar.
# For that, it uses the 'caisar to caisar-confianceai' deploy key of the public
# repository. The latter is the public part of the key, whereas the private one
# is stored in the environment variable $CAISAR_PUBLIC_SSH_PRIVATE_KEY of the
# private repository.

make_public_confianceai:
  stage: make_public
  image: ocaml/opam@sha256:013a26ccbaa8344b63274e335e2492c432cec1c3526b9ba888ab151abb2b4c25
  script:
    - echo "$CAISAR_PUBLIC_SSH_PRIVATE_KEY" | base64 -d > ci/caisar-public/id_ed25519
    - chmod 400 ci/caisar-public/id_ed25519
    - GIT_SSH=$PWD/ci/caisar-public/ssh.sh git push --atomic git@git.irt-systemx.fr:confianceai/ec_1/fa09_caisar.git origin/master:refs/heads/master $TAG
  tags:
    - docker
  rules:
    - if: $CI_PIPELINE_SOURCE == "schedule"
