image: "debian:stretch"

stages:
  - docbuild
  - build
  - publish
  # - test
  - deploy

### build ###

docbuild:docs:
  stage: docbuild
  image: "tianon/latex"
  script:
    - apt-get update -qq
    - apt-get install -y --no-install-recommends make sphinx-common python3-sphinx python3-recommonmark python3-sphinx-rtd-theme
    - make docs
  artifacts:
    paths:
      - doc/_build/html

build:stretch: &build
  stage: build
  script:
    - apt-get update -qq
    - apt-get -y --no-install-recommends build-dep ./
    - apt-get -y --no-install-recommends install devscripts
    - ci/rebuild.sh $dist $is_release
    - make deb BUILD_ARGS=-Pstretch
    - mkdir -p debs
    - mv ../*.* debs
  variables:
    dist: deb9
  artifacts:
    expire_in: 2 weeks
    paths:
      - debs/*
      - lintian.log

build:stretch-release:
  <<: *build
  variables:
    dist: deb9
    is_release: release
  when: manual

build:buster: &build_buster
  <<: *build
  image: "debian:buster"
  script:
    - apt-get update -qq
    - apt-get -y --no-install-recommends build-dep ./
    - apt-get -y --no-install-recommends install devscripts
    - ci/rebuild.sh $dist $is_release
    - make deb
    - mkdir -p debs
    - mv ../*.* debs
  variables:
    dist: deb10

build:buster-release:
  <<: *build_buster
  variables:
    dist: deb10
    is_release: release
  when: manual

build:bionic: &build_bionic
  <<: *build_buster
  variables:
    dist: ubuntu18.04

build:bionic-release:
  <<: *build_bionic
  variables:
    dist: ubuntu18.04
    is_release: release
  when: manual

build:centos7: &build_centos7
  image: "centos:centos7"
  stage: build
  dependencies:
    - docbuild:docs
  script:
    - yum install -y rpm-build yum-utils git make
    - yum-builddep -y rpm/elephant-shed.spec
    - make rpmbuild PACKAGE_RELEASE=1~$(date -u +%Y%m%d.%H%M%S)
  artifacts:
    expire_in: 2 weeks
    paths:
      - rpm/RPMS/*/*
      - rpm/SRPMS/*

build:centos7-release:
  <<: *build_centos7
  script:
    - yum install -y rpm-build yum-utils git make
    - yum-builddep -y rpm/elephant-shed.spec
    - make rpmbuild
  when: manual

### publish ###

publish:stretch: &publish
  tags:
    - aptly
  stage: publish
  dependencies:
    - build:stretch
  script:
    - aptly repo add $repo debs/*.dsc debs/*.deb
    - aptly publish update $repo
    - sync_packages.sh
  variables:
    repo: stretch-test
  only:
    - master
    - rel1_3

publish:stretch-release:
  <<: *publish
  dependencies:
    - build:stretch-release
  variables:
    repo: stretch-stable
  when: manual

publish:buster: &publish_buster
  <<: *publish
  dependencies:
    - build:buster
  variables:
    repo: buster-test

publish:buster-release:
  <<: *publish_buster
  dependencies:
    - build:buster-release
  variables:
    repo: buster-stable
  when: manual

publish:bionic: &publish_bionic
  <<: *publish
  dependencies:
    - build:bionic
  variables:
    repo: bionic-test

publish:bionic-release:
  <<: *publish_bionic
  dependencies:
    - build:bionic-release
  variables:
    repo: bionic-stable
  when: manual

publish:centos7: &publish_centos
  <<: *publish
  dependencies:
    - build:centos7
  script:
    - rpm --addsign rpm/RPMS/noarch/*.rpm rpm/SRPMS/*.rpm
    - cp rpm/RPMS/noarch/*.rpm /home/debian/postgresql/yum/$repo/noarch
    - cp rpm/SRPMS/*.rpm /home/debian/postgresql/yum/$repo/src
    - cd /home/debian; make $repo
  variables:
    repo: el7-test

publish:centos7-release:
  <<: *publish_centos
  dependencies:
    - build:centos7-release
  variables:
    repo: el7
  when: manual

### deploy ###

# requires gitlab-runner to be configured to execute shell commands as root:
# rm /home/gitlab-runner/.bash_logout
# systemctl edit gitlab-runner
# [Service]
# ExecStart=
# ExecStart=/usr/bin/gitlab-runner "run" "--working-directory" "/home/gitlab-runner" "--config" "/etc/gitlab-runner/config.toml" "--service" --user root

deploy:stretch-x86: &deploy
  stage: deploy
  dependencies:
    - publish:stretch
  tags:
    - stretch-amd64
  script:
    - 'test -f /etc/apt/sources.list.d/credativ_postgresql.list || echo "deb https://packages.credativ.com/public/postgresql/ $(lsb_release -cs)-test main" > https://packages.credativ.com/public/postgresql/'
    - apt-get update -q
    #- apt-get install -y ansible
    - apt-get install -y 'elephant-shed.*'
  only:
    - master
    - rel1_3
  environment:
    name: elephant-shed
    url: https://elephant-shed.dev.credativ.de

deploy:buster-x86:
  <<: *deploy
  dependencies:
    - publish:buster
  tags:
    - buster-amd64
  environment:
    name: elephant-shed-buster
    url: https://elephant-shed-buster.dev.credativ.de

deploy:bionic-x86:
  <<: *deploy
  dependencies:
    - publish:bionic
  tags:
    - bionic-amd64
  environment:
    name: elephant-shed-bionic
    url: https://elephant-shed-bionic.dev.credativ.de

deploy:centos7-x86:
  <<: *deploy
  stage: deploy
  dependencies:
    - build:centos7
  tags:
    - centos7-x86
  script:
    - 'test -f /etc/yum.repos.d/pgdg-redhat-all.repo || yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm'
    - 'test -f /etc/yum.repos.d/credativ-test.repo || yum install -y https://packages.credativ.com/public/postgresql/yum/credativ-test-repo.rpm'
    - yum clean expire-cache
    - yum update -y
    - yum install -y elephant-shed
  environment:
    name: elephant-shed-centos7
    url: https://elephant-shed-centos7.dev.credativ.de
