
stages:
  - build
  - docker-build
  - docker-push

build-rpm:
  stage: build
  image: centos:7
  script:
    - env | sort
    - sh .devcontainer/assets/install-build-deps.sh
    - yum install -y rpm-build redhat-rpm-config rpmdevtools
    - cd rpm && sh make_packaging.sh ${CI_PROJECT_DIR} && cd ..
    - mv ${HOME}/rpmbuild ./rpmbuild
  artifacts:
    paths:
      - rpmbuild/SRPMS/
      - rpmbuild/RPMS/noarch/
      - rpmbuild/RPMS/x86_64/

docker-build-rpm:
  stage: docker-build
  image: docker:latest
  services:
    - name: docker:dind
      command: ["--tls=false"]
  dependencies:
    - build-rpm
  script:
    - apk add git bash
    - git clone https://baltig.infn.it/mw-devel/helper-scripts.git helper-scripts
    - cp helper-scripts/scripts/* /usr/local/bin
    - cp rpmbuild/RPMS/x86_64/* ${CI_PROJECT_DIR}/docker/openresty-voms/
    - cp rpmbuild/RPMS/noarch/* ${CI_PROJECT_DIR}/docker/openresty-voms/ 
    - rm ${CI_PROJECT_DIR}/docker/openresty-voms/*-debuginfo*.rpm
    - docker login -u gitlab-ci-token -p ${CI_JOB_TOKEN} ${CI_REGISTRY}
    - export DOCKER_REGISTRY_HOST=${CI_REGISTRY}
    - export DOCKER_REGISTRY_NAMESPACE=${CI_PROJECT_PATH}
    - cd docker/openresty-voms && build-docker-image.sh && push-docker-image.sh

push-to-dockerhub:
  stage: docker-push
  image: docker:latest
  services:
    - name: docker:dind
      command: ["--tls=false"]
  dependencies:
    - docker-build-rpm
  script:
    - apk add git bash
    - git clone https://baltig.infn.it/mw-devel/helper-scripts.git helper-scripts
    - cp helper-scripts/scripts/* /usr/local/bin
    - export DOCKER_PUSH_TO_DOCKERHUB=y
    - env | sort
    - docker login -u gitlab-ci-token -p ${CI_JOB_TOKEN} ${CI_REGISTRY}
    - export DOCKER_REGISTRY_HOST=${CI_REGISTRY}
    - export DOCKER_REGISTRY_NAMESPACE=${CI_PROJECT_PATH}
    - cd docker/openresty-voms && pull-docker-image.sh && unset DOCKER_REGISTRY_HOST
    - docker login -u ${DOCKERHUB_USER} -p ${DOCKERHUB_PASSWORD} && push-docker-image.sh
  only:
    - master
