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

Test ci cd #10

Merged
merged 55 commits into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
02d730c
include tg fmt in pre-commit hooks
digorgonzola Dec 5, 2023
420af2c
.gitignore: include .terraform.lock.hcl files
digorgonzola Dec 5, 2023
cc3f04b
add terragrunt deployment of ecs components
digorgonzola Dec 5, 2023
0f9cd92
update test workflow to include terragrunt deploy
digorgonzola Dec 5, 2023
1ec542b
test.yml: only build for amd64
digorgonzola Dec 5, 2023
c2bad01
test.yml: add debug information temp disable build
digorgonzola Dec 5, 2023
e3a4aa4
deploy: add .terraform.lock.hcl and correct order of inputs
digorgonzola Dec 5, 2023
e33cd2d
test.yml: include env vars
digorgonzola Dec 5, 2023
b699b59
terragrunt.hcl: simplify aws config
digorgonzola Dec 5, 2023
457cc8f
test.yml: aws authentication
digorgonzola Dec 5, 2023
5f7ef07
debug aws auth
digorgonzola Dec 5, 2023
e05ac30
simplify aws auth
digorgonzola Dec 5, 2023
fb90b3b
fix provider config
digorgonzola Dec 5, 2023
f3db34f
tg: remove further unnecessary bits
digorgonzola Dec 5, 2023
b7a69ac
test.yml: remove debug steps
digorgonzola Dec 5, 2023
24a8b7b
tg: fix aws account local
digorgonzola Dec 5, 2023
c4f7fa7
add apply step
digorgonzola Dec 5, 2023
e6ab59f
add image tag input
digorgonzola Dec 5, 2023
e90e1a8
test.yml: pass image_tag to tg
digorgonzola Dec 5, 2023
5e20d43
clean up docker test image + tag
digorgonzola Dec 5, 2023
82117f7
remove obsolete tf files
digorgonzola Dec 6, 2023
e26e164
tg: add new module for ssm-parameter
digorgonzola Dec 6, 2023
9396b04
tf: add new module for ssm-parameter
digorgonzola Dec 6, 2023
69399a4
tf/ecs: refactor ssm parameters to variables
digorgonzola Dec 6, 2023
b9c65b9
tg/ecs: update .terraform.lock.hcl
digorgonzola Dec 6, 2023
46d6c2f
tg/ssm: add .terraform.lock.hcl
digorgonzola Dec 6, 2023
873f8cd
tg/ecs: refactor use of params into vars
digorgonzola Dec 6, 2023
f04eab5
update .gitignore
digorgonzola Dec 6, 2023
ec97807
test.yml: expose all vars/secrets to env context
digorgonzola Dec 6, 2023
60b547e
tg/ssm: rename env var
digorgonzola Dec 6, 2023
7a189ff
update test.yml on event types
digorgonzola Dec 6, 2023
4706566
deploy: refactor to include environment name
digorgonzola Dec 6, 2023
0acde6c
test.yml: set docker build job environment
digorgonzola Dec 6, 2023
cfb26f0
test.yml: update aws credential steps
digorgonzola Dec 6, 2023
551b585
tg/ecs: update route53 records
digorgonzola Dec 6, 2023
b52a198
test.yml: expose ssm param values via tfvars.json file
digorgonzola Dec 8, 2023
89105c6
tf/ecs: update vars to match ssm parameter key names
digorgonzola Dec 8, 2023
2778cdb
tg: remove ssm-parameter module
digorgonzola Dec 8, 2023
fe3c99f
tg/ecs: remove obsolete dependencies and inputs
digorgonzola Dec 8, 2023
ece812b
test.yml: correct image var name
digorgonzola Dec 8, 2023
77e708b
test.yml: update step name and add new line
digorgonzola Dec 8, 2023
7fd9cf1
terragrunt.hcl: add repo url to tags
digorgonzola Dec 8, 2023
4871c3a
test.yml: just build and test docker image
digorgonzola Dec 9, 2023
b0ed432
deploy-staging.yml: build and push workflow for staging
digorgonzola Dec 9, 2023
85d723e
deploy-production.yml: plan and deploy to production
digorgonzola Dec 9, 2023
d6dafd5
delete release.yml
digorgonzola Dec 9, 2023
4ad2559
test.yml: fix docker build tag
digorgonzola Dec 9, 2023
b5343de
deploy-staging.yml: fix aws_region var
digorgonzola Dec 9, 2023
f2d238c
pre-commit.yml: run pre-commit checks before allowing merge
digorgonzola Dec 9, 2023
0993059
pre-commit.yml: install terragrunt
digorgonzola Dec 9, 2023
fe6ea70
requirements.txt: include pre-commit framework
digorgonzola Dec 9, 2023
d1bc11a
.pre-commit-config.yaml: update tf_validate args
digorgonzola Dec 9, 2023
c5f7bbb
tf/ecs versions.tf: add required providers/tf version
digorgonzola Dec 9, 2023
0bb0d37
tg/ecs: terragrunt.hcl update formatting
digorgonzola Dec 9, 2023
72e33c2
pre-commit.yml: set tf version
digorgonzola Dec 9, 2023
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
146 changes: 146 additions & 0 deletions .github/workflows/deploy-production.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
name: release

