stages:
- prepare
- test
- build
- bundle_npm
- build_image
- publish
- deploy
- docs
- mirror

variables:
  npm_config_cache: ${CI_PROJECT_DIR}/.npm
  image_name: ubleipzig/dacap

npm_install:
  stage: prepare
  image:
    name: node:10-alpine
    entrypoint: ["/bin/su", "node", "-c"]
  script:
    - npm run build
  cache:
    key: "${CI_PROJECT_ID}"
    paths:
    - .npm
    - lib
    - node_modules
    - public
    - test
  tags:
    - docker

npm_ci:
  stage: test
  image:
    name: node:10-alpine
    entrypoint: ["/bin/su", "node", "-c"]
  script:
  - npm run ci
  cache:
    key: "${CI_PROJECT_ID}"
    paths:
    - .npm
    - lib
    - public
  tags:
  - docker

npm_pack:
  stage: bundle_npm
  image:
    name: node:10-alpine
    entrypoint: ["/bin/su", "node", "-c"]
  script:
  - npm pack
  cache:
    key: "${CI_PROJECT_ID}"
    paths:
    - .npm
  artifacts:
    name: npm-pack
    paths:
    - '*.tgz'
  tags:
    - docker

docker_build:
  stage: build_image
  image: ubleipzig/deployer:1.0.0
  services:
  - docker:dind
  script: |
    deployer build \
      --build-arg HTTP_PROXY="${HTTP_PROXY}" \
      --build-arg HTTPS_PROXY=${HTTPS_PROXY}" \
      --build-arg NO_PROXY=${NO_PROXY}" \
      --output image.tar.gz
  dependencies:
  - npm_pack
  artifacts:
    name: docker-image
    paths:
    - image.tar.gz
  tags:
    - docker

npm_publish:
  stage: publish
  image:
    name: node:10-alpine
    entrypoint: ["/bin/su", "node", "-c"]
  script:
  - 'echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > ~/.npmrc'
  - npm publish *.tgz --access public
  dependencies:
  - npm_pack
  tags:
  - docker
  except:
  - branches
  only:
  - /^release\/.*/

docker_publish_alpha:
  stage: publish
  image: ubleipzig/deployer:1.0.0
  services:
  - docker:dind
  script: |
    deployer publish \
      --input image.tar.gz \
      --docker-config "${DOCKER_AUTH_CONFIG}" \
      --name ${image_name} \
      --tag ${CI_COMMIT_REF_NAME}
  dependencies:
  - docker_build
  tags:
  - docker
  only:
  - branches
  except:
  - master

docker_publish_staging:
  stage: publish
  image: ubleipzig/deployer:1.0.0
  services:
  - docker:dind
  script: |
    deployer publish \
      --input image.tar.gz \
      --docker-config "$DOCKER_AUTH_CONFIG" \
      --name ${image_name} \
      --tag staging
  dependencies:
  - docker_build
  tags:
    - docker
  only:
  - master

