stages:
  - pkgbuild
  - pkgtest

# pkgbuild {{{
.pkgbuild: &pkgbuild
  stage: pkgbuild
  tags:
    - lxc
    - amd64
  before_script:
    - git config --global user.name CI
    - git config --global user.email ci@nic
  needs: # https://gitlab.nic.cz/help/ci/yaml/README.md#artifact-downloads-to-child-pipelines
    - pipeline: $PARENT_PIPELINE_ID
      job: archive
  artifacts:
    when: always
    expire_in: '1 day'
    paths:
      - pkg/

.apkgbuild: &apkgbuild
  - pip3 install -U apkg
  - apkg build-dep -y
  - apkg build

.pkgdebrepo: &pkgdebrepo
  - apt-get update
  - apt-get install -y curl gnupg2
  - echo "deb http://download.opensuse.org/repositories/home:/CZ-NIC:/$OBS_REPO/$DISTROTEST_REPO/ /" > /etc/apt/sources.list.d/obs.list
  - curl -fsSL "https://download.opensuse.org/repositories/home:CZ-NIC:$OBS_REPO/$DISTROTEST_REPO/Release.key" | gpg --dearmor > /etc/apt/trusted.gpg.d/obs.gpg
  - apt-get update

.debpkgbuild: &debpkgbuild
  - *pkgdebrepo
  - apt-get install -y python3-pip devscripts
  - *apkgbuild

centos-7:pkgbuild:
  <<: *pkgbuild
  image: $CI_REGISTRY/labs/lxc-gitlab-runner/centos-7
  before_script:
    - export LC_ALL=en_US.UTF-8
    - git config --global user.name CI
    - git config --global user.email ci@nic
  script:
    - yum install -y rpm-build python3-pip epel-release
    - *apkgbuild

debian-9:pkgbuild:
  <<: *pkgbuild
  image: $CI_REGISTRY/labs/lxc-gitlab-runner/debian-9
  variables:
    OBS_REPO: knot-resolver-build
    DISTROTEST_REPO: Debian_9.0
  script:
    - *debpkgbuild

debian-10:pkgbuild:
  <<: *pkgbuild
  image: $CI_REGISTRY/labs/lxc-gitlab-runner/debian-10
  variables:
    OBS_REPO: knot-resolver-build
    DISTROTEST_REPO: Debian_10
  script:
    - *debpkgbuild

debian-11:pkgbuild:
  <<: *pkgbuild
  image: $CI_REGISTRY/labs/lxc-gitlab-runner/debian-11
  variables:
    OBS_REPO: knot-resolver-build
    DISTROTEST_REPO: Debian_11
  script:
    - *debpkgbuild

fedora-34:pkgbuild:
  <<: *pkgbuild
  image: $CI_REGISTRY/labs/lxc-gitlab-runner/fedora-34
  script:
    - dnf install -y rpm-build python3-pip
    - *apkgbuild

fedora-35:pkgbuild:
  <<: *pkgbuild
  image: $CI_REGISTRY/labs/lxc-gitlab-runner/fedora-35
  script:
    - dnf install -y rpm-build python3-pip
    - *apkgbuild

opensuse-15.2:pkgbuild:
  <<: *pkgbuild
  image: $CI_REGISTRY/labs/lxc-gitlab-runner/opensuse-15.2
  script:
    - zypper addrepo -G -f https://download.opensuse.org/repositories/home:CZ-NIC:knot-resolver-build/openSUSE_Leap_15.2/home:CZ-NIC:knot-resolver-build.repo
    - zypper install -y rpm-build python3-pip
    - *apkgbuild

opensuse-15.3:pkgbuild:
  <<: *pkgbuild
  image: $CI_REGISTRY/labs/lxc-gitlab-runner/opensuse-15.3
  script:
    - zypper addrepo -G -f https://download.opensuse.org/repositories/home:CZ-NIC:knot-resolver-build/openSUSE_Leap_15.3/home:CZ-NIC:knot-resolver-build.repo
    - zypper install -y rpm-build python3-pip
    - *apkgbuild

