variables:
  DEBIAN_FRONTEND: noninteractive
  LC_ALL: C
  GIT_STRATEGY: fetch
  DOCKER_DRIVER: overlay2
  GIT_SUBMODULE_STRATEGY: recursive

stages:
  - image
  - build
  - test
  - documentation
  - deploy

.image: &image
  stage: image
  before_script:
    - docker info
  script:
    - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY
    - docker pull "$IMAGE_TAG" || true
    - docker build --cache-from "$IMAGE_TAG" -t "$IMAGE_TAG" "scripts/docker/$IMAGE_NAME"
    - docker push "$IMAGE_TAG"
  tags:
    - dind
  only:
    - triggers
    - schedules

docker:knot-dns:debian:
  <<: *image
  variables:
    IMAGE_NAME: debian:latest
    IMAGE_TAG: $CI_REGISTRY_IMAGE/$IMAGE_NAME

docker:knot-dns:debian:unstable:
  <<: *image
  variables:
    IMAGE_NAME: debian:unstable
    IMAGE_TAG: $CI_REGISTRY_IMAGE/$IMAGE_NAME

docker:knot-dns:ubuntu:
  <<: *image
  variables:
    IMAGE_NAME: ubuntu:latest
    IMAGE_TAG: $CI_REGISTRY_IMAGE/$IMAGE_NAME

docker:knot-dns:centos:
  <<: *image
  variables:
    IMAGE_NAME: centos:latest
    IMAGE_TAG: $CI_REGISTRY_IMAGE/$IMAGE_NAME

docker:knot-dns:fedora:
  <<: *image
  variables:
    IMAGE_NAME: fedora:latest
    IMAGE_TAG: $CI_REGISTRY_IMAGE/$IMAGE_NAME

.freebsd_i386: &freebsd_i386
  tags:
    - freebsd
    - i386
  only:
    - master
    - triggers
    - tags

.freebsd_amd64: &freebsd_amd64
  tags:
    - freebsd
    - amd64
  only:
    - master
    - triggers
    - tags

.fedora_latest: &fedora_latest
  image: "$CI_REGISTRY/knot/knot-dns/fedora:latest"
  tags:
    - docker
    - linux
    - amd64
  except:
    - schedules


.centos_latest: &centos_latest
  image: "$CI_REGISTRY/knot/knot-dns/centos:latest"
  tags:
    - docker
    - linux
    - amd64
  except:
    - schedules

.debian_stable: &debian_stable
  image: "$CI_REGISTRY/knot/knot-dns/debian:latest"
  tags:
    - docker
    - linux
    - amd64
  except:
    - schedules

.debian_unstable: &debian_unstable
  image: "registry.labs.nic.cz/knot/knot-dns/debian:unstable"
  tags:
    - docker
    - linux
    - amd64
  except:
    - schedules

.ubuntu_latest: &ubuntu_latest
  image: "$CI_REGISTRY/knot/knot-dns/ubuntu:latest"
  tags:
    - docker
    - linux
    - amd64
  except:
    - schedules

.build: &build_job
  stage: build
  script:
    - autoreconf -fi
    - ./configure --disable-fastparser || ( cat config.log && exit 1 )
    - make -k all V=1
  artifacts:
    untracked: true
    expire_in: '1 hour'
  except:
    - schedules

.test: &test_job
  stage: test
  script:
    - make -k check V=1
  except:
    - schedules

build:fedora:amd64:
  <<: *fedora_latest
  <<: *build_job

test:fedora:amd64:
  <<: *fedora_latest
  <<: *test_job
  dependencies:
    - build:fedora:amd64

build:centos:amd64:
  <<: *centos_latest
  <<: *build_job

test:centos:amd64:
  <<: *centos_latest
  <<: *test_job
  dependencies:
    - build:centos:amd64

build:ubuntu:amd64:
  <<: *ubuntu_latest
  <<: *build_job

test:ubuntu:amd64:
  <<: *ubuntu_latest
  <<: *test_job
  dependencies:
    - build:ubuntu:amd64

build:debian:amd64:
  <<: *debian_stable
  <<: *build_job

test:debian:amd64:
  <<: *debian_stable
  <<: *test_job
  dependencies:
    - build:debian:amd64

build:debian:unstable:amd64:
  <<: *debian_unstable
  <<: *build_job

test:debian:unstable:amd64:
  <<: *debian_unstable
  <<: *test_job
  dependencies:
    - build:debian:unstable:amd64

