From 6da86cf2b882e7e7ac211a29224e6e17984d36a0 Mon Sep 17 00:00:00 2001 From: ivan kenneth wang Date: Sat, 5 Aug 2023 15:55:50 +0800 Subject: [PATCH] Add ci-cd alerts and optimize build by using cache --- bin/discord.sh | 69 ++++++++++++++++++++++++++++++++++++++++++++ buildspec-build.yml | 10 +++---- buildspec-deploy.yml | 12 ++++---- 3 files changed, 81 insertions(+), 10 deletions(-) create mode 100755 bin/discord.sh diff --git a/bin/discord.sh b/bin/discord.sh new file mode 100755 index 0000000..0c64209 --- /dev/null +++ b/bin/discord.sh @@ -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" diff --git a/buildspec-build.yml b/buildspec-build.yml index a8cd982..7ee77fa 100644 --- a/buildspec-build.yml +++ b/buildspec-build.yml @@ -4,15 +4,13 @@ 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 @@ -20,3 +18,5 @@ phases: - docker push $REPOSITORY_URI:$ENVIRONMENT - git tag $ENVIRONMENT-$CODEBUILD_BUILD_NUMBER - git push --tags + finally: + - bin/discord.sh diff --git a/buildspec-deploy.yml b/buildspec-deploy.yml index 8364d50..faa77f1 100644 --- a/buildspec-deploy.yml +++ b/buildspec-deploy.yml @@ -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