stages:
  - build
  - deploy

build:
  image: node:latest
  stage: build
  script:
    - npm install --progress=false
    - npm run build
  artifacts:
    expire_in: 1 week
    paths:
      - dist

deploy:
  image: uibit/awscli:latest
  stage: deploy
  script:
  - rm -rf ~/.aws
  - mv  .aws ~/
  - STACK_NAME=ordbok-frontend-beta
  - CERT_ARN=arn:aws:acm:us-east-1:156160966877:certificate/c7078026-d634-462b-86b4-1a6d33ac6d53
  - if aws cloudformation   describe-stacks --stack-name ${STACK_NAME} &> /dev/null
  - then
  - echo "stack exists"
  - aws cloudformation update-stack --stack-name ${STACK_NAME} --template-body file://aws/beta.ordbok.uib.no_stack.yaml --parameters ParameterKey=CertificateArn,ParameterValue=${CERT_ARN}
  - aws cloudformation wait   stack-update-complete --stack-name ${STACK_NAME}
  - else
  - echo "creating stack"
  - aws cloudformation create-stack  --stack-name ${STACK_NAME}  --template-body file://aws/beta.ordbok.uib.no_stack.yaml --parameters ParameterKey=CertificateArn,ParameterValue=${CERT_ARN}
  - aws cloudformation wait   stack-create-complete --stack-name ${STACK_NAME}
  - fi
  - aws s3 rm "s3://${STACK_NAME}.aws.uib.no" --recursive
  - aws s3 sync dist/ "s3://${STACK_NAME}.aws.uib.no" --delete
