variables:
  # Not normally needed, but may be if some script uses `apt-get install`.
  DEBIAN_FRONTEND: noninteractive
  # Locale settings do not affect the build, but might affect tests.
  LC_ALL: C

  CI_REGISTRY_IMAGE: registry.gitlab.isc.org/isc-projects/images/bind9
  CCACHE_DIR: "/ccache"
  SOFTHSM2_CONF: "/var/tmp/softhsm2/softhsm2.conf"

stages:
  - precheck
  - build
  - test
  - push

### Runner Tag Templates

.linux-amd64: &linux_amd64
  tags:
    - linux
    - amd64

.linux-i386: &linux_i386
  tags:
    - linux
    - i386

### Docker Image Templates

# CentOS

.centos-centos6-amd64: &centos_centos6_amd64_image
  image: "$CI_REGISTRY_IMAGE:centos-centos6-amd64"
  <<: *linux_amd64

.centos-centos7-amd64: &centos_centos7_amd64_image
  image: "$CI_REGISTRY_IMAGE:centos-centos7-amd64"
  <<: *linux_amd64

# Debian

.debian-jessie-amd64: &debian_jessie_amd64_image
  image: "$CI_REGISTRY_IMAGE:debian-jessie-amd64"
  <<: *linux_amd64

.debian-jessie-i386: &debian_jessie_i386_image
  image: "$CI_REGISTRY_IMAGE:debian-jessie-i386"
  <<: *linux_i386

.debian-stretch-amd64: &debian_stretch_amd64_image
  image: "$CI_REGISTRY_IMAGE:debian-stretch-amd64"
  <<: *linux_amd64

.debian-stretch-i386: &debian_stretch_i386_image
  image: "$CI_REGISTRY_IMAGE:debian-stretch-i386"
  <<: *linux_i386

.debian-sid-amd64: &debian_sid_amd64_image
  image: "$CI_REGISTRY_IMAGE:debian-sid-amd64"
  <<: *linux_amd64

.debian-sid-i386: &debian_sid_i386_image
  image: "$CI_REGISTRY_IMAGE:debian-sid-i386"
  <<: *linux_i386

# Fedora

.fedora-29-amd64: &fedora_29_amd64_image
  image: "$CI_REGISTRY_IMAGE:fedora-29-amd64"
  <<: *linux_amd64

# Ubuntu

.ubuntu-xenial-amd64: &ubuntu_xenial_amd64_image
  image: "$CI_REGISTRY_IMAGE:ubuntu-xenial-amd64"
  <<: *linux_amd64

.ubuntu-xenial-i386: &ubuntu_xenial_i386_image
  image: "$CI_REGISTRY_IMAGE:ubuntu-xenial-i386"
  <<: *linux_i386

.ubuntu-bionic-amd64: &ubuntu_bionic_amd64_image
  image: "$CI_REGISTRY_IMAGE:ubuntu-bionic-amd64"
  <<: *linux_amd64

.ubuntu-bionic-i386: &ubuntu_bionic_i386_image
  image: "$CI_REGISTRY_IMAGE:ubuntu-bionic-i386"
  <<: *linux_i386

### Job Templates

.default-triggering-rules: &default_triggering_rules
  only:
    - merge_requests
    - tags
    - web

.precheck: &precheck_job
  <<: *default_triggering_rules
  <<: *debian_sid_amd64_image
  stage: precheck

.build: &build_job
  <<: *default_triggering_rules
  stage: build
  before_script:
    - test -w "${CCACHE_DIR}" && export PATH="/usr/lib/ccache:${PATH}"
  script:
    - ./configure --enable-developer --with-libtool --disable-static --with-cmocka --prefix=$HOME/.local --without-make-clean $EXTRA_CONFIGURE || cat config.log
    - make -j${BUILD_PARALLEL_JOBS:-1} -k all V=1
    - test -z "${RUN_MAKE_INSTALL}" || make install
  artifacts:
    untracked: true
    expire_in: "1 hour"

.system_test: &system_test_job
  <<: *default_triggering_rules
  stage: test
  retry: 2
  before_script:
    - bash -x bin/tests/system/ifconfig.sh up
    - bash -x util/prepare-softhsm2.sh
  script:
    - ( cd bin/tests && make -j${TEST_PARALLEL_JOBS:-1} -k test V=1 )
    - test -s bin/tests/system/systests.output
  artifacts:
    untracked: true
    expire_in: "1 week"
    when: on_failure

.unit_test: &unit_test_job
  <<: *default_triggering_rules
  stage: test
  before_script:
    - export KYUA_RESULT="$CI_PROJECT_DIR/kyua.results"
    - bash -x util/prepare-softhsm2.sh
  script:
    - make unit
  after_script:
    - kyua report-html --force --results-file kyua.results --results-filter "" --output kyua_html
  artifacts:
    paths:
      - kyua.log
      - kyua.results
      - kyua_html/
    expire_in: "1 week"
    when: on_failure

### Job Definitions

# Jobs in the precheck stage

