Skip to content

Blue green deploy #4

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

Merged
merged 116 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
116 commits
Select commit Hold shift + click to select a range
feb2aa3
add blue green tgs
chrispsheehan Sep 27, 2024
d567cd3
mv lb to service
chrispsheehan Sep 27, 2024
7162dc0
deploy refactor
chrispsheehan Sep 27, 2024
94d2e30
destroy refactor
chrispsheehan Sep 27, 2024
0f47d4f
fix
chrispsheehan Sep 27, 2024
06f8ba2
data "aws_subnets"
chrispsheehan Sep 27, 2024
88ca09f
short name
chrispsheehan Sep 27, 2024
12211a8
rm load_balancer_port var
chrispsheehan Sep 27, 2024
b6a8be8
mv vpc endpoint to /ecr
chrispsheehan Sep 27, 2024
65493cc
fmt
chrispsheehan Sep 27, 2024
a911f42
type = "CODE_DEPLOY"
chrispsheehan Sep 27, 2024
c101c90
lb_ to output arn
chrispsheehan Sep 27, 2024
35bbb9e
add checks
chrispsheehan Sep 27, 2024
0bba2cf
test change
chrispsheehan Sep 27, 2024
2aa528e
pass in image tag
chrispsheehan Sep 27, 2024
471de68
build.yml
chrispsheehan Sep 27, 2024
9c2f1e5
rename
chrispsheehan Sep 27, 2024
01f36f6
rm trigger
chrispsheehan Sep 27, 2024
025daa4
trigger on this branch
chrispsheehan Sep 27, 2024
ba27088
fix
chrispsheehan Sep 27, 2024
d83a909
more fixes
chrispsheehan Sep 27, 2024
1553b40
pass image uri
chrispsheehan Sep 27, 2024
b5a0eff
back to tag
chrispsheehan Sep 27, 2024
4b880d6
fix
chrispsheehan Sep 27, 2024
83367bc
most_recent = true
chrispsheehan Sep 27, 2024
768c9f6
baaack to uri
chrispsheehan Sep 27, 2024
382c43b
fix
chrispsheehan Sep 27, 2024
73782e4
dummy uri
chrispsheehan Sep 27, 2024
5b763fc
nudge
chrispsheehan Sep 29, 2024
1488ab0
echo Image:$IMAGE_URI
chrispsheehan Sep 29, 2024
1e3ae3d
debug
chrispsheehan Sep 29, 2024
e4c5b70
chnge action
chrispsheehan Sep 29, 2024
1392e83
use env
chrispsheehan Sep 29, 2024
0d0af6d
test
chrispsheehan Sep 29, 2024
c21df15
setup
chrispsheehan Sep 29, 2024
54242df
fix
chrispsheehan Sep 29, 2024
2e287d7
workflow_call
chrispsheehan Sep 29, 2024
902a2f0
inputs
chrispsheehan Sep 29, 2024
1b3bfd8
project name var
chrispsheehan Sep 29, 2024
f7046e8
vars file
chrispsheehan Sep 29, 2024
a10b11d
output
chrispsheehan Sep 29, 2024
0cf3d02
pass out uri
chrispsheehan Sep 29, 2024
4e78cd2
-var-file=${{ github.workspace }}/tf/variables.tfvars
chrispsheehan Sep 29, 2024
ec687d7
rm env:
chrispsheehan Sep 29, 2024
6214678
rm repo name var
chrispsheehan Sep 29, 2024
88abbb6
pass in codedeploy_app_name var
chrispsheehan Sep 30, 2024
ea2a839
kick
chrispsheehan Sep 30, 2024
e31e9c4
output task_definition_revision
chrispsheehan Sep 30, 2024
3701b83
add deployment to destroy
chrispsheehan Sep 30, 2024
941c27a
rm deploy
chrispsheehan Sep 30, 2024
5f7a098
check existing svc
chrispsheehan Sep 30, 2024
e82ca0b
cluster check
chrispsheehan Sep 30, 2024
30052e0
fix output
chrispsheehan Sep 30, 2024
6c602a1
service and network deploy
chrispsheehan Sep 30, 2024
f1e225a
mv check to action file
chrispsheehan Sep 30, 2024
4be309a
shell: bash
chrispsheehan Sep 30, 2024
85fbcf9
pass in codedeploy_group_name var
chrispsheehan Sep 30, 2024
930b4fe
rm setup trigger
chrispsheehan Sep 30, 2024
1e034af
create deployment in ci
chrispsheehan Sep 30, 2024
6ea3cb9
ci job order fix
chrispsheehan Sep 30, 2024
2947910
fix
chrispsheehan Sep 30, 2024
928d9ef
add revision as version
chrispsheehan Sep 30, 2024
bcdf9af
$GITHUB_OUTPUT
chrispsheehan Sep 30, 2024
b45d2f7
typo
chrispsheehan Sep 30, 2024
2a75b6f
escape json
chrispsheehan Sep 30, 2024
a81a247
setup
chrispsheehan Oct 1, 2024
188f7f5
check for inactive cluster
chrispsheehan Oct 1, 2024
a241cf1
rm trigger
chrispsheehan Oct 1, 2024
b829ab0
fix
chrispsheehan Oct 1, 2024
ce0c8a2
set id
chrispsheehan Oct 1, 2024
d393aed
set bool in $GITHUB_OUTPUT
chrispsheehan Oct 1, 2024
2c8e0da
combine
chrispsheehan Oct 1, 2024
842eb78
debug
chrispsheehan Oct 1, 2024
e8630af
case service_exists
chrispsheehan Oct 1, 2024
379c1ab
refactor
chrispsheehan Oct 1, 2024
df55b8f
task_definition_arn:
chrispsheehan Oct 1, 2024
f045f76
fix code deploy app reference
chrispsheehan Oct 1, 2024
1619ebe
fixes + trigger
chrispsheehan Oct 1, 2024
cdf6495
pass into env vars
chrispsheehan Oct 1, 2024
09ef97c
casing fix?
chrispsheehan Oct 1, 2024
423c2b8
file pipe
chrispsheehan Oct 1, 2024
f8876f0
json fix
chrispsheehan Oct 1, 2024
858c5af
escapign fix
chrispsheehan Oct 1, 2024
64111ee
app_specs_bucket
chrispsheehan Oct 1, 2024
9a38f8a
yaml
chrispsheehan Oct 1, 2024
8850035
refactor
chrispsheehan Oct 1, 2024
25b1701
trigger
chrispsheehan Oct 1, 2024
787d37b
rm "
chrispsheehan Oct 1, 2024
b47fcd7
escape json
chrispsheehan Oct 1, 2024
920f4d7
app_specs_bucket access
chrispsheehan Oct 1, 2024
246c642
fix
chrispsheehan Oct 1, 2024
84acc13
trigger + monitor
chrispsheehan Oct 1, 2024
b8a9a23
fix destroy order
chrispsheehan Oct 1, 2024
0a1caf2
shorten tg name
chrispsheehan Oct 1, 2024
260fbaf
trigger deploy
chrispsheehan Oct 1, 2024
cf46fa7
use target group name
chrispsheehan Oct 1, 2024
a4695ed
repo name
chrispsheehan Oct 1, 2024
eb86c48
TF_VAR_ecr_repository_name
chrispsheehan Oct 1, 2024
ccb1c78
trigger
chrispsheehan Oct 1, 2024
0c1dc95
depl trigger
chrispsheehan Oct 1, 2024
f54d223
codedeploy iams
chrispsheehan Oct 1, 2024
07ea896
deploy
chrispsheehan Oct 1, 2024
3220935
iam fixes
chrispsheehan Oct 1, 2024
6876a6e
get id
chrispsheehan Oct 1, 2024
50d9b02
allow code deploy ecs task access
chrispsheehan Oct 2, 2024
a9f2113
trig
chrispsheehan Oct 2, 2024
7b74da8
minor
chrispsheehan Oct 2, 2024
834af82
lock down codedeploy iam
chrispsheehan Oct 2, 2024
3283d8b
individual policies
chrispsheehan Oct 2, 2024
a9cc84e
rm l
chrispsheehan Oct 2, 2024
1f8be53
elb iam fix
chrispsheehan Oct 2, 2024
51fe523
add task set to iams
chrispsheehan Oct 2, 2024
a0acc75
pass in dep config name
chrispsheehan Oct 2, 2024
a3d77f1
speed up feedback
chrispsheehan Oct 2, 2024
c71fbb9
fix
chrispsheehan Oct 2, 2024
e560d66
ModifyListener iam for lb
chrispsheehan Oct 2, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions .github/actions/build_image/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ inputs:
aws_region:
description: 'ECR aws region'
required: true
ecr_repository_name:
description: 'ECR repo name'
required: true

