stages:
  - build
  - deploy

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

build-prod:
  image: node:latest
  tags:
    - spraaksamlingene_01
  stage: build
  only:
    - /^release_.*$/
  script:
  - echo "VUE_APP_RELEASE=$CI_COMMIT_REF_NAME" >> .env.production
  - npm install --progress=false
  - npm run build -- --mode production
  artifacts:
    expire_in: 1 week
    paths:
    - dist

deploy-dev:
  image: uibit/awscli:latest
  tags:
    - spraaksamlingene_01
  stage: deploy
  only:
    - master
  script:
  - rm -rf ~/.aws
  - mv  .aws ~/
  - export AWS_ACCESS_KEY_ID=$AWS_DEV_ACCESS_KEY_ID
  - export AWS_SECRET_ACCESS_KEY=$AWS_DEV_SECRET_ACCESS_KEY
  - STACK_NAME=ordbok-frontend-dev
  - CERT_ARN=arn:aws:acm:us-east-1:${AWS_DEV_ACCOUNT}:certificate/15c0111e-b2f7-4a24-8c94-b7034a900d55
  - aws cloudformation deploy --stack-name ${STACK_NAME}
                              --template-file "aws/beta.ordbok.uib.no_stack.yaml"
                              --parameter-overrides "CertificateArn=${CERT_ARN}"
                                                    "AltDomainName=dev.ordbok.uib.no"
                                                    "DomainName=beta.ordbok-dev.aws.uib.no"
                                                    "ZoneDomainName=ordbok-dev.aws.uib.no"
                                                    "PathRewriteLambdaARN=arn:aws:lambda:us-east-1:${AWS_DEV_ACCOUNT}:function:rewrite_request:5"
                                                    "OrdbokCacheOriginDomain=8mwl23g34a.execute-api.eu-west-1.amazonaws.com"
                                                    "OrdbokCacheStage=DEV"
                              --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"
  - 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 "/*"

deploy-prod:
  image: uibit/awscli:latest
  tags:
    - spraaksamlingene_01
  stage: deploy
  only:
    - /^release_.*$/
  script:
    - rm -rf ~/.aws
    - mv  .aws ~/
    - export AWS_ACCESS_KEY_ID=$AWS_PROD_ACCESS_KEY_ID
    - export AWS_SECRET_ACCESS_KEY=$AWS_PROD_SECRET_ACCESS_KEY
    - STACK_NAME=ordbok-frontend-beta
    - CERT_ARN=arn:aws:acm:us-east-1:${AWS_PROD_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}"
                                                  "PathRewriteLambdaARN=arn:aws:lambda:us-east-1:${AWS_PROD_ACCOUNT}:function:path_rewrite:13"
                                                  "OrdbokCacheOriginDomain=n5speug9cl.execute-api.eu-west-1.amazonaws.com"
                                                  "OrdbokCacheStage=PROD"
                            --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"
    - 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 "/*"
