image: docker.doit.wisc.edu/registrar/angular-cli-builder:1.2.0

variables:
  S3_BUCKET_NAME: dev-enroll-app-frontend
  TEST_S3_BUCKET_NAME: test-enroll-app-frontend
  DEV_CFDIST_ID: E2LLRLV3LNB0E3
  TEST_CFDIST_ID: E2YFQBS58TTJRA

stages:
  - build
  - upload
  - deploy

build:
  stage: build
  script:
    - npm install
    - ng build --prod --base-href /degree-planner --deploy-url /degree-planner/

upload_artifact:
  stage: upload
  script:
    - npm install
    - ng build --prod --base-href /degree-planner --deploy-url /degree-planner/
    - VERSION=$(node -p -e "require('./package.json').version")
    - tar czf ./course-search-enroll-fe-$VERSION.tar.gz -C dist/course-search-enroll-fe ./
    - aws s3 cp ./course-search-enroll-fe-$VERSION.tar.gz s3://enroll-app-front-end-builds/course-search-enroll-fe-$VERSION.tar.gz
  only:
    - master@adi-ia/course-search-enroll-fe

deploy:
  stage: deploy
  script:
    - VERSION=$(node -p -e "require('./package.json').version")
    - aws s3 cp s3://enroll-app-front-end-builds/course-search-enroll-fe-$VERSION.tar.gz ./course-search-enroll-fe-$VERSION.tar.gz
    - mkdir build && tar xzf ./course-search-enroll-fe-$VERSION.tar.gz -C build
    - aws s3 sync --acl public-read --sse --delete build s3://$S3_BUCKET_NAME
    - aws cloudfront create-invalidation --distribution-id $DEV_CFDIST_ID --paths '/*'
  only:
    - master@adi-ia/course-search-enroll-fe

deploy_test:
  stage: deploy
  script:
    - VERSION=$(node -p -e "require('./package.json').version")
    - aws s3 cp s3://enroll-app-front-end-builds/course-search-enroll-fe-$VERSION.tar.gz ./course-search-enroll-fe-$VERSION.tar.gz
    - mkdir build && tar xzf ./course-search-enroll-fe-$VERSION.tar.gz -C build
    - aws s3 sync --acl public-read --sse --delete s3://$S3_BUCKET_NAME s3://$TEST_S3_BUCKET_NAME
    - aws cloudfront create-invalidation --distribution-id $TEST_CFDIST_ID --paths '/*'
  only:
    - master@adi-ia/course-search-enroll-fe
  when: manual