on:
release:
types:
- published

permissions:
id-token: write # This is required for requesting the JWT
contents: read # This is required for actions/checkout

jobs:
production_deploy_plan:
runs-on: ubuntu-latest
environment: production
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
audience: sts.amazonaws.com
aws-region: ${{ vars.AWS_REGION }}
role-to-assume: ${{ secrets.AWS_ROLE_ARN }}

- name: Get Image Metadata from Release
uses: dsaltares/[email protected]
with:
version: ${{ github.event.release.id }}
file: metadata.json

- name: Set Image Digest from Metadata
id: set_image_digest
run: |
image_digest=$(cat /tmp/metadata.json | jq -r '."containerimage.digest"')
echo "image_digest=$image_digest" >> $GITHUB_OUTPUT

- name: Export shared infrastructure SSM parameter values to auto.tfvars.json files
env:
deploy_path: ./deploy/tg/ecs
environment: ${{ vars.ENVIRONMENT }}
run: |
params=( apps/alb/${{ vars.ALB }} apps/ecr/${{ vars.ECR_REPOSITORY }} core rds/${{ vars.RDS_DB }} )
for param in ${params[@]}; do
filename="$environment.${param//\//-}.auto.tfvars.json"
aws ssm get-parameters-by-path \
--path "/$param/" \
--recursive \
--output json \
--query 'Parameters[*]' \
| jq '. |= map({ (.Name | split("/")[-1]): .Value }) | add' \
> "$deploy_path/$filename"
done

- name: Expose github environment as shell variables
env:
SECRETS_CONTEXT: ${{ toJson(secrets) }}
VARS_CONTEXT: ${{ toJson(vars) }}
run: |
EOF=$(dd if=/dev/urandom bs=15 count=1 status=none | base64)
to_envs() { jq -r "to_entries[] | \"\(.key)<<$EOF\n\(.value)\n$EOF\n\""; }
echo "$VARS_CONTEXT" | to_envs >> $GITHUB_ENV
echo "$SECRETS_CONTEXT" | to_envs >> $GITHUB_ENV

- name: Terragrunt Plan
id: terragrunt_plan
uses: gruntwork-io/[email protected]
with:
tf_version: '1.5.7'
tg_version: '0.51.0'
tg_dir: './deploy/tg'
tg_command: 'run-all plan'
env:
TF_INPUT: 0
TF_IN_AUTOMATION: true
# get the image digest from the build job with optional override from vars context
TF_VAR_image: ${{ vars.IMAGE || steps.set_image_digest.outputs.image_digest }}

production_deploy_apply:
runs-on: ubuntu-latest
environment: staging
needs: [production_deploy_plan]
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
audience: sts.amazonaws.com
aws-region: ${{ vars.AWS_REGION }}
role-to-assume: ${{ secrets.AWS_ROLE_ARN }}

- name: Get Image Metadata from Release
uses: dsaltares/[email protected]
with:
version: ${{ github.event.release.id }}
file: metadata.json

- name: Set Image Digest from Metadata
id: set_image_digest
run: |
image_digest=$(cat /tmp/metadata.json | jq -r '."containerimage.digest"')
echo "image_digest=$image_digest" >> $GITHUB_OUTPUT

