image: node:14

stages:
  - build
  - deploy_staging
  - deploy_master

cache:
  untracked: true
  key: '$CI_PROJECT_ID'
  paths:
    - node_modules/
    - _site/vendor/
    - .bundled/
    - .yarn

build:
  stage: build
  script:
    - ls -l
    - yarn config set cache-folder .yarn
    - yarn install
    - yarn generate
    - echo "BUILD SUCCESSFULLY"
  artifacts:
    paths:
      - dist/
    expire_in: 20 mins
  # environment:
  #   name: development
  # only:
  #   - develop

deploy_staging:
  stage: deploy_staging
  image: python:3.5
  dependencies:
    - build
  script:
    - export AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID
    - export AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY
    - export S3_BUCKET_NAME=$S3_BUCKET_NAME_STAGING
    - export DISTRIBUTION_ID=$DISTRIBUTION_ID
    - pip install awscli --upgrade --user
    - export PATH=~/.local/bin:$PATH
    - aws s3 sync --acl public-read --delete dist $S3_BUCKET_NAME_STAGING
    #- aws cloudfront create-invalidation --distribution-id $DISTRIBUTION_ID --paths '/*'
    - echo "DEPLOYED SUCCESSFULLY TO STAGING"
  environment:
    name: development
  only:
    - develop

deploy_master:
  stage: deploy_master
  image: python:3.5
  dependencies:
    - build
  script:
    - export AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID
    - export AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY
    - export S3_BUCKET_NAME=$S3_BUCKET_NAME_PRODUCTION
    - export DISTRIBUTION_ID=$DISTRIBUTION_ID
    - pip install awscli --upgrade --user
    - export PATH=~/.local/bin:$PATH
    - aws s3 sync --acl public-read --cache-control "max-age=2592000, public" --delete dist $S3_BUCKET_NAME_PRODUCTION
    - aws cloudfront create-invalidation --distribution-id $DISTRIBUTION_ID --paths '/*'
    - echo "DEPLOYED SUCCESSFULLY TO PRODUCTION"
  environment:
    name: production
  only:
    - master