runs:
using: 'composite'
Expand All @@ -20,10 +23,10 @@ runs:
- 'src/**'

- name: Check if ECR repository is empty
id: should_build
id: check_should_build
shell: bash
run: |
IMAGE_COUNT=$(aws ecr describe-images --repository-name ${{ github.event.repository.name }} --region ${{ inputs.aws_region }} --query 'imageDetails | length(@)' --output text)
IMAGE_COUNT=$(aws ecr describe-images --repository-name ${{ inputs.ecr_repository_name }} --region ${{ inputs.aws_region }} --query 'imageDetails | length(@)' --output text)
echo Image count: "$IMAGE_COUNT"
if [[ "${{ steps.changes.outputs.src }}" == "true" || "$IMAGE_COUNT" -eq 0 ]]; then
echo "Either src changed or ECR is empty"
Expand All @@ -43,19 +46,32 @@ runs:
name: Set variables
shell: bash
run: |
echo "LOCAL_IMAGE=${{ github.event.repository.name }}:local" >> $GITHUB_ENV
echo "LOCAL_IMAGE=${{ inputs.ecr_repository_name }}:local" >> $GITHUB_ENV
echo "IMAGE_TAG=$(echo $GITHUB_SHA | cut -c 1-6)" >> $GITHUB_ENV

