Skip to content

Commit

Permalink
Merge pull request #23 from traderjoe-xyz/ivan/add-ci-cd-alerts
Browse files Browse the repository at this point in the history
Add ci-cd alerts and optimize build by using cache
  • Loading branch information
ivan-joepegs-com authored Aug 6, 2023
2 parents ad1a389 + 6da86cf commit f7b2701
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 10 deletions.
69 changes: 69 additions & 0 deletions bin/discord.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/bin/bash

# Start: Remove this if you want successful alerts
if [[ $CODEBUILD_BUILD_SUCCEEDING == "1" ]]; then
exit 0
fi
# End: Remove this if you want successful alerts

ACCOUNT_ID=$(echo "$CODEBUILD_BUILD_ARN" | cut -d ':' -f 5)
PROJECT_NAME=$(echo "$CODEBUILD_BUILD_ID" | cut -d ':' -f 1)

ACTION=$(echo "$PROJECT_NAME" | rev | cut -d '-' -f 1 | rev)

RESULT=succeeded
COLOR=3669879
if [[ $CODEBUILD_BUILD_SUCCEEDING == "0" ]]; then
RESULT=failed
COLOR=16712294
fi

CODEBUILD_URL="https://${AWS_REGION}.console.aws.amazon.com/codesuite/codebuild/${ACCOUNT_ID}/projects/${PROJECT_NAME}/build/$CODEBUILD_BUILD_ID/?region=${AWS_REGION}"

CODEBUILD_START_TIME_S=$((CODEBUILD_START_TIME / 1000))
START_TIME=$(date -d "@${CODEBUILD_START_TIME_S}" +"%b %d, %Y %I:%M %p %z")

DATA=$(cat << EOF
{
"username": "[${ENVIRONMENT^^}] ${ACTION^} ${RESULT}",
"embeds": [
{
"title": "Build Logs",
"url": "${CODEBUILD_URL}",
"color": $COLOR,
"fields": [
{
"name": "Project",
"value": "${PROJECT_NAME}"
},
{
"name": "Build Number",
"value": "${CODEBUILD_BUILD_NUMBER}"
},
{
"name": "Triggered By",
"value": "${CODEBUILD_WEBHOOK_TRIGGER}",
"inline": true
},
{
"name": "Start Time",
"value": "${START_TIME}"
},
{
"name": "Repo URL",
"value": "${CODEBUILD_SOURCE_REPO_URL}"
},
{
"name": "Commit Hash",
"value": "${CODEBUILD_RESOLVED_SOURCE_VERSION}"
}
]
}
]
}
EOF
)

curl --location $DISCORD_URL \
--header 'Content-Type: application/json' \
--data "$DATA"
10 changes: 5 additions & 5 deletions buildspec-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ env:
git-credential-helper: yes

phases:
pre_build:
build:
on-failure: ABORT
commands:
- REPOSITORY_URI=194943407731.dkr.ecr.eu-west-1.amazonaws.com/joeapi
- docker login --username AWS --password $(aws ecr get-login-password --region eu-west-1) $REPOSITORY_URI
build:
on-failure: ABORT
commands:
- docker build -t $REPOSITORY_URI:$CODEBUILD_RESOLVED_SOURCE_VERSION .
- docker pull $REPOSITORY_URI:$ENVIRONMENT || true
- docker build --cache-from $REPOSITORY_URI:$ENVIRONMENT -t $REPOSITORY_URI:$CODEBUILD_RESOLVED_SOURCE_VERSION .
- docker tag $REPOSITORY_URI:$CODEBUILD_RESOLVED_SOURCE_VERSION $REPOSITORY_URI:$ENVIRONMENT-$CODEBUILD_BUILD_NUMBER
- docker tag $REPOSITORY_URI:$CODEBUILD_RESOLVED_SOURCE_VERSION $REPOSITORY_URI:$ENVIRONMENT
- docker push $REPOSITORY_URI:$CODEBUILD_RESOLVED_SOURCE_VERSION
- docker push $REPOSITORY_URI:$ENVIRONMENT-$CODEBUILD_BUILD_NUMBER
- docker push $REPOSITORY_URI:$ENVIRONMENT
- git tag $ENVIRONMENT-$CODEBUILD_BUILD_NUMBER
- git push --tags
finally:
- bin/discord.sh
12 changes: 7 additions & 5 deletions buildspec-deploy.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
version: 0.2

phases:
pre_build:
build:
on-failure: ABORT
commands:
- REPOSITORY_URI=194943407731.dkr.ecr.eu-west-1.amazonaws.com/joeapi
- IMAGE_TAG=${IMAGE_TAG:-$(echo $CODEBUILD_WEBHOOK_TRIGGER | awk -F/ {'print $2'})}
- aws eks update-kubeconfig --name $ENVIRONMENT-trader-joe-eks
build:
on-failure: ABORT
commands:
- kubectl set image -n $ENVIRONMENT deployment/joeapi joeapi=$REPOSITORY_URI:$IMAGE_TAG
- |
for d in $(kubectl get deploy -n $ENVIRONMENT -l app=joepai --no-headers -o custom-columns=_:metadata.name); do
kubectl set image -n $ENVIRONMENT deployment/$d $d=$REPOSITORY_URI:$IMAGE_TAG
done
finally:
- bin/discord.sh

0 comments on commit f7b2701

Please sign in to comment.