rocky-8:pkgbuild:
  <<: *pkgbuild
  image: $CI_REGISTRY/labs/lxc-gitlab-runner/rocky-8
  script:
    - dnf install -y rpm-build python3-pip epel-release dnf-plugins-core
    - dnf config-manager --set-enabled powertools
    - *apkgbuild

ubuntu-18.04:pkgbuild:
  <<: *pkgbuild
  image: $CI_REGISTRY/labs/lxc-gitlab-runner/ubuntu-18.04
  variables:
    OBS_REPO: knot-resolver-build
    DISTROTEST_REPO: xUbuntu_18.04
  script:
    - *debpkgbuild

ubuntu-20.04:pkgbuild:
  <<: *pkgbuild
  image: $CI_REGISTRY/labs/lxc-gitlab-runner/ubuntu-20.04
  variables:
    OBS_REPO: knot-resolver-build
    DISTROTEST_REPO: xUbuntu_20.04
  script:
    - *debpkgbuild

ubuntu-21.10:pkgbuild:
  <<: *pkgbuild
  image: $CI_REGISTRY/labs/lxc-gitlab-runner/ubuntu-21.10
  variables:
    OBS_REPO: knot-resolver-build
    DISTROTEST_REPO: xUbuntu_21.10
  script:
    - *debpkgbuild

nixos-unstable:pkgbuild:
  <<: *pkgbuild
  # We do NOT use LXC, for now at least.
  tags:
    - docker
    - linux
    - amd64
  # TODO: untag after resolving problems around https://github.com/NixOS/nix/issues/5776
  image: nixos/nix:2.3.12

  variables:
    #NIX_PATH: nixpkgs=https://github.com/nixos/nixpkgs/archive/nixos-unstable.tar.gz
    NIX_PATH: nixpkgs=https://github.com/vcunat/nixpkgs/archive/p/apkg.tar.gz
  before_script:
  script:
    - nix-build '<nixpkgs>' -QA apkg
    # the image auto-detects as alpine distro
    - ./result/bin/apkg install -d nix
    - kresd --version
# }}}

# pkgtest {{{
.pkgtest: &pkgtest
  stage: pkgtest
  tags:
    - lxc
    - amd64

.debpkgtest: &debpkgtest
  - *pkgdebrepo
  - apt-get install -y knot-dnsutils
  - apt-get install -y $(find ./pkg/pkgs -name '*.deb' | grep -v module | grep -v debug | grep -v devel)
  - systemctl start kresd@1
  - kdig @127.0.0.1 nic.cz | grep -qi NOERROR

centos-7:pkgtest:
  <<: *pkgtest
  needs:
    - centos-7:pkgbuild
  image: $CI_REGISTRY/labs/lxc-gitlab-runner/centos-7
  before_script:
    - export LC_ALL=en_US.UTF-8
  script:
    - yum install -y epel-release
    - yum install -y knot-utils findutils
    - yum install -y $(find ./pkg/pkgs -name '*.rpm' | grep -v module | grep -v debug | grep -v devel)
    - systemctl start kresd@1
    - kdig @127.0.0.1 nic.cz | grep -qi NOERROR

debian-9:pkgtest:
  <<: *pkgtest
  needs:
    - debian-9:pkgbuild
  image: $CI_REGISTRY/labs/lxc-gitlab-runner/debian-9
  variables:
    OBS_REPO: knot-resolver-build
    DISTROTEST_REPO: Debian_9.0
  script:
    - *debpkgtest

debian-10:pkgtest:
  <<: *pkgtest
  needs:
    - debian-10:pkgbuild
  image: $CI_REGISTRY/labs/lxc-gitlab-runner/debian-10
  variables:
    OBS_REPO: knot-resolver-build
    DISTROTEST_REPO: Debian_10
  script:
    - *debpkgtest

debian-11:pkgtest:
  <<: *pkgtest
  needs:
    - debian-11:pkgbuild
  image: $CI_REGISTRY/labs/lxc-gitlab-runner/debian-11
  variables:
    OBS_REPO: knot-resolver-build
    DISTROTEST_REPO: Debian_11
  script:
    - *debpkgtest

