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

variables:
  npm_config_cache: ${CI_PROJECT_DIR}/.npm
  production_repo: ubleipzig/dacap
  staging_repo: services.ub.uni-leipzig.de:11443/bdd_dev/dacap
  alpha_repo: services.ub.uni-leipzig.de:11443/bdd_dev/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_ALPHA_AUTH_CONFIG}" \
      --name ${alpha_repo} \
      --tag ${CI_COMMIT_REF_SLUG}
  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_STAGING_AUTH_CONFIG}" \
      --name ${staging_repo} \
      --tag staging
  dependencies:
  - docker_build
  tags:
    - docker
  only:
  - master

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

deploy_alpha:
  stage: deploy
  image: ubleipzig/deployer:1.1.0
  variables:
    url_schema: https://
    ingress_host: alpha.ub.uni-leipzig.de
    ingress_path: /dacap
  environment:
    name: alpha/${CI_COMMIT_REF_SLUG}
    url: https://alpha.ub.uni-leipzig.de/dacap/${CI_COMMIT_REF_SLUG}/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_SLUG} \
      --set-string environment.proxy_url=${url_schema}${ingress_host}${ingress_path}/${CI_COMMIT_REF_SLUG}/ \
      --set-string environment.http_proxy=http://proxy.uni-leipzig.de:3128 \
      --set-string ingress.path=${ingress_path}/${CI_COMMIT_REF_SLUG} \
      --set-string ingress.hosts[0]=${ingress_host} \
      --set-string image.repository=${alpha_repo} \
      --set-string image.tag=${CI_COMMIT_REF_SLUG} \
      --set-string pullPolicy=Always \
      --set-string dockerconfigjson=$(echo $(echo "${DOCKER_ALPHA_AUTH_CONFIG}"|base64)|tr -d " ") \
      --charts ./helmchart
  dependencies:
  - docker_publish_alpha
  tags:
  - docker
  only:
  - branches
  except:
  - master

undeploy_alpha:
  stage: deploy
  image: ubleipzig/deployer:1.0.0
  script: |
    deployer undeploy \
      --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_SLUG}
  tags:
  - docker
  only:
  - branches
  except:
  - master
  when: manual

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.repository=${alpha_repo} \
      --set-string image.tag=staging \
      --set-string pullPolicy=Always \
      --set-string dockerconfigjson=$(echo $(echo "${DOCKER_STAGING_AUTH_CONFIG}"|base64)|tr -d " ") \
      --charts ./helmchart

  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 ./helmchart
  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\/.*/
