Skip to content
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

Improve Docker build in deployment workflows #135

Merged
merged 5 commits into from
Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 10 additions & 0 deletions .aws/set-env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash
set -eu

task_file=$1

# make replacements using env vars

sed -i "s/<aws_account>/$AWS_ACCOUNT/g" $task_file
sed -i "s/<aws_bucket>/$AWS_BUCKET/g" $task_file
sed -i "s/<aws_region>/$AWS_REGION/g" $task_file
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.aws/
.git/
.github/
.vscode/
Expand Down
61 changes: 34 additions & 27 deletions .github/workflows/ecs-deploy-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,59 +36,66 @@ jobs:
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION }}

- name: Login to Amazon ECR
id: login-ecr
- name: AWS Login to Amazon ECR
id: aws-login-ecr
uses: aws-actions/amazon-ecr-login@v1

- name: Build, tag, and push image to Amazon ECR
id: build-client-image
- name: Define image paths
id: define-image-paths
env:
ECR_REPOSITORY: cal-itp-benefits-client
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
IMAGE_TAG: ${{ github.sha }}
ECR_REGISTRY: ${{ steps.aws-login-ecr.outputs.registry }}
GIT_SHA: ${{ github.sha }}
AWS_CLI_TAG: ${{ secrets.AWS_CLI_TAG }}
run: |
# Build a docker container and
# push it to ECR so that it can
# be deployed to ECS.
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG"
echo "::set-output name=client::$ECR_REGISTRY/cal-itp-benefits-client:$GIT_SHA"
echo "::set-output name=config::$ECR_REGISTRY/aws-cli:$AWS_CLI_TAG"

- name: Define path to configuration image
id: define-config-image
env:
ECR_REPOSITORY: aws-cli
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
IMAGE_TAG: ${{ secrets.AWS_CLI_TAG }}
run: |
echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG"
- name: Docker Login to Amazon ECR
id: docker-login-ecr
uses: docker/login-action@v1
with:
registry: ${{ steps.aws-login-ecr.outputs.registry }}
username: ${{ secrets.AWS_ACCESS_KEY_ID }}
password: ${{ secrets.AWS_SECRET_ACCESS_KEY }}

- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v1

- name: Build, tag, and push image to Amazon ECR
id: build-client-image
uses: docker/build-push-action@v2
with:
builder: ${{ steps.buildx.outputs.name }}
cache-from: type=gha,scope=benefits
cache-to: type=gha,scope=benefits,mode=max
context: .
push: true
tags: ${{ steps.define-image-paths.outputs.client }}

- name: Add environment-specific config to ECS task
id: task-env
env:
AWS_ACCOUNT: ${{ secrets.AWS_ACCOUNT }}
AWS_BUCKET: ${{ secrets.AWS_BUCKET }}
AWS_REGION: ${{ secrets.AWS_REGION }}
run: |
sed -i "s/<aws_account>/$AWS_ACCOUNT/g" .aws/ecs-task.json
sed -i "s/<aws_bucket>/$AWS_BUCKET/g" .aws/ecs-task.json
sed -i "s/<aws_region>/$AWS_REGION/g" .aws/ecs-task.json
.aws/set-env.sh .aws/ecs-task.json

- name: Fill in client image ID in ECS task
id: client-task-def
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: .aws/ecs-task.json
container-name: cal-itp-benefits-client
image: ${{ steps.build-client-image.outputs.image }}
image: ${{ steps.define-image-paths.outputs.client }}

- name: Fill in config image ID in ECS task
id: config-task-def
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: ${{ steps.client-task-def.outputs.task-definition }}
container-name: cal-itp-benefits-client-config
image: ${{ steps.define-config-image.outputs.image }}
image: ${{ steps.define-image-paths.outputs.config }}

- name: Deploy Amazon ECS task definition
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
Expand Down
61 changes: 34 additions & 27 deletions .github/workflows/ecs-deploy-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,59 +27,66 @@ jobs:
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION }}

- name: Login to Amazon ECR
id: login-ecr
- name: AWS Login to Amazon ECR
id: aws-login-ecr
uses: aws-actions/amazon-ecr-login@v1

- name: Build, tag, and push image to Amazon ECR
id: build-client-image
- name: Define image paths
id: define-image-paths
env:
ECR_REPOSITORY: cal-itp-benefits-client
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
IMAGE_TAG: ${{ github.sha }}
ECR_REGISTRY: ${{ steps.aws-login-ecr.outputs.registry }}
GIT_SHA: ${{ github.sha }}
AWS_CLI_TAG: ${{ secrets.AWS_CLI_TAG }}
run: |
# Build a docker container and
# push it to ECR so that it can
# be deployed to ECS.
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG"
echo "::set-output name=client::$ECR_REGISTRY/cal-itp-benefits-client:$GIT_SHA"
echo "::set-output name=config::$ECR_REGISTRY/aws-cli:$AWS_CLI_TAG"

- name: Define path to configuration image
id: define-config-image
env:
ECR_REPOSITORY: aws-cli
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
IMAGE_TAG: ${{ secrets.AWS_CLI_TAG }}
run: |
echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG"
- name: Docker Login to Amazon ECR
id: docker-login-ecr
uses: docker/login-action@v1
with:
registry: ${{ steps.aws-login-ecr.outputs.registry }}
username: ${{ secrets.AWS_ACCESS_KEY_ID }}
password: ${{ secrets.AWS_SECRET_ACCESS_KEY }}

- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v1

- name: Build, tag, and push image to Amazon ECR
id: build-client-image
uses: docker/build-push-action@v2
with:
builder: ${{ steps.buildx.outputs.name }}
cache-from: type=gha,scope=benefits
cache-to: type=gha,scope=benefits,mode=max
context: .
push: true
tags: ${{ steps.define-image-paths.outputs.client }}

- name: Add environment-specific config to ECS task
id: task-env
env:
AWS_ACCOUNT: ${{ secrets.AWS_ACCOUNT }}
AWS_BUCKET: ${{ secrets.AWS_BUCKET }}
AWS_REGION: ${{ secrets.AWS_REGION }}
run: |
sed -i "s/<aws_account>/$AWS_ACCOUNT/g" .aws/ecs-task.json
sed -i "s/<aws_bucket>/$AWS_BUCKET/g" .aws/ecs-task.json
sed -i "s/<aws_region>/$AWS_REGION/g" .aws/ecs-task.json
.aws/set-env.sh .aws/ecs-task.json

- name: Fill in client image ID in ECS task
id: client-task-def
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: .aws/ecs-task.json
container-name: cal-itp-benefits-client
image: ${{ steps.build-client-image.outputs.image }}
image: ${{ steps.define-image-paths.outputs.client }}

- name: Fill in config image ID in ECS task
id: config-task-def
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: ${{ steps.client-task-def.outputs.task-definition }}
container-name: cal-itp-benefits-client-config
image: ${{ steps.define-config-image.outputs.image }}
image: ${{ steps.define-image-paths.outputs.config }}

- name: Deploy Amazon ECS task definition
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
Expand Down