-
Notifications
You must be signed in to change notification settings - Fork 0
CICD
-
When code is pushed to the branches starting with 'release/', upload all the source code to S3 via github actions.
-
When a file named 'source.zip' is created in a specific bucket, EventBridge is triggered, which triggers CodePipeline. CodePipeline will trigger CodeBuild and CodeDeploy sequentially.
-
CodeBuild opens the file named 'source.zip' then makes a Docker image.
-
Uploads it to ECR.
-
When CodeBuild ends, CodePipeline starts to run CodeDeploy, which deploys the Docker image to ECS. It uses an artifact created by CodeBuild, which contains the information about the image to be deployed.
When the source code is pushed to the branches starting with 'release/', for example 'release/dev', 'release/live', it triggers GitHub Actions to upload it to S3. Let's see what it looks like. (Click to see full source code)
- name: Create source.zip
run: |
sudo apt-get update && sudo apt-get install -y zip
zip -r source.zip . -x ".git/*"
- name: Upload to S3
run: |
BUCKET_NAME=${{ vars.GIT_PROJECT_NAME }}
aws s3 cp source.zip s3://codepipeline-mimir-cicd/${BUCKET_NAME}/source.zipWhen a file named 'source.zip' is created in a specific bucket used only for CI/CD. It triggers CodePipeline.
How can S3 trigger CodePipeline? The secret is EventBridge.
EventBridge has Source and Target.
In this EventBridge, the Source is S3 and the Target is CodePipeline.
When there is an event in S3 that can be processed by EventBridge, it is conveyed to EventBridge.
Then EventBridge checks whether the event matches a rule. If it matches, EventBridge triggers a target.
Let's see how the rule look.
{
"source": ["aws.s3"],
"detail-type": ["AWS API Call via CloudTrail"],
"detail": {
"eventSource": ["s3.amazonaws.com"],
"eventName": ["CopyObject", "CompleteMultipartUpload", "PutObject"],
"requestParameters": {
"bucketName": ["codepipeline-mimir-cicd"],
"key": ["AuthServer/source.zip"]
}
}
}It says it'll trigger the target when "CopyObject", "CompleteMultipartUpload", "PutObject" events occur in codepipeline-mimir-cicd bucket at the path AuthServer/source.zip.
Unzip the source.zip file, build the source code, make a Docker image, and push it to ECR.
We'll deploy the image using CodeDeploy.
Because there is no direct connection between CodeBuild and CodeDeploy, CodeDeploy cannot determine which image was just built by CodeBuild when it starts deploying.
To ensure CodeDeploy knows which image to deploy. write the information about the image to a file named imagedefinitions.json. (See full build code)
# Commands omitted for brevity
post_build:
commands:
- echo Writing imagedefinitions.json...
- printf '[{"name":"%s","imageUri":"%s"}]' "${GIT_PROJECT_NAME}" "${DOCKER_URL}" > imagedefinitions.json
artifacts:
files:
- imagedefinitions.jsonWhen CodeBuild has successfully finished, CodePipeline triggers CodeDeploy. First, it reads the imagedefinitions.json file from the S3 artifact path to select the proper Docker image to deploy. Then it pulls the image from ECR and starts the ECS deployment.