- if: env.SHOULD_BUILD == 'true'
name: Build Docker Image
shell: bash
run: |
docker build . -t $LOCAL_IMAGE
docker tag $LOCAL_IMAGE ${{ inputs.aws_account_id }}.dkr.ecr.${{ inputs.aws_region }}.amazonaws.com/${{ github.event.repository.name }}:$IMAGE_TAG
docker tag $LOCAL_IMAGE ${{ inputs.aws_account_id }}.dkr.ecr.${{ inputs.aws_region }}.amazonaws.com/${{ inputs.ecr_repository_name }}:$IMAGE_TAG

- if: env.SHOULD_BUILD == 'true'
name: Push Docker Image to ECR
shell: bash
run: |
aws ecr get-login-password --region ${{ env.aws_region }} | docker login --username AWS --password-stdin ${{ inputs.AWS_ACCOUNT_ID }}.dkr.ecr.${{ inputs.aws_region }}.amazonaws.com
docker push ${{ inputs.aws_account_id }}.dkr.ecr.${{ inputs.aws_region }}.amazonaws.com/${{ github.event.repository.name }}:$IMAGE_TAG
aws ecr get-login-password --region ${{ inputs.aws_region }} | docker login --username AWS --password-stdin ${{ inputs.aws_account_id }}.dkr.ecr.${{ inputs.aws_region }}.amazonaws.com
docker push ${{ inputs.aws_account_id }}.dkr.ecr.${{ inputs.aws_region }}.amazonaws.com/${{ inputs.ecr_repository_name }}:$IMAGE_TAG
IMAGE_URI="${{ inputs.aws_account_id }}.dkr.ecr.${{ inputs.aws_region }}.amazonaws.com/${{ inputs.ecr_repository_name }}:$IMAGE_TAG"
echo "IMAGE_URI=$IMAGE_URI" >> $GITHUB_ENV
echo Image:$IMAGE_URI

- if: env.SHOULD_BUILD != 'true'
name: Get Latest ECR Image Tag
shell: bash
run: |
LATEST_TAG=$(aws ecr describe-images --repository-name ${{ inputs.ecr_repository_name }} \
--region ${{ inputs.aws_region }} --query 'sort_by(imageDetails,& imagePushedAt)[-1].imageTags[0]' --output text)
IMAGE_URI="${{ inputs.aws_account_id }}.dkr.ecr.${{ inputs.aws_region }}.amazonaws.com/${{ inputs.ecr_repository_name }}:$LATEST_TAG"
echo "$IMAGE_TAG=$LATEST_TAG" >> $GITHUB_ENV
echo "IMAGE_URI=$IMAGE_URI" >> $GITHUB_ENV
64 changes: 64 additions & 0 deletions .github/actions/check_svc/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: Check ECS Service
description: Check if an ECS cluster and service exist
inputs:
aws_region:
description: 'AWS Region'
required: true
aws_role:
description: 'AWS Role to assume'
required: true
tfvars_file:
description: 'Path to the tfvars file'
required: true

runs:
using: "composite"
steps:
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-region: ${{ inputs.aws_region }}
role-to-assume: arn:aws:iam::${{ inputs.aws_role }}
role-session-name: GitHubActions

- name: Read project_name from tfvars file
id: read-project-name
shell: bash
run: |
# Extract project_name from tfvars file
PROJECT_NAME=$(grep 'project_name' "${{ inputs.tfvars_file }}" | sed 's/.*= "\(.*\)"/\1/')
echo "Extracted Project Name: $PROJECT_NAME"
echo "PROJECT_NAME=$PROJECT_NAME" >> $GITHUB_ENV

