<commit_before>#!/bin/bash
set -ex

if [ -z "$STATE_BUCKET" -o -z "$BLUE_STATE_KEY" -o -z "$GREEN_STATE_KEY" -o -z "$APP_DIR" -o -z "$REPO_NAME" ]
then
    echo 'Missing parameters'
    exit 0
fi

COMMIT=${TRAVIS_COMMIT:-'unknown'}
SHORT_COMMIT=${COMMIT:0:7}

ACCOUNT=$(aws sts get-caller-identity --query 'Account' --output text)

echo "Building image"
docker build -t $REPO_NAME:$SHORT_COMMIT $APP_DIR

echo "Tagging image"
docker tag $REPO_NAME:$SHORT_COMMIT $REPO_NAME:latest
docker tag $REPO_NAME:$SHORT_COMMIT ${ACCOUNT}.dkr.ecr.${AWS_REGION}.amazonaws.com/${REPO_NAME}:${SHORT_COMMIT}
docker tag $REPO_NAME:$SHORT_COMMIT ${ACCOUNT}.dkr.ecr.${AWS_REGION}.amazonaws.com/${REPO_NAME}:latest

docker images

echo "Logging to ECR"
$(aws ecr --region $AWS_REGION get-login)

echo "Pushing image"
docker push ${ACCOUNT}.dkr.ecr.${AWS_REGION}.amazonaws.com/${REPO_NAME}:${SHORT_COMMIT}
docker push ${ACCOUNT}.dkr.ecr.${AWS_REGION}.amazonaws.com/${REPO_NAME}:latest

echo "Getting current environment"
HEADER_COLOR=$(curl -si http://$WEBSITE  | grep X-Color)
if [ $? -gt 0 ]
then
    echo "Unable to detect site"
    exit 0
fi
COLOR=${HEADER_COLOR#X-Color: }

echo "Current deployement is $COLOR"
if [[ $COLOR == "blue" ]]
then
    NEXT='green'
    STATE_KEY=$GREEN_STATE_KEY
else
    NEXT='blue'
    STATE_KEY=$BLUE_STATE_KEY
fi

echo "Updating $NEXT stack"
pushd ${TRAVIS_BUILD_DIR}/${TERRAFORM_DIR}/${NEXT}
rm -rf .terraform
export AWS_DEFAULT_REGION=$AWS_REGION
terraform remote config -backend=s3 -backend-config="bucket=$STATE_BUCKET" -backend-config="key=$STATE_KEY"
TF_VAR_voteapp_tag=${SHORT_COMMIT} terraform plan
TF_VAR_voteapp_tag=${SHORT_COMMIT} terraform apply
popd
<commit_msg>Simpler test<commit_after>#!/bin/bash
set -ex

if [ -z "$STATE_BUCKET" -o -z "$BLUE_STATE_KEY" -o -z "$GREEN_STATE_KEY" -o -z "$APP_DIR" -o -z "$REPO_NAME" ]
then
    echo 'Missing parameters'
    exit 0
fi

COMMIT=${TRAVIS_COMMIT:-'unknown'}
SHORT_COMMIT=${COMMIT:0:7}

ACCOUNT=$(aws sts get-caller-identity --query 'Account' --output text)

echo "Building image"
docker build -t $REPO_NAME:$SHORT_COMMIT $APP_DIR

echo "Tagging image"
docker tag $REPO_NAME:$SHORT_COMMIT $REPO_NAME:latest
docker tag $REPO_NAME:$SHORT_COMMIT ${ACCOUNT}.dkr.ecr.${AWS_REGION}.amazonaws.com/${REPO_NAME}:${SHORT_COMMIT}
docker tag $REPO_NAME:$SHORT_COMMIT ${ACCOUNT}.dkr.ecr.${AWS_REGION}.amazonaws.com/${REPO_NAME}:latest

docker images

echo "Logging to ECR"
$(aws ecr --region $AWS_REGION get-login)

echo "Pushing image"
docker push ${ACCOUNT}.dkr.ecr.${AWS_REGION}.amazonaws.com/${REPO_NAME}:${SHORT_COMMIT}
docker push ${ACCOUNT}.dkr.ecr.${AWS_REGION}.amazonaws.com/${REPO_NAME}:latest

echo "Getting current environment"
HEADER_COLOR=$(curl -si http://$WEBSITE  | grep X-Color)
if [ $? -gt 0 ]
then
    echo "Unable to detect site"
    exit 0
fi
COLOR=${HEADER_COLOR#X-Color: }

echo "Current deployement is $COLOR"
if [ "$COLOR" == "blue" ]
then
    NEXT='green'
    STATE_KEY=$GREEN_STATE_KEY
else
    NEXT='blue'
    STATE_KEY=$BLUE_STATE_KEY
fi

echo "Updating $NEXT stack"
pushd ${TRAVIS_BUILD_DIR}/${TERRAFORM_DIR}/${NEXT}
rm -rf .terraform
export AWS_DEFAULT_REGION=$AWS_REGION
terraform remote config -backend=s3 -backend-config="bucket=$STATE_BUCKET" -backend-config="key=$STATE_KEY"
TF_VAR_voteapp_tag=${SHORT_COMMIT} terraform plan
TF_VAR_voteapp_tag=${SHORT_COMMIT} terraform apply
popd

{'fraction_non_alphanumeric': 0.15034033737792246, 'fraction_numerical': 0.003551346552234389, 'mean_word_length': 5.627450980392157, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4110782', 'n_tokens_mistral': 1335, 'n_tokens_neox': 1308, 'n_words': 277}