build:debian:unstable:amd64:asan:
  variables:
    CC: clang-6.0
    CFLAGS: "-fsanitize=address -g -O2 -fno-omit-frame-pointer"
    LDFLAGS: "-fsanitize=address"
    ASAN_SYMBOLIZER_PATH: /usr/lib/llvm-6.0/bin/llvm-symbolizer
    LSAN_OPTIONS: verbosity=1:log_threads=1
  allow_failure: true
  <<: *debian_unstable
  <<: *build_job

test:debian:unstable:amd64:asan:
  variables:
    CC: clang-6.0
    CFLAGS: "-fsanitize=address -g -O2"
    LDFLAGS: "-fsanitize=address"
    ASAN_SYMBOLIZER_PATH: /usr/lib/llvm-6.0/bin/llvm-symbolizer
    LSAN_OPTIONS: verbosity=1:log_threads=1
  allow_failure: true
  <<: *debian_unstable
  <<: *test_job
  dependencies:
    - build:debian:unstable:amd64:asan

build:debian:unstable:amd64:ubsan:
  variables:
    CC: clang-6.0
    CFLAGS: "-fsanitize=undefined -fno-sanitize=nonnull-attribute -g -O2"
    LDFLAGS: "-fsanitize=undefined"
    UBSAN_SYMBOLIZER_PATH: /usr/lib/llvm-6.0/bin/llvm-symbolizer
    UBSAN_OPTIONS: print_stacktrace=1
  allow_failure: true
  <<: *debian_unstable
  <<: *build_job

test:debian:unstable:amd64:ubsan:
  variables:
    CC: clang-6.0
    CFLAGS: "-fsanitize=undefined -fno-sanitize=nonnull-attribute -g -O2"
    LDFLAGS: "-fsanitize=undefined"
    UBSAN_SYMBOLIZER_PATH: /usr/lib/llvm-6.0/bin/llvm-symbolizer
    UBSAN_OPTIONS: print_stacktrace=1
  allow_failure: true
  <<: *debian_unstable
  <<: *test_job
  dependencies:
    - build:debian:unstable:amd64:ubsan

build:freebsd:i386:
  <<: *freebsd_i386
  <<: *build_job

test:freebsd:i386:
  <<: *freebsd_i386
  <<: *test_job
  dependencies:
    - build:freebsd:i386

build:freebsd:amd64:
  <<: *freebsd_amd64
  <<: *build_job

test:freebsd:amd64:
  <<: *freebsd_amd64
  <<: *test_job
  dependencies:
    - build:freebsd:amd64

build:archive:
  <<: *debian_stable
  stage: build
  script:
    - autoreconf -fi
    - mkdir _build
    - cd _build
    - ../configure
    - make distcheck V=1
  only:
    - master
    - tags
    - triggers
  artifacts:
    paths:
      - _build/*.tar.xz

obs:devel:
  <<: *debian_stable
  stage: deploy
  only:
    - schedules
  dependencies:
    - build:archive
  script:
    - mv _build/*.tar.xz ./
    - scripts/make-distrofiles.sh
    - echo -e "[general]\napiurl = https://api.opensuse.org\n\n[https://api.opensuse.org]\nuser = CZ-NIC-automation\npass = $OBS_PASSWORD" > /root/.oscrc
    - scripts/build-in-obs.sh knot-dns-devel

build:documentation:
  <<: *debian_stable
  stage: documentation
  only:
    - tags
    - triggers
  dependencies:
    - build:debian:amd64
  script:
    - make -C doc html singlehtml pdf V=1
  artifacts:
    paths:
      - doc/_build/html/
      - doc/_build/singlehtml/
      - doc/_build/latex/knot.pdf
    expire_in: '1 hour'

deploy:documentation:
  <<: *debian_stable
  stage: deploy
  dependencies:
    - build:documentation
  only:
    - tags
    - triggers
  script:
    - "curl --http1.1 --request POST --form token=$WEBSITE_TOKEN --form ref=master
      --form \"variables[RELEASE_CI_BUILD_REF_NAME]=$CI_COMMIT_REF_NAME\"
      --form \"variables[RELEASE_CI_BUILD_ID]=$CI_JOB_ID\"
      https://gitlab.labs.nic.cz/api/v3/projects/5/trigger/builds"
  artifacts:
    name: "knot-dns-$CI_COMMIT_REF_NAME-doc"
    paths:
      - doc/_build/html/
      - doc/_build/singlehtml/
      - doc/_build/latex/knot.pdf
