
variables:
  opam_image: ${CI_REGISTRY_IMAGE}/opam
  build_deps_image: ${CI_REGISTRY_IMAGE}/build_deps
  public_docker_image: docker.io/tezos/tezos

stages:
  - prepare
  - build_deps
  - build
  - test
  - opam
  - publish
  - deploy
  - cleanup

## Template for `docker_in_docker` jobs

.dind_template: &dind_definition
  image: docker:latest
  variables:
    DOCKER_DRIVER: overlay2
  services:
    - docker:dind
  before_script:
    - docker login -u "${CI_REGISTRY_USER}" -p "${CI_REGISTRY_PASSWORD}" "${CI_REGISTRY}"
    - apk add --no-cache curl jq
    - . ./scripts/version.sh
  tags:
    - safe_docker

prepare:opam:
  <<: *dind_definition
  stage: prepare
  only:
    - schedules
  script:
    - ./scripts/ci/create_docker_image.opam.sh "${opam_image}"
    - docker push "${opam_image}:alpine-${alpine_version}_ocaml-${ocaml_version}"
    - docker tag "${opam_image}:alpine-${alpine_version}_ocaml-${ocaml_version}"
                 "${opam_image}:latest"
    - docker push "${opam_image}:latest"

## Tezos build dependencies

build_deps:
  <<: *dind_definition
  stage: build_deps
  script:
    ## Building dependencies...
    - ./scripts/ci/create_docker_image.build_deps.sh
        "${build_deps_image}" "${CI_COMMIT_SHA}"
        "${opam_image}:alpine-${alpine_version}_ocaml-${ocaml_version}"
        "yes"

.build_deps_template: &build_deps_definition
  image: ${build_deps_image}:${CI_COMMIT_SHA}
  before_script:
    - if [ "${CI_PROJECT_PATH}" = "tezos/tezos" ] ; then
        ./scripts/apply_patch.sh "${CI_COMMIT_REF_NAME}" ;
      fi
    - opam list
  tags:
    - gitlab-org

## Tezos

build:
  <<: *build_deps_definition
  stage: build
  except:
    - master@tezos/tezos
  script:
    - make all
    - make build-test
  artifacts:
    paths:
    - _build
    expire_in: 1 day

## Basic tests (run on merge requests)

.test_template: &test_definition
  <<: *build_deps_definition
  stage: test
  except:
    - master@tezos/tezos
  dependencies:
    - build

test:opam_packaging:
  <<: *test_definition
  script:
    - ./scripts/check_opam_test.sh "$CI_PROJECT_DIR/$CI_CONFIG_PATH"

test:ocp-indent:
  <<: *test_definition
  script:
    - jbuilder build @runtest_indent

test:stdlib:
  <<: *test_definition
  script:
    - jbuilder build @src/lib_stdlib/runtest

test:stdlib_lwt:
  <<: *test_definition
  script:
    - jbuilder build @src/lib_stdlib_lwt/runtest

test:data_encoding:
  <<: *test_definition
  script:
    - jbuilder build @src/lib_data_encoding/runtest

test:storage:
  <<: *test_definition
  script:
    - jbuilder build @src/lib_storage/runtest

test:crypto:
  <<: *test_definition
  script:
    - jbuilder build @src/lib_crypto/runtest

test:shell:
  <<: *test_definition
  script:
    - jbuilder build @src/lib_shell/runtest

test:p2p:io-scheduler:
  <<: *test_definition
  script:
    - jbuilder build @src/lib_p2p/runtest_p2p_io_scheduler

test:p2p:socket:
  <<: *test_definition
  script:
    - jbuilder build @src/lib_p2p/runtest_p2p_socket

test:p2p:pool:
  <<: *test_definition
  script:
    - jbuilder build @src/lib_p2p/runtest_p2p_pool

test:proto_alpha:
  <<: *test_definition
  script:
    - jbuilder build @src/proto_alpha/lib_protocol/runtest

test:client_alpha:transaction:
  <<: *test_definition
  script:
    - jbuilder build @src/proto_alpha/lib_client/runtest_transaction

test:client_alpha:origination:
  <<: *test_definition
  script:
    - jbuilder build @src/proto_alpha/lib_client/runtest_origination

test:client_alpha:endorsement:
  <<: *test_definition
  script:
    - jbuilder build @src/proto_alpha/lib_client/runtest_endorsement

test:client_alpha:vote:
  <<: *test_definition
  script:
    - jbuilder build @src/proto_alpha/lib_client/runtest_vote

test:basic.sh:
  <<: *test_definition
  script:
    - jbuilder build @src/bin_client/runtest_basic.sh

test:contracts.sh:
  <<: *test_definition
  script:
    - jbuilder build @src/bin_client/runtest_contracts.sh

