Skip to content

Commit 2160191

Browse files
authored
Set default working directory where possible (#3258)
In the GitHub Actions workflows, this uses the `defaults` option to set the working directory to the `terraform/app` directory by default so we don't need to override it for each step in the workflow. See https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/setting-a-default-shell-and-working-directory for more information on how this works.
2 parents ff7712c + c322ee2 commit 2160191

3 files changed

Lines changed: 17 additions & 16 deletions

File tree

.github/workflows/deploy-application.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ env:
3030
aws-role: ${{ inputs.environment == 'production'
3131
&& 'arn:aws:iam::820242920762:role/GithubDeployMavisAndInfrastructure'
3232
|| 'arn:aws:iam::393416225559:role/GithubDeployMavisAndInfrastructure' }}
33-
tf-dir: terraform/app
33+
terraform-working-directory: terraform/app
3434

3535
jobs:
3636
plan-changes:
@@ -65,7 +65,7 @@ jobs:
6565
terraform_version: 1.10.5
6666
- name: Update the task definition
6767
id: plan
68-
working-directory: ${{ env.tf-dir }}
68+
working-directory: ${{ env.terraform-working-directory }}
6969
run: |
7070
terraform init -backend-config="env/${{ inputs.environment }}-backend.hcl" -upgrade
7171
terraform plan -var="image_digest=$DIGEST" -target=aws_ecs_task_definition.task_definition \
@@ -105,7 +105,7 @@ jobs:
105105
with:
106106
terraform_version: 1.10.5
107107
- name: Apply the changes
108-
working-directory: ${{ env.tf-dir }}
108+
working-directory: ${{ env.terraform-working-directory }}
109109
run: |
110110
terraform init -backend-config="env/${{ inputs.environment }}-backend.hcl" -upgrade
111111
terraform apply ${{ runner.temp }}/tfplan

.github/workflows/deploy-infrastructure.yml

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ env:
3131
aws_role: ${{ inputs.environment == 'production'
3232
&& 'arn:aws:iam::820242920762:role/GithubDeployMavisAndInfrastructure'
3333
|| 'arn:aws:iam::393416225559:role/GithubDeployMavisAndInfrastructure' }}
34-
tf_dir: terraform/app
34+
35+
defaults:
36+
run:
37+
working-directory: terraform/app
3538

3639
jobs:
3740
plan:
@@ -54,11 +57,8 @@ jobs:
5457
- name: Install AWS Cli
5558
run: sudo snap install --classic aws-cli
5659
- name: Check if any deployments are running
57-
working-directory: ${{ env.tf_dir }}
58-
run: |
59-
../scripts/check-for-running-deployments.sh ${{ inputs.environment }}
60+
run: ../scripts/check-for-running-deployments.sh ${{ inputs.environment }}
6061
- name: Get image digest
61-
working-directory: ${{ env.tf_dir }}
6262
run: |
6363
DIGEST="${{ inputs.docker_sha }}"
6464
if terraform state list | grep -q 'aws_ecs_task_definition.task_definition'; then
@@ -72,15 +72,14 @@ jobs:
7272
echo "DIGEST=$DIGEST" >> $GITHUB_ENV
7373
- name: Terraform Plan
7474
id: plan
75-
working-directory: ${{ env.tf_dir }}
7675
run: |
7776
set -e
7877
terraform plan -var="image_digest=$DIGEST" -var-file="env/${{ inputs.environment }}.tfvars" \
7978
-out ${{ runner.temp }}/tfplan | tee ${{ runner.temp }}/tf_stdout
8079
- name: Validate the changes
8180
run: |
8281
set -e
83-
./terraform/scripts/validate_plan.sh ${{ runner.temp }}/tf_stdout
82+
../scripts/validate_plan.sh ${{ runner.temp }}/tf_stdout
8483
- name: Upload artifact
8584
uses: actions/upload-artifact@v4
8685
with:
@@ -112,7 +111,6 @@ jobs:
112111
with:
113112
terraform_version: 1.10.5
114113
- name: Apply the changes
115-
working-directory: ${{ env.tf_dir }}
116114
run: |
117115
set -e
118116
terraform init -backend-config="env/${{ inputs.environment }}-backend.hcl" -upgrade

.github/workflows/destroy_infrastructure.yml renamed to .github/workflows/destroy-infrastructure.yml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
name: Destroy Infrastructure
2-
run-name: Destroy Infrastructure for ${{ inputs.environment }}
1+
name: Destroy infrastructure
2+
run-name: Destroy infrastructure for ${{ inputs.environment }}
33

44
on:
55
workflow_dispatch:
@@ -18,14 +18,17 @@ on:
1818

1919
env:
2020
aws_role: arn:aws:iam::393416225559:role/GithubDeployMavisAndInfrastructure
21-
tf_dir: terraform/app
2221

2322
jobs:
2423
destroy-resources:
24+
name: Destroy resources
2525
runs-on: ubuntu-latest
2626
permissions:
2727
id-token: write
2828
environment: ${{ inputs.environment }}
29+
defaults:
30+
run:
31+
working-directory: terraform/app
2932
steps:
3033
- name: Checkout code
3134
uses: actions/checkout@v4
@@ -39,7 +42,6 @@ jobs:
3942
with:
4043
terraform_version: 1.10.5
4144
- name: Ensure DB cluster can be deleted
42-
working-directory: ${{ env.tf_dir }}
4345
run: |
4446
set -e
4547
terraform init -backend-config="env/${{ inputs.environment }}-backend.hcl" -upgrade
@@ -53,11 +55,12 @@ jobs:
5355
fi
5456
5557
- name: Delete cluster
56-
working-directory: ${{ env.tf_dir }}
5758
run: |
5859
terraform destroy -var-file="env/${{ inputs.environment }}.tfvars" \
5960
-var="image_digest=notneededfordestroy" -auto-approve
61+
6062
destroy-backend:
63+
name: Destroy backend
6164
runs-on: ubuntu-latest
6265
needs: destroy-resources
6366
permissions:

0 commit comments

Comments
 (0)