variables:
  public_docker_image: docker.io/tezos/tezos

stages:
  - prepare
  - build
  - test
  - 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 info
    - docker login -u gitlab-ci-token -p "$CI_BUILD_TOKEN" "${CI_REGISTRY}"
  tags:
    - docker

## Prepare docker image with precompiled OCaml/opam

prepare:
  <<: *dind_definition
  stage: prepare
  only:
    - schedules
  before_script:
    - . ./scripts/version.sh
  script:
    - ./scripts/create_docker_image.alpine.sh
    - mkdir ~/.docker || true
    - echo "$CI_DOCKER_AUTH" > ~/.docker/config.json ;
    - docker push "tezos/opam:alpine-${alpine_version}_ocaml-${ocaml_version}"
    - docker tag "tezos/opam:alpine-${alpine_version}_ocaml-${ocaml_version}"
                 "tezos/opam:latest"
    - docker push "tezos/opam:latest"

## Tezos

build:
  <<: *dind_definition
  stage: build
  script:
    ## Building dependencies...
    - ./scripts/create_docker_image.build_deps.sh
        "build_deps" "${CI_BUILD_REF}"
        "${CI_REGISTRY_IMAGE}/build_deps"
    ## Preparing sources for the alphanet or the zeronet...
    - if [ "${CI_PROJECT_PATH}" = "tezos/tezos" ] ; then
        ./scripts/apply_patch.sh "${CI_BUILD_REF_NAME}" ;
      fi
    ## Building...
    - ./scripts/create_docker_image.build.sh
        "${CI_REGISTRY_IMAGE}/build" "${CI_BUILD_REF}"
        "build_deps:${CI_BUILD_REF}"
    ## Saving the docker image...
    - docker push "${CI_REGISTRY_IMAGE}/build:${CI_BUILD_REF}"

## Basic tests

.test_template: &test_definition
  stage: test
  image: ${CI_REGISTRY_IMAGE}/build:${CI_BUILD_REF}
  before_script:
    - cd /home/opam/tezos
  dependencies:
    - build

test:utils:data-encoding:
  <<: *test_definition
  script:
    - make -C test/utils run-test-data-encoding

test:utils:merkle:
  <<: *test_definition
  script:
    - make -C test/utils run-test-merkle

test:shell:store:
  <<: *test_definition
  script:
    - make -C test/shell run-test-store

test:shell:context:
  <<: *test_definition
  script:
    - make -C test/shell run-test-context

test:shell:state:
  <<: *test_definition
  script:
    - make -C test/shell run-test-state

test:p2p:io-scheduler:
  <<: *test_definition
  script:
    - make -C test/p2p run-test-p2p-io-scheduler

test:p2p:connection:
  <<: *test_definition
  script:
    - make -C test/p2p run-test-p2p-connection

test:p2p:connection-pool:
  <<: *test_definition
  script:
    - make -C test/p2p run-test-p2p-connection-pool

test:proto_alpha:transactions:
  <<: *test_definition
  script:
    - make -C test/proto_alpha run-test-transaction
  retry: 1

test:proto_alpha:origination:
  <<: *test_definition
  script:
    - make -C test/proto_alpha run-test-origination
  retry: 1

test:proto_alpha:endorsement:
  <<: *test_definition
  script:
    - make -C test/proto_alpha run-test-endorsement
  retry: 1

test:proto_alpha:vote:
  <<: *test_definition
  script:
    - make -C test/proto_alpha run-test-vote
  retry: 1

test:basic.sh:
  <<: *test_definition
  script:
    - make -C test run-basic.sh
  retry: 1

test:contracts.sh:
  <<: *test_definition
  script:
    - make -C test run-contracts.sh
  retry: 1

test:multinode.sh:
  <<: *test_definition
  script:
    - make -C test run-multinode.sh
  retry: 1

## 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:
    - docker pull "${CI_REGISTRY_IMAGE}/build:${CI_BUILD_REF}"
    - if [ "${CI_PROJECT_PATH}" = "tezos/tezos" ] ; then
        ./scripts/apply_patch.sh "${CI_BUILD_REF_NAME}" ;
      fi
    - ./scripts/create_docker_image.minimal.sh
        "${public_docker_image}" "${CI_BUILD_REF_NAME}"
        "${CI_REGISTRY_IMAGE}/build:${CI_BUILD_REF}"
    - mkdir ~/.docker || true
    - echo "$CI_DOCKER_AUTH" > ~/.docker/config.json ;
    - docker push "${public_docker_image}:${CI_BUILD_REF_NAME}"

## Publish to github...

publish:github:
  image: tezos/opam
  stage: publish
  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_BUILD_REF_NAME}" ;
      fi
    - git filter-branch --prune-empty --env-filter
            'if [ $GIT_COMMIT != "f42e9d12ac75d2876a4cf600d44976b7090d4533" ] ; then
               export GIT_COMMITTER_NAME="$GIT_AUTHOR_NAME" ;
               export GIT_COMMITTER_EMAIL="$GIT_AUTHOR_EMAIL" ;
               export GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE" ;
               export GIT_AUTHOR_NAME="Dynamic Ledger Solutions, Inc." ;
               export GIT_AUTHOR_EMAIL="contact@tezos.com" ;
             fi'
         HEAD
    - git push git@github.com:tezos/tezos.git -f HEAD:${CI_BUILD_REF_NAME}


## Relaunching the bootstrap servers of the alphanet.zeronet

.bootstrap_template: &bootstrap_definition
  image: tezos/opam
  stage: deploy
  only:
    - alphanet@tezos/tezos
    - zeronet@tezos/tezos
  before_script:
    - echo "${CI_KH}" > ~/.ssh/known_hosts
    - if [ "${CI_BUILD_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

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...

# cleanup:
  # <<: *dind_definition
  # stage: cleanup
  # script:
    # - apk add -U curl jq
    # - ./scripts/delete_ci_image.sh gitlab-ci-token "${CI_BUILD_TOKEN}"
         # "${CI_PROJECT_PATH}/build"
         # "${CI_BUILD_REF}"