# workflow
stages:
 - build
 - test
 - integration
 - acceptance
 - release
 - deployment

# default image for jobs
default:
  image: node:11

# cache modules between jobs
cache:
  key: ${CI_COMMIT_REF_SLUG}
  paths:
    - .npm/

## BUILD ##

build:
  stage: build
  before_script:
    - npm ci --cache .npm --prefer-offline --only=production
  script:
    - cp config.sample.json config.json
    - npm run build
  artifacts:
    when: on_success
    expire_in: 1 day
    paths:
      - dist/
  tags:
    - test

## TESTING ##

test:e2e:
  stage: test
  image: cypress/included:4.5.0
  services:
    - name: ${CI_REGISTRY_IMAGE}/server:0.1
  before_script:
    # install missing dependencies
    - npm install -g sirv-cli
     # making sure the process is orphan
    - sirv dist --port 3000 > /dev/null 2>&1 &
  script:
    - cypress run -e CYPRESS_baseUrl=http://localhost:3000
  tags:
    - test

## VALIDATION ##

test1:
  stage: integration
  environment:
    name: test1
    url: https://test1.startinblox.com
  before_script:
    - npm ci --cache .npm --prefer-offline --only=production
  script:
    - echo "$APP_CONFIG_TEST1" > config.json
    - echo "$SSH_DEPLOY_KEY" | tr -d '\r' > gitlab.key && chmod 600 gitlab.key
    - npm run build
    - scp -i gitlab.key -o StrictHostKeyChecking=no -r dist/* test1@astral.startinblox.com:~/front/
  only:
    - /^feature\/.*/
  when: manual
  tags:
    - deploy

test2:
  stage: integration
  environment:
    name: test2
    url: https://test2.startinblox.com
  before_script:
    - npm ci --cache .npm --prefer-offline --only=production
  script:
    - echo "$APP_CONFIG_TEST2" > config.json
    - echo "$SSH_DEPLOY_KEY" | tr -d '\r' > gitlab.key && chmod 600 gitlab.key
    - npm run build
    - scp -i gitlab.key -o StrictHostKeyChecking=no -r dist/* test2@astral.startinblox.com:~/front/
  only:
    - /^feature\/.*/
  when: manual
  tags:
    - deploy

test3:
  stage: integration
  environment:
    name: test3
    url: https://test3.startinblox.com
  before_script:
    - npm ci --cache .npm --prefer-offline --only=production
  script:
    - echo "$APP_CONFIG_TEST3" > config.json
    - echo "$SSH_DEPLOY_KEY" | tr -d '\r' > gitlab.key && chmod 600 gitlab.key
    - npm run build
    - scp -i gitlab.key -o StrictHostKeyChecking=no -r dist/* test3@astral.startinblox.com:~/front/
  only:
    - /^feature\/.*/
  when: manual
  tags:
    - deploy

stg1:
  stage: acceptance
  environment:
    name: stg1
    url: https://stg1.startinblox.com
  before_script:
    - npm ci --cache .npm --prefer-offline --only=production
  script:
    - echo "$APP_CONFIG_STG1" > config.json
    - echo "$SSH_DEPLOY_KEY" | tr -d '\r' > gitlab.key && chmod 600 gitlab.key
    - npm run build
    - scp -i gitlab.key -o StrictHostKeyChecking=no -r dist/* stg1@astral.startinblox.com:~/front/
  only:
    - /^release\/.*/
  when: manual
  tags:
    - deploy

stg2:
  stage: acceptance
  environment:
    name: stg2
    url: https://stg2.startinblox.com
  before_script:
    - npm ci --cache .npm --prefer-offline --only=production
  script:
    - echo "$APP_CONFIG_STG2" > config.json
    - echo "$SSH_DEPLOY_KEY" | tr -d '\r' > gitlab.key && chmod 600 gitlab.key
    - npm run build
    - scp -i gitlab.key -o StrictHostKeyChecking=no -r dist/* stg2@astral.startinblox.com:~/front/
  only:
    - /^release\/.*/
  when: manual
  tags:
    - deploy

## RELEASE TAGGING ##

publish:
  stage: release
  script:
    - npm install -g semantic-release@v17 @semantic-release/gitlab
    - semantic-release
  only:
    - master
  tags:
    - deploy

## LIVE DEPLOYMENTS ##