fedora-34:pkgtest:
  <<: *pkgtest
  needs:
    - fedora-34:pkgbuild
  image: $CI_REGISTRY/labs/lxc-gitlab-runner/fedora-34
  script:
    - dnf install -y knot-utils findutils
    - dnf install -y $(find ./pkg/pkgs -name '*.rpm' | grep -v module | grep -v debug | grep -v devel)
    - systemctl start kresd@1
    - kdig @127.0.0.1 nic.cz | grep -qi NOERROR

fedora-35:pkgtest:
  <<: *pkgtest
  needs:
    - fedora-35:pkgbuild
  image: $CI_REGISTRY/labs/lxc-gitlab-runner/fedora-35
  script:
    - dnf install -y knot-utils findutils
    - dnf install -y $(find ./pkg/pkgs -name '*.rpm' | grep -v module | grep -v debug | grep -v devel)
    - systemctl start kresd@1
    - kdig @127.0.0.1 nic.cz | grep -qi NOERROR

opensuse-15.2:pkgtest:
  <<: *pkgtest
  needs:
    - opensuse-15.2:pkgbuild
  image: $CI_REGISTRY/labs/lxc-gitlab-runner/opensuse-15.2
  script:
    - zypper addrepo -G -f https://download.opensuse.org/repositories/home:CZ-NIC:knot-resolver-build/openSUSE_Leap_15.2/home:CZ-NIC:knot-resolver-build.repo
    - zypper install -y knot-utils
    - zypper install --allow-unsigned-rpm -y $(find ./pkg/pkgs -name '*.rpm' | grep -v module | grep -v debug | grep -v devel)
    - systemctl start kresd@1
    - kdig @127.0.0.1 nic.cz | grep -qi NOERROR

opensuse-15.3:pkgtest:
  <<: *pkgtest
  needs:
    - opensuse-15.3:pkgbuild
  image: $CI_REGISTRY/labs/lxc-gitlab-runner/opensuse-15.3
  script:
    - zypper addrepo -G -f https://download.opensuse.org/repositories/home:CZ-NIC:knot-resolver-build/openSUSE_Leap_15.3/home:CZ-NIC:knot-resolver-build.repo
    - zypper install -y knot-utils
    - zypper install --allow-unsigned-rpm -y $(find ./pkg/pkgs -name '*.rpm' | grep -v module | grep -v debug | grep -v devel)
    - systemctl start kresd@1
    - kdig @127.0.0.1 nic.cz | grep -qi NOERROR

rocky-8:pkgtest:
  <<: *pkgtest
  needs:
    - rocky-8:pkgbuild
  image: $CI_REGISTRY/labs/lxc-gitlab-runner/rocky-8
  script:
    - dnf install -y epel-release
    - dnf install -y knot-utils findutils
    - dnf install -y $(find ./pkg/pkgs -name '*.rpm' | grep -v module | grep -v debug | grep -v devel)
    - systemctl start kresd@1
    - kdig @127.0.0.1 nic.cz | grep -qi NOERROR

ubuntu-18.04:pkgtest:
  <<: *pkgtest
  needs:
    - ubuntu-18.04:pkgbuild
  image: $CI_REGISTRY/labs/lxc-gitlab-runner/ubuntu-18.04
  variables:
    OBS_REPO: knot-resolver-build
    DISTROTEST_REPO: xUbuntu_18.04
  script:
    - *debpkgtest

ubuntu-20.04:pkgtest:
  <<: *pkgtest
  needs:
    - ubuntu-20.04:pkgbuild
  image: $CI_REGISTRY/labs/lxc-gitlab-runner/ubuntu-20.04
  variables:
    OBS_REPO: knot-resolver-build
    DISTROTEST_REPO: xUbuntu_20.04
  script:
    - *debpkgtest

ubuntu-21.10:pkgtest:
  <<: *pkgtest
  needs:
    - ubuntu-21.10:pkgbuild
  image: $CI_REGISTRY/labs/lxc-gitlab-runner/ubuntu-21.10
  variables:
    OBS_REPO: knot-resolver-build
    DISTROTEST_REPO: xUbuntu_21.10
  script:
    - *debpkgtest
# }}}