test:multinode.sh:
  <<: *test_definition
  script:
    - jbuilder build @src/bin_client/runtest_multinode.sh

test:proto:sandbox:
  <<: *test_definition
  script:
    - jbuilder build @runtest_sandbox

## Basic tests through opam packaged (run on master only)

.opam_template: &opam_definition
  image: ${opam_image}
  stage: opam
  dependencies: []
  only:
    - master@tezos/tezos
    - /^.*opam.*$/
  script:
    - ./scripts/opam-pin.sh
    - opam depext --yes ${package}
    - opam install --yes ${package}
    - opam reinstall --yes --with-test ${package}
  tags:
    - gitlab-org

##BEGIN_OPAM##
opam:00:tezos-stdlib:
  <<: *opam_definition
  variables:
    package: tezos-stdlib

opam:01:ocplib-resto:
  <<: *opam_definition
  variables:
    package: ocplib-resto

opam:02:tezos-data-encoding:
  <<: *opam_definition
  variables:
    package: tezos-data-encoding

opam:03:ocplib-resto-directory:
  <<: *opam_definition
  variables:
    package: ocplib-resto-directory

opam:04:tezos-error-monad:
  <<: *opam_definition
  variables:
    package: tezos-error-monad

opam:05:blake2:
  <<: *opam_definition
  variables:
    package: blake2

opam:06:tezos-rpc:
  <<: *opam_definition
  variables:
    package: tezos-rpc

opam:07:tezos-stdlib-lwt:
  <<: *opam_definition
  variables:
    package: tezos-stdlib-lwt

opam:08:tweetnacl:
  <<: *opam_definition
  variables:
    package: tweetnacl

opam:09:tezos-crypto:
  <<: *opam_definition
  variables:
    package: tezos-crypto

opam:10:tezos-micheline:
  <<: *opam_definition
  variables:
    package: tezos-micheline

opam:11:ocplib-resto-cohttp:
  <<: *opam_definition
  variables:
    package: ocplib-resto-cohttp

opam:12:tezos-base:
  <<: *opam_definition
  variables:
    package: tezos-base

opam:13:irmin-leveldb:
  <<: *opam_definition
  variables:
    package: irmin-leveldb

opam:14:tezos-protocol-environment-sigs:
  <<: *opam_definition
  variables:
    package: tezos-protocol-environment-sigs

opam:15:tezos-rpc-http:
  <<: *opam_definition
  variables:
    package: tezos-rpc-http

opam:16:tezos-shell-services:
  <<: *opam_definition
  variables:
    package: tezos-shell-services

opam:17:tezos-storage:
  <<: *opam_definition
  variables:
    package: tezos-storage

opam:18:tezos-protocol-compiler:
  <<: *opam_definition
  variables:
    package: tezos-protocol-compiler

opam:19:tezos-client-base:
  <<: *opam_definition
  variables:
    package: tezos-client-base

opam:20:tezos-protocol-alpha:
  <<: *opam_definition
  variables:
    package: tezos-protocol-alpha

opam:21:tezos-protocol-environment-client:
  <<: *opam_definition
  variables:
    package: tezos-protocol-environment-client

opam:22:tezos-protocol-genesis:
  <<: *opam_definition
  variables:
    package: tezos-protocol-genesis

opam:23:tezos-protocol-updater:
  <<: *opam_definition
  variables:
    package: tezos-protocol-updater

opam:24:tezos-p2p:
  <<: *opam_definition
  variables:
    package: tezos-p2p

opam:25:ocplib-resto-json:
  <<: *opam_definition
  variables:
    package: ocplib-resto-json

opam:26:tezos-client-alpha:
  <<: *opam_definition
  variables:
    package: tezos-client-alpha

opam:27:tezos-client-genesis:
  <<: *opam_definition
  variables:
    package: tezos-client-genesis

opam:28:tezos-embedded-protocol-alpha:
  <<: *opam_definition
  variables:
    package: tezos-embedded-protocol-alpha

opam:29:tezos-embedded-protocol-demo:
  <<: *opam_definition
  variables:
    package: tezos-embedded-protocol-demo

opam:30:tezos-embedded-protocol-genesis:
  <<: *opam_definition
  variables:
    package: tezos-embedded-protocol-genesis

opam:31:tezos-shell:
  <<: *opam_definition
  variables:
    package: tezos-shell

opam:32:ocplib-ezresto:
  <<: *opam_definition
  variables:
    package: ocplib-ezresto

opam:33:tezos-client:
  <<: *opam_definition
  variables:
    package: tezos-client

opam:34:tezos-node:
  <<: *opam_definition
  variables:
    package: tezos-node

