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

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

.image: &image
  stage: image
  before_script:
    - docker info
  script:
    - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY
    - docker build --no-cache -t "$IMAGE_TAG" "scripts/docker/$IMAGE_NAME"
    - docker push "$IMAGE_TAG"
  tags:
    - dind
  only:
    variables:
      - $SCHEDULE_TYPE == "weekly"

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

.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: "$CI_REGISTRY/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 $EXTRA_CONFIGURE || ( 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

.pkg_symbols: &pkg_symbols
  stage: test
  script:
    - ln -s distro/deb debian
    - sed -i "s/__VERSION__/99/g" distro/deb/changelog
    - dpkg-gensymbols -c4 -esrc/.libs/$LIB_NAME.so.$LIB_ABI -P. -p$LIB_NAME$LIB_ABI
  allow_failure: true
  except:
  only:
    - schedules
  dependencies:
    - build:debian:amd64

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:
  variables:
    EXTRA_CONFIGURE: --enable-xdp=yes
  <<: *debian_stable
  <<: *build_job
  except:  # run on schedules as well, for debian:symbols tests

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

pkg:debian:symbols:libknot:
  variables:
    LIB_NAME: libknot
    LIB_ABI: 11
  <<: *debian_stable
  <<: *pkg_symbols

pkg:debian:symbols:libdnssec:
  variables:
    LIB_NAME: libdnssec
    LIB_ABI: 8
  <<: *debian_stable
  <<: *pkg_symbols

pkg:debian:symbols:libzscanner:
  variables:
    LIB_NAME: libzscanner
    LIB_ABI: 3
  <<: *debian_stable
  <<: *pkg_symbols

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-9
    CFLAGS: "-fsanitize=address -g -O2 -fno-omit-frame-pointer"
    LDFLAGS: "-fsanitize=address"
    ASAN_SYMBOLIZER_PATH: /usr/lib/llvm-9/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-9
    CFLAGS: "-fsanitize=address -g -O2"
    LDFLAGS: "-fsanitize=address"
    ASAN_SYMBOLIZER_PATH: /usr/lib/llvm-9/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-9
    CFLAGS: "-fsanitize=undefined -fno-sanitize=nonnull-attribute -g -O2"
    LDFLAGS: "-fsanitize=undefined"
    UBSAN_SYMBOLIZER_PATH: /usr/lib/llvm-9/bin/llvm-symbolizer
    UBSAN_OPTIONS: print_stacktrace=1
  allow_failure: true
  <<: *debian_unstable
  <<: *build_job

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

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

