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:${AWS_ACCOUNT}:certificate/c7078026-d634-462b-86b4-1a6d33ac6d53
  - aws cloudformation deploy --stack-name ${STACK_NAME}
                              --template-file "aws/beta.ordbok.uib.no_stack.yaml"
                              --parameter-overrides "CertificateArn=${CERT_ARN}"
                              --no-fail-on-empty-changeset
                              --capabilities CAPABILITY_IAM
  - aws s3 rm "s3://${STACK_NAME}.aws.uib.no" --recursive
  - aws s3 sync dist/ "s3://${STACK_NAME}.aws.uib.no" --delete
  - aws s3 sync api-static/ "s3://${STACK_NAME}.aws.uib.no" --delete
  - DISTRO_ID=$(aws cloudformation describe-stack-resource
                                   --stack-name ${STACK_NAME}
                                   --logical-resource-id Distribution
                                   --query 'StackResourceDetail.PhysicalResourceId' | jq -r)
  - aws cloudfront create-invalidation --distribution-id ${DISTRO_ID} --paths "/*"
