default:
  interruptible: true

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 # new jinja2 breaks docs (sphinx/breathe)
  - pip3 install -U apkg 'jinja2<3.1'
  - 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

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

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

nixos-unstable:pkgbuild:
  <<: *pkgbuild
  # We do NOT use LXC, for now at least.
  parallel:
    matrix:
      - PLATFORM: [ amd64, arm64 ]
  tags:
    - docker
    - linux
    - ${PLATFORM}
  # https://github.com/NixOS/nix/issues/10648#issuecomment-2101993746
  image: docker.io/nixos/nix:latest-${PLATFORM}

  variables:
    NIX_PATH: nixpkgs=https://github.com/nixos/nixpkgs/archive/nixos-unstable.tar.gz
  before_script:
  script:
    - nix-build '<nixpkgs>' -QA apkg
    # the image auto-detects as alpine distro
    # If apkg version differs (too much), it will fail to reuse archive and fail.
    - ./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 +edns nic.cz | tee /dev/stderr | grep -qi 'status: NOERROR'"

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 +edns nic.cz | tee /dev/stderr | grep -qi 'status: 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 +edns nic.cz | tee /dev/stderr | grep -qi 'status: 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 +edns nic.cz | tee /dev/stderr | grep -qi 'status: 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
# }}}
