#image: ${CI_REGISTRY}/storm2/build/ngx-voms-build:master-latest
image: storm2/ngx-voms-build:latest

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

build-rpm:
  stage: build
  script:
    - env | sort
    - export VOMS_MODULE_HOME=${CI_PROJECT_DIR}
    - cd rpm && sh make_packaging.sh && cd ..
    - mv ${HOME}/rpmbuild.tar.gz .
    - mv ${HOME}/rpmbuild ./rpmbuild
  artifacts:
    paths:
      - rpmbuild.tar.gz
      - rpmbuild/SRPMS/
      - rpmbuild/RPMS/noarch/
      - rpmbuild/RPMS/x86_64/

docker-build-rpm:
  stage: docker-build
  image: docker:latest
  services:
    - docker:dind
  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/ngx-voms-packaging/
    - cp rpmbuild/RPMS/noarch/* ${CI_PROJECT_DIR}/docker/ngx-voms-packaging/
    - 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 && cd ngx-voms-packaging && build-docker-image.sh && push-docker-image.sh

push-to-dockerhub:
  stage: docker-push
  image: docker:latest
  services:
    - docker:dind
  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 && cd ngx-voms-packaging && pull-docker-image.sh && cd .. && unset DOCKER_REGISTRY_HOST
    - docker login -u ${DOCKERHUB_USER} -p ${DOCKERHUB_PASSWORD}
    - cd ngx-voms-packaging && push-docker-images.sh
  only:
    - master

