-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: DBTP-1507 Setup codepipeline notifications #314
Changes from all commits
2fbcbc1
d74a458
31f6ee1
065f434
7c43eb5
8f0e3a4
ad16c0f
1a1f80c
0b84d7c
04667f7
6479d1f
5f1a862
cd25d02
5c7cddf
069b091
3e61b5d
26d8ebf
2631bef
9e0bec7
58767b9
6aad173
8e6a57b
3503dec
28ce523
e877faa
740442a
af54f02
2f40fb0
6323922
5b28002
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,16 +3,63 @@ version: 0.2 | |
env: | ||
variables: | ||
DEPLOY_TIMEOUT: 1800 | ||
parameter-store: | ||
SLACK_TOKEN: /codebuild/slack_oauth_token | ||
|
||
phases: | ||
install: | ||
commands: | ||
- echo "Starting deployment script for ${SERVICE} service" | ||
- pip install yq --quiet | ||
- echo "Installing platform-helper" | ||
- pip install dbt-platform-helper --quiet | ||
- platform-helper --version | ||
- echo "Installing regclient" | ||
- curl -s -L https://github.com/regclient/regclient/releases/latest/download/regctl-linux-amd64 > /usr/local/bin/regctl | ||
- chmod +x /usr/local/bin/regctl | ||
|
||
build: | ||
commands: | ||
- set -e | ||
|
||
# Extract timestamp from image config and check if it exists | ||
- aws ecr get-login-password --region ${AWS_REGION} | docker login --username AWS --password-stdin ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com | ||
|
||
# construct Slack message env vars | ||
- SLACK_REF=$(regctl image config "${REPOSITORY_URL}:${IMAGE_TAG}" | jq -r '.config.Labels."uk.gov.trade.digital.build.timestamp"') | ||
- | | ||
if [ "${SLACK_REF}" = "null" ] || [ -z "${SLACK_REF}" ]; then | ||
echo "Image contains no timestamp label" | ||
exit 1 | ||
fi | ||
echo "Found image timestamp ${SLACK_REF}" | ||
|
||
- UPPERCASE_SERVICE=$(echo "${SERVICE}" | tr '[:lower:]' '[:upper:]') | ||
- UPPERCASE_TAG=$(echo "${IMAGE_TAG}" | tr '[:lower:]' '[:upper:]') | ||
|
||
# Extract the pipeline name from CODEBUILD_INITIATOR default env var | ||
- | | ||
if [[ "${CODEBUILD_INITIATOR}" == codepipeline/* ]]; then | ||
PIPELINE_NAME=$(echo "${CODEBUILD_INITIATOR}" | cut -d'/' -f2) | ||
echo "Pipeline name is ${PIPELINE_NAME}" | ||
else | ||
echo "ERROR: Build not triggered by CodePipeline." | ||
exit 1 | ||
fi | ||
|
||
# Construct the pipeline execution URL | ||
- PIPELINE_EXECUTION_URL="https://${AWS_REGION}.console.aws.amazon.com/codesuite/codepipeline/pipelines/${PIPELINE_NAME}/executions/${PIPELINE_EXECUTION_ID}" | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe just remove some of these random empty lines in this section. |
||
- echo "Pipeline execution URL ${PIPELINE_EXECUTION_URL}" | ||
|
||
- BUILD_ID_PREFIX=$(echo $CODEBUILD_BUILD_ID | cut -d':' -f1) | ||
- echo "BUILD_ID_PREFIX - ${BUILD_ID_PREFIX}" | ||
|
||
- | | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be just a single line item. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried adding as a single line comment but there was a syntax error because of the use of the ":" which only resolved when using multiline There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI - you should be able to just surround the line in single quotes. It's because it thinks it's a map rather than a string. Surrounding in single quotes will make the YAML parser treat it as a string. But happy for you to leave it as a multi-line quote in this case as it's probably neater. |
||
MESSAGE=":rocket: STARTED - Deployment of ${UPPERCASE_SERVICE} service - Tag: ${UPPERCASE_TAG} - <https://${AWS_REGION}.console.aws.amazon.com/codesuite/codebuild/${AWS_ACCOUNT_ID}/projects/${BUILD_ID_PREFIX}/build/${CODEBUILD_BUILD_ID}/?region=${AWS_REGION}|Build log> | <${PIPELINE_EXECUTION_URL}|Pipeline execution url>" | ||
|
||
- platform-helper notify add-comment "${SLACK_CHANNEL_ID}" "${SLACK_TOKEN}" "${SLACK_REF}" "${MESSAGE}" | ||
|
||
# Check if the specified image tag exists | ||
- | | ||
if ! aws ecr describe-images --repository-name "${REPOSITORY_NAME}" --image-ids "imageTag=${IMAGE_TAG}" > /dev/null 2>&1; then | ||
|
@@ -52,7 +99,7 @@ phases: | |
- cd "${CODEBUILD_SRC_DIR}" | ||
|
||
# If count is a range, get the first value otherwise get count | ||
- | | ||
- | | ||
if [ $(yq '.count | type == "object"' copilot/web/manifest.yml) == "true" ]; then | ||
count=$(yq '.count.range' copilot/web/manifest.yml | tr -d '"' | cut -d '-' -f1) | ||
else | ||
|
@@ -85,10 +132,10 @@ phases: | |
sleep 10 | ||
now=$( date +%s ) | ||
elapsed=$(( now-start )) | ||
|
||
deploy_status=$(aws ecs list-service-deployments --cluster "${cluster}" --service "${service_name}" --created-at "after=${start}" | jq -r '.serviceDeployments[0].status') | ||
echo "Deployment status after ${elapsed} seconds: ${deploy_status}" | ||
|
||
if [[ ${elapsed} -gt ${DEPLOY_TIMEOUT} ]]; then | ||
echo "Error: deployment not completed in ${DEPLOY_TIMEOUT} seconds" | ||
exit 1 | ||
|
@@ -97,7 +144,34 @@ phases: | |
|
||
# Check deployment success | ||
- | | ||
if [ "${deploy_status}" != "SUCCESSFUL" ]; then | ||
echo "Error: deployment did not succeed" | ||
case "${deploy_status}" in | ||
SUCCESSFUL) | ||
STATUS_EMOJI=":large_green_circle:" | ||
STATUS_TEXT="COMPLETE" | ||
;; | ||
ROLLBACK_SUCCESSFUL) | ||
STATUS_EMOJI=":red_circle:" | ||
STATUS_TEXT="FAILED" | ||
;; | ||
ROLLBACK_FAILED) | ||
STATUS_EMOJI=":red_circle::warning:" | ||
STATUS_TEXT="FAILED" | ||
;; | ||
*) | ||
STATUS_EMOJI=":large_blue_circle:" | ||
STATUS_TEXT="STOP_REQUESTED/STOPPED/PENDING/IN_PROGRESS" | ||
;; | ||
esac | ||
|
||
- echo "Status emoji set to ${STATUS_EMOJI}" | ||
|
||
- MESSAGE="${STATUS_EMOJI} ${STATUS_TEXT} - Deployment of ${UPPERCASE_TAG} to ${UPPERCASE_SERVICE} service - ${deploy_status} - <${PIPELINE_EXECUTION_URL}|Pipeline execution url>" | ||
|
||
- platform-helper notify add-comment "${SLACK_CHANNEL_ID}" "${SLACK_TOKEN}" "${SLACK_REF}" "${MESSAGE}" | ||
|
||
# Exit with an error code if deployment did not succeed | ||
- | | ||
if [ "${deploy_status}" != "SUCCESSFUL" ]; then | ||
echo "Error: deployment status is ${deploy_status}" | ||
exit 1 | ||
fi |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think in general, these multiline commands should be reserved for blocks, such as if/then/else clauses, function definitions while loops etc. So I'd put line 30 in it's own single line item.