opam:35:tezos-test-helpers:
  <<: *opam_definition
  variables:
    package: tezos-test-helpers

opam:36:ocplib-ezresto-directory:
  <<: *opam_definition
  variables:
    package: ocplib-ezresto-directory

opam:37:tezos-protocol-demo:
  <<: *opam_definition
  variables:
    package: tezos-protocol-demo


##END_OPAM##

## Publishing (small) docker images with tezos binaries

publish:docker:minimal:
  <<: *dind_definition
  stage: publish
  only:
    - master@tezos/tezos
    - alphanet@tezos/tezos
    - zeronet@tezos/tezos
  script:
    - if [ "${CI_PROJECT_PATH}" = "tezos/tezos" ] ; then
        ./scripts/apply_patch.sh "${CI_COMMIT_REF_NAME}" ;
      fi
    - ./scripts/ci/create_docker_image.build.sh
        "tezos_build" "${CI_COMMIT_REF_NAME}"
        "${build_deps_image}:${CI_COMMIT_SHA}"
    - ./scripts/ci/create_docker_image.minimal.sh
        "${public_docker_image}" "${CI_COMMIT_REF_NAME}"
        "tezos_build:${CI_COMMIT_REF_NAME}"
    - mkdir ~/.docker || true
    - echo "${CI_DOCKER_AUTH}" > ~/.docker/config.json ;
    - docker push "${public_docker_image}:${CI_COMMIT_REF_NAME}"

## Publish to github...

publish:github:
  image: ${opam_image}
  stage: publish
  dependencies: []
  only:
    - master@tezos/tezos
    - alphanet@tezos/tezos
  script:
    - echo "${CI_KH}" > ~/.ssh/known_hosts
    - echo "${CI_PK_GITHUB}" > ~/.ssh/id_rsa
    - chmod 400 ~/.ssh/id_rsa
    - rm -fr .git/refs/original
    - if [ "${CI_PROJECT_PATH}" = "tezos/tezos" ] ; then
        ./scripts/apply_patch.sh "${CI_COMMIT_REF_NAME}" ;
      fi
    - sudo apk add --no-cache openssh
    - git push git@github.com:tezos/tezos.git -f HEAD:${CI_COMMIT_REF_NAME}
  tags:
    - gitlab-org

## Publish docs to gitlab...

pages:
  image: ${build_deps_image}:${CI_COMMIT_SHA}
  stage: publish
  only:
    - master@tezos/tezos
  artifacts:
    paths:
    - public
  script:
    - sudo apk add --no-cache py3-sphinx py3-sphinx_rtd_theme
    - sudo ln -s /usr/bin/sphinx-build-3 /usr/bin/sphinx-build
    - opam install --yes odoc
    - make doc-html && sudo mv docs/_build "${CI_PROJECT_DIR}"/public
  tags:
    - gitlab-org

## Relaunching the bootstrap servers of the alphanet.zeronet

.bootstrap_template: &bootstrap_definition
  image: ${opam_image}
  stage: deploy
  dependencies: []
  only:
    - alphanet@tezos/tezos
    - zeronet@tezos/tezos
  before_script:
    - echo "${CI_KH}" > ~/.ssh/known_hosts
    - if [ "${CI_COMMIT_REF_NAME}" = "alphanet" ]; then
        echo "${CI_PK_ALPHANET}" | tr -d "\r" > ~/.ssh/id_ed25519 ;
      else
        echo "${CI_PK_ZERONET}" | tr -d "\r" > ~/.ssh/id_ed25519 ;
      fi
    - echo "${CI_SSH_CONFIG}" | tr -d "\r" > ~/.ssh/config
    - chmod 600 ~/.ssh/id_ed25519
  allow_failure: true
  tags:
    - gitlab-org

deploy:bootstrap1:
  <<: *bootstrap_definition
  script:
    - ssh bootstrap1

deploy:bootstrap2:
  <<: *bootstrap_definition
  script:
    - ssh bootstrap2

deploy:bootstrap3:
  <<: *bootstrap_definition
  script:
    - ssh bootstrap3

deploy:bootstrap4:
  <<: *bootstrap_definition
  script:
    - ssh bootstrap4

deploy:bootstrap5:
  <<: *bootstrap_definition
  script:
    - ssh bootstrap5

deploy:bootstrap6:
  <<: *bootstrap_definition
  script:
    - ssh bootstrap6

# cleanup:
  # <<: *dind_definition
  # stage: cleanup
  # dependencies: []
  # script:
    # - ./scripts/ci/docker_registry_delete.sh
         # "${CI_REGISTRY_USER}" "${CI_REGISTRY_PASSWORD}"
         # "${build_deps_image}" "${CI_COMMIT_SHA}"
