image: storm2/ngx-voms-build:latest

stages:
  - build
  - test
  - docker-build
  - deploy

build4c:
  stage: build
  script:
    - env
    - sh ${HOME}/build-install-ngx-voms.sh -d -c
    - mv ${HOME}/local local
    - mv ${HOME}/openresty-1.13.6.1/build/nginx-1.13.6 nginx-1.13.6
    - tar cvzf artifacts.tar.gz local nginx-1.13.6
  artifacts:
    paths:
      - artifacts.tar.gz

test4c:
  stage: test
  dependencies:
    - build4c
  script:
    - rm -rf ${HOME}/local/
    - rm -rf ${HOME}/openresty-1.13.6.1/build/nginx-1.13.6/
    - tar xvzf artifacts.tar.gz
    - mv local ${HOME}
    - mv nginx-1.13.6 ${HOME}/openresty-1.13.6.1/build/
    - sh test-ngx-voms.sh
    - sh cov-ngx-voms.sh
    - mv /tmp/coverage-report coverage
  artifacts:
    paths:
      - coverage

pages:
  stage: deploy
  image: docker:latest
  dependencies:
    - test4c
  script:
    - mv coverage/ public/
  artifacts:
    paths:
      - public
    expire_in: 30 days

docker-build:
  stage: docker-build
  image: docker:latest
  services:
    - docker:dind
  dependencies:
    - build4c
  script:
    - tar xvzf artifacts.tar.gz
    - mv local ${HOME}
    - cd ${HOME}/local && rm openresty/nginx/sbin/nginx.old && tar cvzf openresty.tar.gz openresty
    - mv ${HOME}/local/openresty.tar.gz ${CI_PROJECT_DIR}/docker && cd ${CI_PROJECT_DIR}/docker && sh build-image.sh
    - docker tag storm2/ngx-voms:latest ${CI_REGISTRY_IMAGE}/ngx-voms:${CI_COMMIT_SHA:0:8}
    - docker login -u gitlab-ci-token -p ${CI_JOB_TOKEN} ${CI_REGISTRY}
    - docker push ${CI_REGISTRY_IMAGE}/ngx-voms:${CI_COMMIT_SHA:0:8}

dockerhub-push:
  stage: docker-build
  image: docker:latest
  services:
    - docker:dind
  script:
    - docker login -u gitlab-ci-token -p ${CI_JOB_TOKEN} ${CI_REGISTRY}
    - docker pull ${CI_REGISTRY_IMAGE}/ngx-voms:${CI_COMMIT_SHA:0:8}
    - docker tag ${CI_REGISTRY_IMAGE}/ngx-voms:${CI_COMMIT_SHA:0:8} storm2/ngx-voms:${CI_COMMIT_SHA:0:8}
    - docker tag ${CI_REGISTRY_IMAGE}/ngx-voms:${CI_COMMIT_SHA:0:8} storm2/ngx-voms:latest
    - docker login -u ${DOCKERHUB_USER} -p ${DOCKERHUB_PASSWORD}
    - docker push storm2/ngx-voms:${CI_COMMIT_SHA:0:8}
    - docker push storm2/ngx-voms:latest
  only:
    - master
