version: 0.2

phases:
  install:
    commands:
      - echo Entered the install phase...
      - apt-get update -y
      - sudo apt-get install zip gzip tar -y
      - pip3 install --upgrade pip
      - ln -s /usr/local/bin/pip /usr/bin/pip
  pre_build:
    commands:
      - echo Entered the pre_build phase...
      - echo Current directory is $CODEBUILD_SRC_DIR
      - ls -la
      - export dirname=${PWD##*/}
      - echo Directory name $dirname
      - cd ..
      - mv $dirname $PROJECTNAME
      - ls -la
      - cd $PROJECTNAME
      - git config --global url."https://github.com/".insteadOf "git@github.com:"
      - git init
      - git remote add origin https://$GITHUBTOKEN@github.com/$GITHUBUSER/$PROJECTNAME.git
      - git fetch
      - git checkout -ft origin/$SOURCEBRANCH
      - git submodule init
      - git submodule update --recursive
      - ls -lR
      - cd ..
      - echo Upgrading AWS CLI using pip3...
      - pip install --upgrade awscli
      - echo Installing Taskcat using pip3...
      - pip install taskcat==0.9.13
      - echo Verifying Taskcat installation...
      - taskcat
      - echo Configuring aws cli...
      - aws configure set default.region us-west-2
  build:
    commands:
      - echo Entered the build phase...
      - cd $PROJECTNAME
      - taskcat test run -l
      - |
        if $(grep -Fq "CREATE_FAILED" taskcat_outputs/index.html)
        then
          echo Quickstart FAILED!
          exit 1
        else
          echo Quickstart Passed!
          exit 0
        fi
    finally:
      - ls -1 taskcat_outputs
      - ls -1 taskcat_outputs | while read LOG; do cat taskcat_outputs/${LOG}; done
      - echo Zipping and uploading report to $ARTIFACT_BUCKET S3 bucket
      - zip -r taskcat_report.zip taskcat_outputs
      - aws s3 cp taskcat_report.zip s3://$ARTIFACT_BUCKET/taskcat_reports/$CODEBUILD_BUILD_ID.zip