- name: Export shared infrastructure SSM parameter values to auto.tfvars.json files
env:
deploy_path: ./deploy/tg/ecs
environment: ${{ vars.ENVIRONMENT }}
run: |
params=( apps/alb/${{ vars.ALB }} apps/ecr/${{ vars.ECR_REPOSITORY }} core rds/${{ vars.RDS_DB }} )
for param in ${params[@]}; do
filename="$environment.${param//\//-}.auto.tfvars.json"
aws ssm get-parameters-by-path \
--path "/$param/" \
--recursive \
--output json \
--query 'Parameters[*]' \
| jq '. |= map({ (.Name | split("/")[-1]): .Value }) | add' \
> "$deploy_path/$filename"
done

- name: Expose github environment as shell variables
env:
SECRETS_CONTEXT: ${{ toJson(secrets) }}
VARS_CONTEXT: ${{ toJson(vars) }}
run: |
EOF=$(dd if=/dev/urandom bs=15 count=1 status=none | base64)
to_envs() { jq -r "to_entries[] | \"\(.key)<<$EOF\n\(.value)\n$EOF\n\""; }
echo "$VARS_CONTEXT" | to_envs >> $GITHUB_ENV
echo "$SECRETS_CONTEXT" | to_envs >> $GITHUB_ENV

- name: Terragrunt Apply
id: terragrunt_plan
uses: gruntwork-io/[email protected]
with:
tf_version: '1.5.7'
tg_version: '0.51.0'
tg_dir: './deploy/tg'
tg_command: 'run-all apply'
env:
TF_INPUT: 0
TF_IN_AUTOMATION: true
# get the image digest from the build job with optional override from vars context
TF_VAR_image: ${{ vars.IMAGE || steps.set_image_digest.outputs.image_digest }}
203 changes: 203 additions & 0 deletions .github/workflows/deploy-staging.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
name: Deploy to Staging

on:
push:
branches:
- master

permissions:
id-token: write # This is required for requesting the JWT
contents: read # This is required for actions/checkout

jobs:
build_test_push:
runs-on: ubuntu-latest
environment: staging
outputs:
image_digest: ${{ steps.build_and_push.outputs.digest }}
image_metadata: ${{ steps.build_and_push.outputs.metadata }}
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up QEMU
uses: docker/setup-qemu-action@v3

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

- name: Setup Docker Structure Test
run: >
curl -LO
https://storage.googleapis.com/container-structure-test/latest/container-structure-test-linux-amd64
&& chmod +x container-structure-test-linux-amd64 && sudo mv container-structure-test-linux-amd64
/usr/local/bin/container-structure-test

- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
audience: sts.amazonaws.com
aws-region: ${{ vars.AWS_REGION }}
role-to-assume: ${{ secrets.AWS_ROLE_ARN }}

- name: Login to ECR
uses: docker/login-action@v3
with:
registry: ${{ vars.ECR_REGISTRY }}

- name: Build Docker Image
uses: docker/build-push-action@v5
with:
context: .
load: true
tags: ${{ vars.ECR_REPOSITORY }}:latest

- name: Test Docker Image
run: |
container-structure-test test --image ${{ vars.ECR_REPOSITORY }}:latest --config tests/config.yaml

- name: Build and Push Docker Image
id: build_and_push
uses: docker/build-push-action@v5
with:
context: .
# Only building for AMD64 for now
# platforms: linux/amd64,linux/arm64
push: true
tags: ${{ vars.ECR_REGISTRY }}/${{ vars.ECR_REPOSITORY }}:latest

staging_deploy_plan:
runs-on: ubuntu-latest
environment: staging
needs: build_test_push
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
audience: sts.amazonaws.com
aws-region: ${{ vars.AWS_REGION }}
role-to-assume: ${{ secrets.AWS_ROLE_ARN }}

- name: Export shared infrastructure SSM parameter values to auto.tfvars.json files
env:
deploy_path: ./deploy/tg/ecs
environment: ${{ vars.ENVIRONMENT }}
run: |
params=( apps/alb/${{ vars.ALB }} apps/ecr/${{ vars.ECR_REPOSITORY }} core rds/${{ vars.RDS_DB }} )
for param in ${params[@]}; do
filename="$environment.${param//\//-}.auto.tfvars.json"
aws ssm get-parameters-by-path \
--path "/$param/" \
--recursive \
--output json \
--query 'Parameters[*]' \
| jq '. |= map({ (.Name | split("/")[-1]): .Value }) | add' \
> "$deploy_path/$filename"
done