docker_publish_production:
  stage: publish
  image: ubleipzig/deployer:1.0.0
  services:
  - docker:dind
  script: |
    version=`expr ${CI_COMMIT_TAG} ':' 'release/\(.\+\)'`
    deployer publish \
      --input image.tar.gz
      --docker-config "$DOCKER_AUTH_CONFIG" \
      --name ${image_name} \
      --tag latest \
      --tag ${version} \
      --tag $(expr ${version} ':' '\([^.]\+\)') \
      --tag $(expr ${version} ':' '[^.]\+\.\([^.]\+\)') \
      --tag $(expr ${version} ':' '[^.]\+\.[^.]\+\.\(.\+\))`
  dependencies:
  - docker_build
  tags:
    - docker
  except:
  - branches
  only:
  - /^release\/.*/

deploy_alpha:
  stage: deploy
  image: ubleipzig/deployer:1.0.0
  variables:
    url_schema: https://
    ingress_host: alpha.ub.uni-leipzig.de
    ingress_path: /dacap
  environment:
    name: alpha/${CI_COMMIT_REF_NAME}
    url: https://alpha.ub.uni-leipzig.de/dacap/${CI_COMMIT_REF_NAME}/admin
  script: |
    deployer deploy \
      --cluster-url "${K8S_ALPHA_CLUSTER_URL}" \
      --certificate-authority "${K8S_ALPHA_CLUSTER_CA}" \
      --token "${K8S_ALPHA_DEPLOY_TOKEN}" \
      --namespace "${K8S_ALPHA_CLUSTER_NAMESPACE}" \
      --service-account "${K8S_ALPHA_DEPLOY_USER}" \
      --name issue-${CI_COMMIT_REF_NAME} \
      --set-string environment.proxy_url=${url_schema}${ingress_host}${ingress_path}/${CI_COMMIT_REF_NAME}/ \
      --set-string environment.http_proxy=http://proxy.uni-leipzig.de:3128 \
      --set-string ingress.path=${ingress_path}/${CI_COMMIT_REF_NAME} \
      --set-string ingress.hosts[0]=${ingress_host} \
      --set-string image.tag=alpha-${CI_COMMIT_REF_NAME} \
      --set-string pullPolicy=Always \
      --charts ./helmcharts
  dependencies:
  - docker_publish_alpha
  tags:
  - docker
  only:
  - branches
  except:
  - master

deploy_staging:
  stage: deploy
  image: ubleipzig/deployer:1.0.0
  variables:
    url_schema: https://
    ingress_host: staging.ub.uni-leipzig.de
    ingress_path: /dacap
  environment:
    name: staging
    url: https://staging.ub.uni-leipzig.de/dacap/admin
  script: |
    deployer deploy \
      --cluster-url "${K8S_STAGING_CLUSTER_URL}" \
      --certificate-authority "${K8S_STAGING_CLUSTER_CA}" \
      --token "${K8S_STAGING_DEPLOY_TOKEN}" \
      --namespace "${K8S_STAGING_CLUSTER_NAMESPACE}" \
      --service-account "${K8S_STAGING_DEPLOY_USER}" \
      --name staging \
      --set-string environment.proxy_url=${url_schema}${ingress_host}${ingress_path}/ \
      --set-string environment.http_proxy=http://proxy.uni-leipzig.de:3128 \
      --set-string ingress.path=${ingress_path} \
      --set-string ingress.hosts[0]=${ingress_host} \
      --set-string image.tag=staging \
      --set-string pullPolicy=Always \
      --charts ./helmcharts

  dependencies:
  - docker_publish_staging
  tags:
  - docker
  only:
  - master

deploy_production:
  stage: deploy
  image: ubleipzig/deployer:1.0.0
  variables:
    url_schema: https://
    ingress_host: api.ub.uni-leipzig.de
    ingress_path: /dacap
  environment:
    name: production
    url: https://api.ub.uni-leipzig.de/dacap/admin
  script: |
    version=`expr ${CI_COMMIT_TAG} ':' 'release/\(.\+\)'`
    deployer deploy \
      --cluster-url "${K8S_PRODUCTION_CLUSTER_URL}" \
      --certificate-authority "${K8S_PRODUCTION_CLUSTER_CA}" \
      --token "${K8S_PRODUCTION_DEPLOY_TOKEN}" \
      --namespace "${K8S_PRODUCTION_CLUSTER_NAMESPACE}" \
      --service-account "${K8S_PRODUCTION_DEPLOY_USER}" \
      --name live \
      --set-string environment.proxy_url=${url_schema}${ingress_host}${ingress_path}/ \
      --set-string environment.http_proxy=http://proxy.uni-leipzig.de:3128 \
      --set-string ingress.path=${ingress_path} \
      --set-string ingress.hosts[0]=${ingress_host} \
      --set-string image.tag=${version} \
      --set-string pullPolicy=Always \
      --charts ./helmcharts
  dependencies:
  - docker_publish_production
  tags:
  - docker
  only:
  - /^release\/.*/

gh_pages:
  stage: docs
  image:
    name: squidfunk/mkdocs-material:2.7.2
    entrypoint: [ "/bin/sh", "-c" ]
  script: |
    git remote set-url origin https://${GITLAB_USER}:${GITLAB_TOKEN}@git.sc.uni-leipzig.de/ubl/bdd_dev/webmasterei/dacap.git
    mkdocs gh-deploy
  tags:
  - docker
  except:
  - branches
  only:
  - /^release\/.*/

github_mirror:
  stage: mirror
  image:
    name: alpine/git
    entrypoint: [ "/bin/sh", "-c" ]
  variables:
    GIT_STRATEGY: clone
    GIT_CHECKOUT: "false"
  script: |
    cd /tmp
    git clone --mirror ${CI_REPOSITORY_URL} project
    cd project
    git remote add github https://${GITHUB_USER}:${GITHUB_TOKEN}@github.com/ubleipzig/dacap.git
    git push --mirror github
  tags:
  - docker
  only:
  - master
  - /^release\/.*/