community:
  stage: deployment
  environment:
    name: community
    url: https://community.startinblox.com
  before_script:
    - npm ci --cache .npm --prefer-offline --only=production
  script:
    - echo "$APP_CONFIG_COMMUNITY" > config.json
    - echo "$SSH_DEPLOY_KEY" | tr -d '\r' > gitlab.key && chmod 600 gitlab.key
    - npm run build
    - scp -i gitlab.key -o StrictHostKeyChecking=no -r dist/* community@astral.startinblox.com:~/front/
  only:
    - master
  when: manual
  tags:
    - deploy

nantes:
  stage: deployment
  environment:
    name: nantes
    url: https://app.nantes.happy-dev.fr
  before_script:
    - npm ci --cache .npm --prefer-offline --only=production
  script:
    - echo "$APP_CONFIG_NANTESHD" > config.json
    - echo "$SSH_DEPLOY_KEY" | tr -d '\r' > gitlab.key && chmod 600 gitlab.key
    - npm run build
    - scp -i gitlab.key -o StrictHostKeyChecking=no -r dist/* nantes@ssh-nantes.happy-dev.fr:~/sib/www/
  only:
    - master
  when: manual
  tags:
    - deploy

paris:
  stage: deployment
  environment:
    name: paris
    url: https://app.paris.happy-dev.fr
  before_script:
    - npm ci --cache .npm --prefer-offline --only=production
  script:
    - echo "$APP_CONFIG_PARIS" > config.json
    - echo "$SSH_DEPLOY_KEY" | tr -d '\r' > gitlab.key && chmod 600 gitlab.key
    - npm run build
    - scp -i gitlab.key -o StrictHostKeyChecking=no -r dist/* paris@ssh-paris.happy-dev.fr:~/sib/www/
  only:
    - master
  when: manual
  tags:
    - deploy

smarttoulouse:
  stage: deployment
  environment:
    name: smarttoulouse
    url: https://smart-toulouse.happy-dev.fr
  before_script:
    - npm ci --cache .npm --prefer-offline --only=production
  script:
    - echo "$APP_CONFIG_TOULOUSE" > config.json
    - echo "$SSH_DEPLOY_KEY" | tr -d '\r' > gitlab.key && chmod 600 gitlab.key
    - npm run build
    - scp -i gitlab.key -o StrictHostKeyChecking=no -r dist/* smart-toulouse@ssh-smart-toulouse.happy-dev.fr:~/www/
  only:
    - master
  when: manual
  tags:
    - deploy

volumes:
  stage: deployment
  environment:
    name: volumes
    url: https://volumes.hubl.world
  before_script:
    - npm ci --cache .npm --prefer-offline --only=production
  script:
    - echo "$APP_CONFIG_VOLUMES" > config.json
    - echo "$SSH_DEPLOY_KEY" | tr -d '\r' > gitlab.key && chmod 600 gitlab.key
    - npm run build
    - scp -i gitlab.key -o StrictHostKeyChecking=no -r dist/* volumes-sib@astral.startinblox.com:~/front/
  only:
    - master
  when: manual
  tags:
    - deploy

communityhubl:
  stage: deployment
  environment:
    name: communityhubl
    url: https://community.hubl.world
  before_script:
    - npm ci --cache .npm --prefer-offline --only=production
  script:
    - echo "$APP_CONFIG_HUBL_COMMUNITY" > config.json
    - echo "$SSH_DEPLOY_KEY" | tr -d '\r' > gitlab.key && chmod 600 gitlab.key
    - npm run build
    - scp -i gitlab.key -o StrictHostKeyChecking=no -r dist/* community-hubl@astral.startinblox.com:~/front/
  only:
    - master
  when: manual
  tags:
    - deploy

playground:
  stage: deployment
  environment:
    name: playground
    url: https://playground.hubl.world
  before_script:
    - npm ci --cache .npm --prefer-offline --only=production
  script:
    - echo "$APP_CONFIG_PLAYGROUND" > config.json
    - echo "$SSH_DEPLOY_KEY" | tr -d '\r' > gitlab.key && chmod 600 gitlab.key
    - npm run build
    - scp -i gitlab.key -o StrictHostKeyChecking=no -r dist/* playground@astral.startinblox.com:~/front/
  only:
    - master
  when: manual
  tags:
    - deploy

casaco:
  stage: deployment
  environment:
    name: casaco
    url: https://casaco.hubl.world
  before_script:
    - npm ci --cache .npm --prefer-offline --only=production
  script:
    - echo "$APP_CONFIG_CASACO" > config.json
    - echo "$SSH_DEPLOY_KEY" | tr -d '\r' > gitlab.key && chmod 600 gitlab.key
    - npm run build
    - scp -i gitlab.key -o StrictHostKeyChecking=no -r dist/* casaco@astral.startinblox.com:~/front/
  only:
    - master
  when: manual
  tags:
    - deploy

opteos:
  stage: deployment
  environment:
    name: opteos
    url: https://opteos.hubl.world
  before_script:
    - npm ci --cache .npm --prefer-offline --only=production
  script:
    - echo "$APP_CONFIG_OPTEOS" > config.json
    - echo "$SSH_DEPLOY_KEY" | tr -d '\r' > gitlab.key && chmod 600 gitlab.key
    - npm run build
    - scp -i gitlab.key -o StrictHostKeyChecking=no -r dist/* opteos@astral.startinblox.com:~/front/
  only:
    - master
  when: manual
  tags:
    - deploy

fnk:
  stage: deployment
  environment:
    name: fnk
    url: https://fnk.hubl.world
  before_script:
    - npm ci --cache .npm --prefer-offline --only=production
  script:
    - echo "$APP_CONFIG_FNK" > config.json
    - echo "$SSH_DEPLOY_KEY" | tr -d '\r' > gitlab.key && chmod 600 gitlab.key
    - npm run build
    - scp -i gitlab.key -o StrictHostKeyChecking=no -r dist/* fnk@astral.startinblox.com:~/front/
  only:
    - master
  when: manual
  tags:
    - deploy

labriqueterie:
  stage: deployment
  environment:
    name: labriqueterie
    url: https://labriqueterie.hubl.world
  before_script:
    - npm ci --cache .npm --prefer-offline --only=production
  script:
    - echo "$APP_CONFIG_LABRIQUETERIE" > config.json
    - echo "$SSH_DEPLOY_KEY" | tr -d '\r' > gitlab.key && chmod 600 gitlab.key
    - npm run build
    - scp -i gitlab.key -o StrictHostKeyChecking=no -r dist/* labriqueterie@astral.startinblox.com:~/front/
  only:
    - master
  when: manual
  tags:
    - deploy

facttic:
  stage: deployment
  environment:
    name: facttic
    url: https://facttic.hubl.world
  before_script:
    - npm ci --cache .npm --prefer-offline --only=production
  script:
    - echo "$APP_CONFIG_FACTTIC" > config.json
    - echo "$SSH_DEPLOY_KEY" | tr -d '\r' > gitlab.key && chmod 600 gitlab.key
    - npm run build
    - scp -i gitlab.key -o StrictHostKeyChecking=no -r dist/* facttic@astral.startinblox.com:~/front/
  only:
    - master
  when: manual
  tags:
    - deploy

cotech:
  stage: deployment
  environment:
    name: cotech
    url: https://hubl.coops.tech
  before_script:
    - npm ci --cache .npm --prefer-offline --only=production
  script:
    - echo "$APP_CONFIG_COTECH" > config.json
    - echo "$SSH_DEPLOY_KEY" | tr -d '\r' > gitlab.key && chmod 600 gitlab.key
    - npm run build
    - scp -i gitlab.key -o StrictHostKeyChecking=no -r dist/* cotech@astral.startinblox.com:~/front/
  only:
    - master
  when: manual
  tags:
    - deploy

lelaptop:
  stage: deployment
  environment:
    name: lelaptop
    url: https://lelaptop.hubl.world
  before_script:
    - npm ci --cache .npm --prefer-offline --only=production
  script:
    - echo "$APP_CONFIG_LELAPTOP" > config.json
    - echo "$SSH_DEPLOY_KEY" | tr -d '\r' > gitlab.key && chmod 600 gitlab.key
    - npm run build
    - scp -i gitlab.key -o StrictHostKeyChecking=no -r dist/* lelaptop@astral.startinblox.com:~/front/
  only:
    - master
  when: manual
  tags:
    - deploy

virtualassembly:
  stage: deployment
  environment:
    name: virtualassembly
    url: https://virtual-assembly.hubl.world
  before_script:
    - npm ci --cache .npm --prefer-offline --only=production
  script:
    - echo "$APP_CONFIG_VIRTUAL_ASS" > config.json
    - echo "$SSH_DEPLOY_KEY" | tr -d '\r' > gitlab.key && chmod 600 gitlab.key
    - npm run build
    - scp -i gitlab.key -o StrictHostKeyChecking=no -r dist/* virtual-assembly@astral.startinblox.com:~/front/
  only:
    - master
  when: manual
  tags:
    - deploy

ouisharequebec:
  stage: deployment
  environment:
    name: ouisharequebec
    url: https://ouishare-quebec.hubl.world
  before_script:
    - npm ci --cache .npm --prefer-offline --only=production
  script:
    - echo "$APP_CONFIG_OUISHARE" > config.json
    - echo "$SSH_DEPLOY_KEY" | tr -d '\r' > gitlab.key && chmod 600 gitlab.key
    - npm run build
    - scp -i gitlab.key -o StrictHostKeyChecking=no -r dist/* ouishare-quebec@astral.startinblox.com:~/front/
  only:
    - master
  when: manual
  tags:
    - deploy

nouvelleaquitaine:
  stage: deployment
  environment:
    name: nouvelleaquitaine
    url: https://app.nouvelle-aquitaine.happy-dev.fr
  before_script:
    - npm ci --cache .npm --prefer-offline --only=production
  script:
    - echo "$APP_CONFIG_NVLE_AQ" > config.json
    - echo "$SSH_DEPLOY_KEY" | tr -d '\r' > gitlab.key && chmod 600 gitlab.key
    - npm run build
    - scp -i gitlab.key -o StrictHostKeyChecking=no -r dist/* nouvelle-aquitaine@ssh-nouvelle-aquitaine.happy-dev.fr:~/front/
  only:
    - master
  when: manual
  tags:
    - deploy

paca:
  stage: deployment
  environment:
    name: paca
    url: https://app.paca.happy-dev.fr
  before_script:
    - npm ci --cache .npm --prefer-offline --only=production
  script:
    - echo "$APP_CONFIG_PACA" > config.json
    - echo "$SSH_DEPLOY_KEY" | tr -d '\r' > gitlab.key && chmod 600 gitlab.key
    - npm run build
    - scp -i gitlab.key -o StrictHostKeyChecking=no -r dist/* paca@ssh-paca.happy-dev.fr:~/front/
  only:
    - master
  when: manual
  tags:
    - deploy

strasbourg:
  stage: deployment
  environment:
    name: strasbourg
    url: https://app.strasbourg.happy-dev.fr
  before_script:
    - npm ci --cache .npm --prefer-offline --only=production
  script:
    - echo "$APP_CONFIG_STRASBOURG" > config.json
    - echo "$SSH_DEPLOY_KEY" | tr -d '\r' > gitlab.key && chmod 600 gitlab.key
    - npm run build
    - scp -i gitlab.key -o StrictHostKeyChecking=no -r dist/* strasbourg@ssh-strasbourg.happy-dev.fr:~/front/
  only:
    - master
  when: manual
  tags:
    - deploy

bretagne:
  stage: deployment
  environment:
    name: bretagne
    url: https://app.bretagne.happy-dev.fr
  before_script:
    - npm ci --cache .npm --prefer-offline --only=production
  script:
    - echo "$APP_CONFIG_BRETAGNE" > config.json
    - echo "$SSH_DEPLOY_KEY" | tr -d '\r' > gitlab.key && chmod 600 gitlab.key
    - npm run build
    - scp -i gitlab.key -o StrictHostKeyChecking=no -r dist/* bretagne@ssh-bretagne.happy-dev.fr:~/front/
  only:
    - master
  when: manual
  tags:
    - deploy

lemans:
  stage: deployment
  environment:
    name: lemans
    url: https://app.lemans.happy-dev.fr
  before_script:
    - npm ci --cache .npm --prefer-offline --only=production
  script:
    - echo "$APP_CONFIG_LEMANS" > config.json
    - echo "$SSH_DEPLOY_KEY" | tr -d '\r' > gitlab.key && chmod 600 gitlab.key
    - npm run build
    - scp -i gitlab.key -o StrictHostKeyChecking=no -r dist/* lemans@ssh-lemans.happy-dev.fr:~/front/
  only:
    - master
  when: manual
  tags:
    - deploy

apidays:
  stage: deployment
  environment:
    name: apidays
    url: https://apidays.hubl.world
  before_script:
    - npm ci --cache .npm --prefer-offline --only=production
  script:
    - echo "$APP_CONFIG_APIDAYS" > config.json
    - echo "$SSH_DEPLOY_KEY" | tr -d '\r' > gitlab.key && chmod 600 gitlab.key
    - npm run build
    - scp -i gitlab.key -o StrictHostKeyChecking=no -r dist/* apidays@astral.startinblox.com:~/front/
  only:
    - master
  when: manual
  tags:
    - deploy

ejp:
  stage: deployment
  environment:
    name: ejp
    url: https://ejp.hubl.world
  before_script:
    - npm ci --cache .npm --prefer-offline --only=production
  script:
    - echo "$APP_CONFIG_EJP" > config.json
    - echo "$SSH_DEPLOY_KEY" | tr -d '\r' > gitlab.key && chmod 600 gitlab.key
    - npm run build
    - scp -i gitlab.key -o StrictHostKeyChecking=no -r dist/* georgetown-uni@astral.startinblox.com:~/front/
  only:
    - master
  when: manual
  tags:
    - deploy
