Skip to content

Commit 557e14c

Browse files
authored
feat: Refactor to use a layered approach instead of full environment configs for easier deployment. (#38)
1 parent 9feef1c commit 557e14c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+896
-500
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
name: Discover changed OpenTofu modules
2+
description: |
3+
Finds all OpenTofu modules that have changed in a pull request or push to a
4+
branch.
5+
outputs:
6+
modules:
7+
description: A JSON array of all changed modules.
8+
value: ${{ steps.modified.outputs.modules }}
9+
inputs:
10+
working-directory:
11+
description: The working directory to search for modules in.
12+
required: false
13+
default: tofu
14+
runs:
15+
using: composite
16+
steps:
17+
- name: Find all OpenTofu modules
18+
id: find
19+
uses: bendrucker/find-terraform-modules@v1
20+
with:
21+
working-directory: ${{ inputs.working-directory }}
22+
- name: Show all matching modules
23+
shell: bash
24+
run: |
25+
mods=(${{ join(fromJSON(steps.find.outputs.modules), ' ') }})
26+
printf "%s\n" "${mods[@]}"
27+
- name: Find all changed files
28+
id: diff
29+
uses: technote-space/get-diff-action@v6
30+
with:
31+
FORMAT: json
32+
- name: Show changed files
33+
shell: bash
34+
run: |
35+
echo "${{ steps.diff.outputs.diff }}"
36+
- name: Get the modified modules
37+
id: modified
38+
uses: actions/github-script@v7
39+
with:
40+
script: |
41+
const modules = ${{ steps.find.outputs.modules }}
42+
const diff = ${{ steps.diff.outputs.diff }}
43+
const modifiedModules = modules.filter(
44+
(module) => {
45+
return !!diff.find(file => new RegExp(`^${module}/.+`).test(file))
46+
}
47+
)
48+
49+
core.setOutput('modules', modifiedModules)
50+
- name: Show modified modules
51+
shell: bash
52+
run: |
53+
echo "${{ steps.modified.outputs.modules }}"
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Setup OpenTofu
2+
description: Sets up OpenTofu and related environment variables
3+
inputs:
4+
config:
5+
description: OpenTofu configuration to initialize.
6+
required: true
7+
default: service
8+
cache-key:
9+
description: Cache key for OpenTofu initialization.
10+
required: false
11+
default: ''
12+
runs:
13+
using: composite
14+
steps:
15+
- name: Cache OpenTofu
16+
uses: actions/cache@v4
17+
with:
18+
path: ./tofu/config/${{ inputs.config }}/.terraform
19+
key: ${{ runner.os }}-tofu-${{ inputs.config }}-${{ inputs.cache-key }}-${{ hashFiles('./tofu/config/${{ inputs.config }}/.terraform.lock.hcl') }}
20+
restore-keys: |
21+
${{ runner.os }}-tofu-${{ inputs.config }}-${{ inputs.cache-key }}-
22+
- name: Setup OpenTofu
23+
uses: opentofu/setup-opentofu@v1
24+
with:
25+
tofu_wrapper: false
26+
- name: Display OpenTofu version
27+
shell: bash
28+
run: tofu version
29+
- name: Initialize OpenTofu
30+
shell: bash
31+
working-directory: ./tofu/config/${{ inputs.config }}
32+
run: tofu init

.github/workflows/branch.yaml

Lines changed: 0 additions & 111 deletions
This file was deleted.

.github/workflows/deploy-app.yaml

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,33 +26,39 @@ jobs:
2626
runs-on: ubuntu-latest
2727
environment: ${{ inputs.environment }}
2828
env:
29+
# Set required secrets and variables.
2930
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
3031
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
3132
AWS_REGION: ${{ vars.AWS_REGION || 'us-east-1' }}
3233
DOPPLER_TOKEN: ${{ secrets.DOPPLER_TOKEN }}
34+
35+
TF_VAR_application: ${{ inputs.application }}
36+
TF_VAR_environment: ${{ vars.TF_VAR_ENVIRONMENT }}
37+
TF_VAR_program: ${{ vars.TF_VAR_PROGRAM }}
38+
TF_VAR_project: ${{ vars.TF_VAR_PROJECT }}
3339
steps:
3440
- name: distinct ID ${{ inputs.distinct_id || github.run_id }}
3541
uses: imesense/[email protected]
3642
with:
3743
input-string: ${{ inputs.distinct_id || github.run_id }}
3844
- name: Checkout code
3945
uses: actions/checkout@v4
46+
- name: Verify the application exists
47+
uses: andstor/file-existence-action@v3
48+
with:
49+
fail: true
50+
files: "tofu/config/hosting/specs/${{ inputs.application }}.yaml"
4051
- name: Set up AWS credentials
4152
uses: aws-actions/configure-aws-credentials@v4
4253
with:
4354
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
4455
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
4556
aws-region: us-east-1
4657
- name: Setup OpenTofu
47-
uses: opentofu/setup-opentofu@v1
48-
- name: Display OpenTofu version
49-
run: tofu version
50-
- name: Initialize OpenTofu
51-
working-directory: ./tofu/config/${{ inputs.environment }}/infra
52-
run: tofu init
53-
- name: Verify the application exists
54-
working-directory: ./tofu/config/${{ inputs.environment }}/infra
55-
run: tofu state list module.app\[\"${{ inputs.application }}\"]
58+
uses: ./.github/actions/setup-opentofu
59+
with:
60+
cache-key: ${{ inputs.application }}
61+
config: hosting
5662
- name: Apply changes
57-
working-directory: ./tofu/config/${{ inputs.environment }}/infra
58-
run: tofu apply --target module.app\[\"${{ inputs.application }}\"] --auto-approve
63+
working-directory: ./tofu/config/hosting
64+
run: tofu apply --auto-approve

.github/workflows/deploy.yaml

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ name: Deploy infrastructure
33
on:
44
workflow_dispatch:
55
inputs:
6+
config:
7+
description: Configuration to deploy.
8+
required: true
9+
type: choice
10+
options:
11+
- foundation
12+
- networking
13+
- docs
614
environment:
715
description: Environment to deploy to.
816
default: development
@@ -14,14 +22,22 @@ permissions:
1422

1523
jobs:
1624
deploy:
17-
name: Deploy infrastrucure to ${{ inputs.environment }}
25+
name: Deploy ${{ inputs.config }} to ${{ inputs.environment }}
1826
runs-on: ubuntu-latest
1927
environment: ${{ inputs.environment }}
2028
env:
29+
# Set required secrets and variables.
2130
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
2231
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
2332
AWS_REGION: ${{ vars.AWS_REGION || 'us-east-1' }}
2433
DOPPLER_TOKEN: ${{ secrets.DOPPLER_TOKEN }}
34+
TF_VAR_public_subnet_cidrs: ${{ secrets.TF_VAR_PUBLIC_SUBNET_CIDRS }}
35+
TF_VAR_private_subnet_cidrs: ${{ secrets.TF_VAR_PRIVATE_SUBNET_CIDRS }}
36+
37+
TF_VAR_vpc_cidr: ${{ secrets.TF_VAR_VPC_CIDR }}
38+
TF_VAR_environment: ${{ vars.TF_VAR_ENVIRONMENT }}
39+
TF_VAR_program: ${{ vars.TF_VAR_PROGRAM }}
40+
TF_VAR_project: ${{ vars.TF_VAR_PROJECT }}
2541
steps:
2642
- name: Checkout code
2743
uses: actions/checkout@v4
@@ -32,12 +48,9 @@ jobs:
3248
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
3349
aws-region: us-east-1
3450
- name: Setup OpenTofu
35-
uses: opentofu/setup-opentofu@v1
36-
- name: Display OpenTofu version
37-
run: tofu version
38-
- name: Initialize OpenTofu
39-
working-directory: ./tofu/config/${{ inputs.environment }}/infra
40-
run: tofu init
51+
uses: ./.github/actions/setup-opentofu
52+
with:
53+
config: ${{ inputs.config }}
4154
- name: Apply changes
42-
working-directory: ./tofu/config/${{ inputs.environment }}/infra
55+
working-directory: ./tofu/config/${{ inputs.config }}
4356
run: tofu apply --auto-approve

.github/workflows/main.yaml

Lines changed: 0 additions & 60 deletions
This file was deleted.

0 commit comments

Comments
 (0)