- name: Check if ECS cluster and service exist
id: check-cluster-and-service-exists
shell: bash
run: |
# Check if the ECS cluster exists and is ACTIVE
CLUSTER_STATUS=$(aws ecs describe-clusters \
--clusters ${{ env.PROJECT_NAME }} \
--region ${{ inputs.aws_region }} \
--query 'clusters[0].status' \
--output text 2>/dev/null || echo "NOT_FOUND")

echo "Cluster status: $CLUSTER_STATUS"

if [ "$CLUSTER_STATUS" != "ACTIVE" ]; then
# If cluster is not ACTIVE, set service existence to false
echo "SERVICE_EXISTS=false" >> $GITHUB_ENV
else
# If the cluster is active, check if the service exists
SERVICE_STATUS=$(aws ecs describe-services \
--cluster ${{ env.PROJECT_NAME }} \
--services ${{ env.PROJECT_NAME }} \
--query 'services[0].status' \
--region ${{ inputs.aws_region }} \
--output text 2>/dev/null || echo "NOT_FOUND")

if [ "$SERVICE_STATUS" == "NOT_FOUND" ]; then
echo "SERVICE_EXISTS=false" >> $GITHUB_ENV
else
echo "SERVICE_EXISTS=true" >> $GITHUB_ENV
fi
fi

98 changes: 98 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
name: Build

on:
workflow_call:
inputs:
aws_account_id:
required: true
type: string
aws_region:
required: true
type: string
aws_role:
required: true
type: string
outputs:
task_definition_arn:
description: "The ARN of the deployed task definition"
value: ${{ jobs.task.outputs.task_definition_arn }}
task_definition_revision:
description: "The revision of the deployed task definition"
value: ${{ jobs.task.outputs.task_definition_revision }}
image_uri:
value: ${{ jobs.image.outputs.image_uri }}
app_specs_bucket:
value: ${{ jobs.task.outputs.app_specs_bucket }}

permissions:
id-token: write
contents: read

jobs:
image:
runs-on: ubuntu-latest
outputs:
image_uri: ${{ steps.set-image-uri.outputs.image_uri }}
steps:
- uses: actions/checkout@v4
- uses: aws-actions/configure-aws-credentials@v4
with:
aws-region: ${{ inputs.aws_region }}
role-to-assume: arn:aws:iam::${{ inputs.aws_account_id }}:role/${{ inputs.aws_role }}
role-session-name: GitHubActions

- name: Build and push detected changes
id: build-image
uses: ./.github/actions/build_image
with:
aws_account_id: ${{ inputs.aws_account_id }}
aws_region: ${{ inputs.aws_region }}
ecr_repository_name: ${{ github.event.repository.name }}

- name: Set image_uri output
id: set-image-uri
run: |
echo "image_uri=${{ env.IMAGE_URI }}" >> $GITHUB_OUTPUT

task:
needs: image
runs-on: ubuntu-latest
env:
TF_VAR_image_uri: ${{ needs.image.outputs.image_uri }}
TF_VAR_ecr_repository_name: ${{ github.event.repository.name }}
outputs:
task_definition_arn: ${{ steps.set-envs.outputs.TASK_DEFINITION_ARN }}
task_definition_revision: ${{ steps.set-envs.outputs.TASK_DEFINITION_REVISION }}
app_specs_bucket: ${{ steps.set-envs.outputs.APP_SPECS_BUCKET }}
steps:
- uses: actions/checkout@v4
- uses: aws-actions/configure-aws-credentials@v4
with:
aws-region: ${{ inputs.aws_region }}
role-to-assume: arn:aws:iam::${{ inputs.aws_account_id }}:role/${{ inputs.aws_role }}
role-session-name: GitHubActions

- name: Init
shell: bash
run: |
cd tf/task
terraform init

- name: Deploy
shell: bash
id: deploy
run: |
cd tf/task
terraform apply -auto-approve -var-file=${{ github.workspace }}/tf/variables.tfvars

- name: Set env vars
id: set-envs
shell: bash
run: |
cd tf/task
TASK_DEFINITION_ARN=$(terraform output -raw task_definition_arn)
TASK_DEFINITION_REVISION=$(terraform output -raw task_definition_revision)
APP_SPECS_BUCKET=$(terraform output -raw app_specs_bucket)
echo "TASK_DEFINITION_ARN=$TASK_DEFINITION_ARN" >> $GITHUB_OUTPUT
echo "TASK_DEFINITION_REVISION=$TASK_DEFINITION_REVISION" >> $GITHUB_OUTPUT
echo "APP_SPECS_BUCKET=$APP_SPECS_BUCKET" >> $GITHUB_OUTPUT
Loading