misc:sid:amd64:
  <<: *precheck_job
  script:
    - sh util/check-ans-prereq.sh
    - sh util/checklibs.sh > checklibs.out
    - sh util/tabify-changes < CHANGES > CHANGES.tmp
    - diff -urNap CHANGES CHANGES.tmp
    - rm CHANGES.tmp
    - perl util/check-changes CHANGES
    - perl -w util/merge_copyrights
    - diff -urNap util/copyrights util/newcopyrights
    - rm util/newcopyrights
    - perl -w util/update_copyrights < util/copyrights
    - if test "$(git status --porcelain | grep -Ev '\?\?' | wc -l)" -gt "0"; then git status --short; exit 1; fi
    - xmllint --noout --nonet `git ls-files '*.xml' '*.docbook'`
    - xmllint --noout --nonet --html `git ls-files '*.html'`
  artifacts:
    paths:
      - util/newcopyrights
      - checklibs.out
    expire_in: "1 week"
    when: on_failure

🐞:sid:amd64:
  <<: *precheck_job
  script: util/check-cocci

# Jobs for doc builds on Debian Sid (amd64)

docs:sid:amd64:
  <<: *debian_sid_amd64_image
  stage: build
  script:
    - ./configure || cat config.log
    - make -C doc/misc docbook
    - make -C doc/arm Bv9ARM.html
  artifacts:
    paths:
      - doc/arm/
    expire_in: "1 month"
  only:
    - merge_requests
    - tags
    - web
    - master@isc-projects/bind9
    - /^v9_[1-9][0-9]$/@isc-projects/bind9

push:docs:sid:amd64:
  <<: *debian_sid_amd64_image
  stage: push
  dependencies: []
  script:
    - curl -X POST -F token=$GITLAB_PAGES_DOCS_TRIGGER_TOKEN -F ref=master $GITLAB_PAGES_DOCS_TRIGGER_URL
  only:
    - master@isc-projects/bind9
    - /^v9_[1-9][0-9]$/@isc-projects/bind9

# Jobs for regular GCC builds on CentOS 6 (amd64)

gcc:centos6:amd64:
  variables:
    CC: gcc
    CFLAGS: "-Wall -Wextra -O2 -g"
    EXTRA_CONFIGURE: "--with-libidn2 --disable-warn-error"
  <<: *centos_centos6_amd64_image
  <<: *build_job

system:gcc:centos6:amd64:
  <<: *centos_centos6_amd64_image
  <<: *system_test_job
  dependencies:
    - gcc:centos6:amd64

unit:gcc:centos6:amd64:
  <<: *centos_centos6_amd64_image
  <<: *unit_test_job
  dependencies:
    - gcc:centos6:amd64

# Jobs for regular GCC builds on CentOS 7 (amd64)

gcc:centos7:amd64:
  variables:
    CC: gcc
    CFLAGS: "-Wall -Wextra -O2 -g"
    EXTRA_CONFIGURE: "--with-libidn2"
  <<: *centos_centos7_amd64_image
  <<: *build_job

system:gcc:centos7:amd64:
  <<: *centos_centos7_amd64_image
  <<: *system_test_job
  dependencies:
    - gcc:centos7:amd64

unit:gcc:centos7:amd64:
  <<: *centos_centos7_amd64_image
  <<: *unit_test_job
  dependencies:
    - gcc:centos7:amd64

# Jobs for regular GCC builds on Debian 8 Jessie (amd64)

gcc:jessie:amd64:
  variables:
    CC: gcc
    CFLAGS: "-Wall -Wextra -O2 -g"
    EXTRA_CONFIGURE: "--without-cmocka --with-python"
  <<: *debian_jessie_amd64_image
  <<: *build_job

system:gcc:jessie:amd64:
  <<: *debian_jessie_amd64_image
  <<: *system_test_job
  dependencies:
    - gcc:jessie:amd64

unit:gcc:jessie:amd64:
  <<: *debian_jessie_amd64_image
  <<: *unit_test_job
  dependencies:
    - gcc:jessie:amd64

# Jobs for regular GCC builds on Debian 9 Stretch (amd64)

gcc:stretch:amd64:
  variables:
    CC: gcc
    CFLAGS: "-Wall -Wextra -O2 -g"
  <<: *debian_stretch_amd64_image
  <<: *build_job

system:gcc:stretch:amd64:
  <<: *debian_stretch_amd64_image
  <<: *system_test_job
  dependencies:
    - gcc:stretch:amd64

unit:gcc:stretch:amd64:
  <<: *debian_stretch_amd64_image
  <<: *unit_test_job
  dependencies:
    - gcc:stretch:amd64

# Jobs for regular GCC builds on Debian Sid (amd64)

gcc:sid:amd64:
  variables:
    CC: gcc
    CFLAGS: "-Wall -Wextra -O2 -g"
    EXTRA_CONFIGURE: "--with-libidn2"
    RUN_MAKE_INSTALL: 1
  <<: *debian_sid_amd64_image
  <<: *build_job

system:gcc:sid:amd64:
  <<: *debian_sid_amd64_image
  <<: *system_test_job
  dependencies:
    - gcc:sid:amd64

unit:gcc:sid:amd64:
  <<: *debian_sid_amd64_image
  <<: *unit_test_job
  dependencies:
    - gcc:sid:amd64