- name: Expose github environment as shell variables
env:
SECRETS_CONTEXT: ${{ toJson(secrets) }}
VARS_CONTEXT: ${{ toJson(vars) }}
run: |
EOF=$(dd if=/dev/urandom bs=15 count=1 status=none | base64)
to_envs() { jq -r "to_entries[] | \"\(.key)<<$EOF\n\(.value)\n$EOF\n\""; }
echo "$VARS_CONTEXT" | to_envs >> $GITHUB_ENV
echo "$SECRETS_CONTEXT" | to_envs >> $GITHUB_ENV

- name: Terragrunt Plan
id: terragrunt_plan
uses: gruntwork-io/[email protected]
with:
tf_version: '1.5.7'
tg_version: '0.51.0'
tg_dir: './deploy/tg'
tg_command: 'run-all plan'
env:
TF_INPUT: 0
TF_IN_AUTOMATION: true
# get the image digest from the build job with optional override from vars context
TF_VAR_image: ${{ vars.IMAGE || needs.build_test_push.outputs.image_digest }}

staging_deploy_apply:
runs-on: ubuntu-latest
environment: staging
needs: [staging_deploy_plan, build_test_push]
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
audience: sts.amazonaws.com
aws-region: ${{ vars.AWS_REGION }}
role-to-assume: ${{ secrets.AWS_ROLE_ARN }}

- name: Export shared infrastructure SSM parameter values to auto.tfvars.json files
env:
deploy_path: ./deploy/tg/ecs
environment: ${{ vars.ENVIRONMENT }}
run: |
params=( apps/alb/${{ vars.ALB }} apps/ecr/${{ vars.ECR_REPOSITORY }} core rds/${{ vars.RDS_DB }} )
for param in ${params[@]}; do
filename="$environment.${param//\//-}.auto.tfvars.json"
aws ssm get-parameters-by-path \
--path "/$param/" \
--recursive \
--output json \
--query 'Parameters[*]' \
| jq '. |= map({ (.Name | split("/")[-1]): .Value }) | add' \
> "$deploy_path/$filename"
done

- name: Expose github environment as shell variables
env:
SECRETS_CONTEXT: ${{ toJson(secrets) }}
VARS_CONTEXT: ${{ toJson(vars) }}
run: |
EOF=$(dd if=/dev/urandom bs=15 count=1 status=none | base64)
to_envs() { jq -r "to_entries[] | \"\(.key)<<$EOF\n\(.value)\n$EOF\n\""; }
echo "$VARS_CONTEXT" | to_envs >> $GITHUB_ENV
echo "$SECRETS_CONTEXT" | to_envs >> $GITHUB_ENV

- name: Terragrunt Apply
id: terragrunt_plan
uses: gruntwork-io/[email protected]
with:
tf_version: '1.5.7'
tg_version: '0.51.0'
tg_dir: './deploy/tg'
tg_command: 'run-all apply'
env:
TF_INPUT: 0
TF_IN_AUTOMATION: true
# get the image digest from the build job with optional override from vars context
TF_VAR_image: ${{ vars.IMAGE || needs.build_test_push.outputs.image_digest }}

create_release:
name: Create Release
runs-on: ubuntu-latest
needs: [build_test_push]
steps:
- name: Write image metadata to file
id: metadata_to_file
run: ${{ needs.build_test_push.outputs.image_metadata }} > metadata.json
- name: Create Draft Release
id: create_draft_release
uses: softprops/action-gh-release@v1
with:
name: Draft Release ${{ github.ref }}
body: |
## Info
Commit ${{ github.sha }} was deployed to `staging`. [See code diff](${{ github.event.compare }}).

It was initialized by [${{ github.event.sender.login }}](${{ github.event.sender.html_url }}).

## How to Promote?
In order to promote this to prod, edit the draft and press **"Publish release"**.
draft: true
files: metadata.json
32 changes: 32 additions & 0 deletions .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Run Pre-commit Checks

on:
pull_request:
branches:
- master

permissions:
id-token: write # This is required for requesting the JWT
contents: read # This is required for actions/checkout

jobs:
pre_commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.10'
- uses: hashicorp/setup-terraform@v3
with:
terraform_version: "1.5.7"
- name: Setup Terragrunt
id: setup_terragrunt
run: |
wget https://github.com/gruntwork-io/terragrunt/releases/download/v${tg_version}/terragrunt_linux_amd64 \
&& mv terragrunt_linux_amd64 terragrunt \
&& chmod +x terragrunt \
&& mv terragrunt /usr/local/bin/terragrunt
env:
tg_version: '0.51.0'
- uses: pre-commit/[email protected]
Loading