obs:trigger:
  <<: *debian_stable
  stage: deploy
  only:
    variables:
      - $OBS_REPO
  except: []
  dependencies:
    - build:archive
  environment:
    name: OBS/$OBS_REPO
    url: https://build.opensuse.org/package/show/home:CZ-NIC:$OBS_REPO/knot
  tags:
    - respdiff
  script:
    - mv _build/*.tar.xz ./
    - scripts/make-distrofiles.sh
    - scripts/build-in-obs.sh $OBS_REPO

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

.deploytest: &deploytest
  stage: deploy-test
  only:
    variables:
      - $OBS_REPO =~ /^knot-dns-devel|knot-dns-latest|knot-dns-testing$/
  dependencies: []
  when: delayed
  start_in: 3 minutes  # give OBS build some time
  tags:
    - respdiff

obs:build:all:
  <<: *deploytest
  allow_failure: true
  script:
    - "osc results home:CZ-NIC:$OBS_REPO knot -w"
    - version=$(sed 's/^v//' <(git describe --exact-match HEAD || git rev-parse --short HEAD) )
    - > # check version only for one (reliable) repo to avoid false negatives
      ! osc ls -b home:CZ-NIC:$OBS_REPO knot Debian_9.0 x86_64 | \
        grep -E '(rpm|deb|tar\.xz)$' | grep -v $version || \
        (echo "ERROR: version mismatch"; exit 1)
    - >
      ! osc results home:CZ-NIC:$OBS_REPO knot --csv | \
        grep -Ev 'disabled|excluded|Rawhide' | grep -v 'succeeded' -q || \
        (echo "ERROR: build(s) failed"; exit 1)

.distrotest: &distrotest
  <<: *deploytest
  script:
    - "osc results home:CZ-NIC:$OBS_REPO knot -a x86_64 -r $DISTROTEST_REPO -w"
    - >
      osc results home:CZ-NIC:$OBS_REPO knot -a x86_64 -r $DISTROTEST_REPO --csv | grep 'succeeded|$' -q || \
        (echo "ERROR: build failed"; exit 1)
    - export LABEL="gl$(date +%s)_$OBS_REPO"
    - export COMMITDIR="/var/tmp/respdiff-jobs/$(git rev-parse --short HEAD)-$LABEL"
    - export TESTDIR="$COMMITDIR/distrotest.$DISTROTEST_NAME"
    - ln -s $COMMITDIR distrotest_commitdir
    - sudo -u respdiff /var/opt/respdiff/contrib/job_manager/submit.py -w
      -p $DISTROTEST_PRIORITY
      $(sudo -u respdiff /var/opt/respdiff/contrib/job_manager/create.py
        "$(git rev-parse --short HEAD)" -l $LABEL -t distrotest.$DISTROTEST_NAME
        --package knot-dns --obs-repo $OBS_REPO)
    - export EXITCODE=$(cat $TESTDIR/j*_exitcode)
    - if [[ "$EXITCODE" != "0" ]]; then cat $TESTDIR/j*_{vagrant.log.txt,stdout.txt}; fi
    - exit $EXITCODE
  after_script:
    - 'cp -t . distrotest_commitdir/distrotest.$DISTROTEST_NAME/j* ||:'
  artifacts:
    when: always
    expire_in: 1 week
    paths:
      - ./j*
  retry:
    max: 1
    when:
      - script_failure

obs:centos7:x86_64:
  <<: *distrotest
  variables:
    DISTROTEST_NAME: centos7
    DISTROTEST_REPO: CentOS_7_EPEL

obs:centos8:x86_64:
  <<: *distrotest
  variables:
    DISTROTEST_NAME: centos8
    DISTROTEST_REPO: CentOS_8_EPEL

obs:debian9:x86_64:
  <<: *distrotest
  variables:
    DISTROTEST_NAME: debian9
    DISTROTEST_REPO: Debian_9.0

obs:debian10:x86_64:
  <<: *distrotest
  variables:
    DISTROTEST_NAME: debian10
    DISTROTEST_REPO: Debian_10

obs:fedora32:x86_64:
  <<: *distrotest
  variables:
    DISTROTEST_NAME: fedora32
    DISTROTEST_REPO: Fedora_32

obs:fedora33:x86_64:
  <<: *distrotest
  variables:
    DISTROTEST_NAME: fedora33
    DISTROTEST_REPO: Fedora_33

obs:leap15:x86_64:
  <<: *distrotest
  variables:
    DISTROTEST_NAME: leap15
    DISTROTEST_REPO: openSUSE_Leap_15.2

obs:ubuntu1604:x86_64:
  <<: *distrotest
  variables:
    DISTROTEST_NAME: ubuntu1604
    DISTROTEST_REPO: xUbuntu_16.04

obs:ubuntu1804:x86_64:
  <<: *distrotest
  variables:
    DISTROTEST_NAME: ubuntu1804
    DISTROTEST_REPO: xUbuntu_18.04

obs:ubuntu2004:x86_64:
  <<: *distrotest
  variables:
    DISTROTEST_NAME: ubuntu2004
    DISTROTEST_REPO: xUbuntu_20.04

obs:ubuntu2010:x86_64:
  <<: *distrotest
  variables:
    DISTROTEST_NAME: ubuntu2010
    DISTROTEST_REPO: xUbuntu_20.10