# Jobs for regular GCC builds on Debian Sid (i386)

gcc:sid:i386:
  variables:
    CC: gcc
    CFLAGS: "-Wall -Wextra -O2 -g"
    EXTRA_CONFIGURE: "--with-libidn2 --without-python"
  <<: *debian_sid_i386_image
  <<: *build_job

system:gcc:sid:i386:
  <<: *debian_sid_i386_image
  <<: *system_test_job
  dependencies:
    - gcc:sid:i386

unit:gcc:sid:i386:
  <<: *debian_sid_i386_image
  <<: *unit_test_job
  dependencies:
    - gcc:sid:i386

# Jobs for regular GCC builds on Fedora 29 (amd64)

gcc:fedora29:amd64:
  variables:
    CC: gcc
    CFLAGS: "-Wall -Wextra -O2 -g"
    EXTRA_CONFIGURE: "--with-libidn2"
  <<: *fedora_29_amd64_image
  <<: *build_job

system:gcc:fedora29:amd64:
  <<: *fedora_29_amd64_image
  <<: *system_test_job
  dependencies:
    - gcc:fedora29:amd64

unit:gcc:fedora29:amd64:
  <<: *fedora_29_amd64_image
  <<: *unit_test_job
  dependencies:
    - gcc:fedora29:amd64

# Jobs for regular GCC builds on Ubuntu 16.04 Xenial Xerus (amd64)

gcc:xenial:amd64:
  variables:
    CC: gcc
    CFLAGS: "-Wall -Wextra -O2 -g"
  <<: *ubuntu_xenial_amd64_image
  <<: *build_job

system:gcc:xenial:amd64:
  <<: *ubuntu_xenial_amd64_image
  <<: *system_test_job
  dependencies:
    - gcc:xenial:amd64

unit:gcc:xenial:amd64:
  <<: *ubuntu_xenial_amd64_image
  <<: *unit_test_job
  dependencies:
    - gcc:xenial:amd64

# Jobs for regular GCC builds on Ubuntu 18.04 Bionic Beaver (amd64)

gcc:bionic:amd64:
  variables:
    CC: gcc
    CFLAGS: "-Wall -Wextra -O2 -g"
    EXTRA_CONFIGURE: "--with-libidn2"
  <<: *ubuntu_bionic_amd64_image
  <<: *build_job

system:gcc:bionic:amd64:
  <<: *ubuntu_bionic_amd64_image
  <<: *system_test_job
  dependencies:
    - gcc:bionic:amd64

unit:gcc:bionic:amd64:
  <<: *ubuntu_bionic_amd64_image
  <<: *unit_test_job
  dependencies:
    - gcc:bionic:amd64

# Jobs for GCC builds with ASAN enabled on Debian Sid (amd64)

asan:sid:amd64:
  variables:
    CC: gcc
    CFLAGS: "-Wall -Wextra -O2 -g -fsanitize=address,undefined -DISC_MEM_USE_INTERNAL_MALLOC=0"
    LDFLAGS: "-fsanitize=address,undefined"
    EXTRA_CONFIGURE: "--with-libidn2"
  <<: *debian_sid_amd64_image
  <<: *build_job

system:asan:sid:amd64:
  <<: *debian_sid_amd64_image
  <<: *system_test_job
  dependencies:
    - asan:sid:amd64

unit:asan:sid:amd64:
  <<: *debian_sid_amd64_image
  <<: *unit_test_job
  dependencies:
    - asan:sid:amd64

# Jobs for Clang builds on Debian Stretch (amd64)

clang:stretch:amd64:
  variables:
    CC: clang
    CFLAGS: "-Wall -Wextra -Wenum-conversion -O2 -g"
    EXTRA_CONFIGURE: "--with-python=python3"
  <<: *debian_stretch_amd64_image
  <<: *build_job

unit:clang:stretch:amd64:
  <<: *debian_stretch_amd64_image
  <<: *unit_test_job
  dependencies:
    - clang:stretch:amd64

# Jobs for Clang builds on Debian Stretch (i386)

clang:stretch:i386:
  variables:
    CC: clang
    CFLAGS: "-Wall -Wextra -Wenum-conversion -O2 -g"
    EXTRA_CONFIGURE: "--with-python=python2"
  <<: *debian_stretch_i386_image
  <<: *build_job

# Jobs for PKCS#11-enabled GCC builds on Debian Sid (amd64)

pkcs11:sid:amd64:
  variables:
    CC: gcc
    CFLAGS: "-Wall -Wextra -O2 -g"
    EXTRA_CONFIGURE: "--enable-native-pkcs11 --with-pkcs11=/usr/lib/softhsm/libsofthsm2.so"
  <<: *debian_sid_amd64_image
  <<: *build_job

system:pkcs11:sid:amd64:
  <<: *debian_sid_amd64_image
  <<: *system_test_job
  dependencies:
    - pkcs11:sid:amd64

unit:pkcs11:sid:amd64:
  <<: *debian_sid_amd64_image
  <<: *unit_test_job
  dependencies:
    - pkcs11:sid:amd64
