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

build-rpms-el8:
  stage: build
  image: almalinux:8
  script: 
    - env | sort
    - dnf -y install epel-release
    - dnf install -y wget openssl-devel zlib-devel pcre2-devel make rpmdevtools rpmlint boost-devel voms-devel gcc-c++
    - source .devcontainer/assets/build-library.sh
    - CI_PROJECT_DIR=$PWD
    - downloadNginx
    - buildHttpgNginxRPM
    - buildVomsModuleRPM
    - cd ${CI_PROJECT_DIR}/docker && mkdir artifacts
    - cp ~/rpmbuild/SRPMS/* artifacts/
    - cp ~/rpmbuild/RPMS/x86_64/* artifacts/
  artifacts:
    paths:
      - docker/artifacts/

build-rpms-el9:
  stage: build
  image: almalinux:9
  script: 
    - env | sort
    - dnf -y install epel-release
    - dnf install -y wget openssl-devel zlib-devel pcre2-devel make rpmdevtools rpmlint boost-devel voms-devel gcc-c++
    - source .devcontainer/assets/build-library.sh
    - CI_PROJECT_DIR=$PWD
    - downloadNginx
    - buildHttpgNginxRPM
    - buildVomsModuleRPM
    - cd ${CI_PROJECT_DIR}/docker && mkdir artifacts
    - cp ~/rpmbuild/SRPMS/* artifacts/
    - cp ~/rpmbuild/RPMS/x86_64/* artifacts/
  artifacts:
    paths:
      - docker/artifacts/

docker-build-rpms:
  stage: docker-build
  image: docker:latest
  services:
    - name: docker:dind
      command: ["--tls=false"]
  dependencies:
    - build-rpms-el8
    - build-rpms-el9
  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
    - rm ${CI_PROJECT_DIR}/docker/artifacts/*-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 && 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-rpms
  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 && pull-docker-image.sh && unset DOCKER_REGISTRY_HOST
    - docker login -u ${DOCKERHUB_USER} -p ${DOCKERHUB_PASSWORD} && push-docker-image.sh
  only:
    - master
