@@ -2,15 +2,6 @@ name: Deploy infrastructure
22run-name : Deploy infrastructure for ${{ inputs.environment }}
33
44on :
5- workflow_call :
6- inputs :
7- environment :
8- description : Deployment environment
9- required : true
10- type : string
11- git_ref_to_deploy :
12- required : true
13- type : string
145 workflow_dispatch :
156 inputs :
167 environment :
@@ -26,7 +17,6 @@ permissions: {}
2617
2718env :
2819 environment : ${{ inputs.environment }}
29- image_tag : ${{ inputs.image_tag || github.sha }}
3020 git_ref_to_deploy : ${{ inputs.git_ref_to_deploy || github.ref_name }}
3121 aws_role : ${{ inputs.environment == 'production'
3222 && 'arn:aws:iam::820242920762:role/GithubDeployMavisAndInfrastructure'
@@ -42,9 +32,92 @@ defaults:
4232 working-directory : terraform/app
4333
4434jobs :
35+
36+ validate-inputs :
37+ runs-on : ubuntu-latest
38+ permissions : {}
39+ steps :
40+ - name : Validate inputs
41+ run : |
42+ if [[ "$environment" == "preview" || "$environment" == "production" ]]; then
43+ if [[ -z "$git_ref_to_deploy" ]]; then
44+ echo "Error: git_ref_to_deploy is required for preview and production environments."
45+ exit 1
46+ fi
47+ fi
48+ determine-git-sha :
49+ runs-on : ubuntu-latest
50+ permissions : {}
51+ needs : validate-inputs
52+ outputs :
53+ git-sha : ${{ steps.get-git-sha.outputs.git-sha }}
54+ steps :
55+ - name : Checkout code
56+ uses : actions/checkout@v6
57+ with :
58+ ref : ${{ inputs.git_ref_to_deploy || github.sha }}
59+ - name : Get git sha
60+ id : get-git-sha
61+ run : echo "git-sha=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT
62+ validate-permissions :
63+ needs : validate-inputs
64+ runs-on : ubuntu-latest
65+ defaults :
66+ run :
67+ working-directory : ./terraform
68+ permissions :
69+ id-token : write
70+ steps :
71+ - name : Checkout code
72+ uses : actions/checkout@v6
73+ with :
74+ ref : ${{ github.ref_name }}
75+ - name : Configure AWS Credentials
76+ uses : aws-actions/configure-aws-credentials@v5
77+ with :
78+ role-to-assume : arn:aws:iam::${{ env.account_id }}:role/GithubDeployMavisAndInfrastructure
79+ aws-region : eu-west-2
80+ - name : Compare permissions
81+ id : compare-permissions
82+ run : |
83+ source ./scripts/validate-github-actions-policy.sh
84+ validate_policies "arn:aws:iam::$account_id:policy/DeployMavisResources" ./account/resources/iam_policy_DeployMavisResources.json
85+ exit $?
86+ update-permissions :
87+ runs-on : ubuntu-latest
88+ needs : validate-permissions
89+ if : ${{ !cancelled() && (inputs.environment == 'production' || inputs.environment == 'preview') && needs.validate-permissions.result == 'failure' }}
90+ environment : ${{ inputs.environment }}
91+ defaults :
92+ run :
93+ working-directory : ./terraform
94+ permissions :
95+ id-token : write
96+ steps :
97+ - name : Checkout code
98+ uses : actions/checkout@v6
99+ with :
100+ ref : ${{ github.ref_name }}
101+ - name : Configure AWS Credentials
102+ uses : aws-actions/configure-aws-credentials@v5
103+ with :
104+ role-to-assume : arn:aws:iam::${{ env.account_id }}:role/GithubDeployMavisAndInfrastructure
105+ aws-region : eu-west-2
106+ - name : Update IAM policy
107+ run : ./scripts/update-github-actions-policy.sh "arn:aws:iam::$account_id:policy/DeployMavisResources" ./account/resources/iam_policy_DeployMavisResources.json
108+
45109 plan :
46110 name : Terraform plan
47111 runs-on : ubuntu-latest
112+ needs :
113+ [
114+ determine-git-sha,
115+ validate-permissions,
116+ update-permissions,
117+ ]
118+ if : ${{ !cancelled() &&
119+ ((inputs.environment != 'production' && inputs.environment != 'preview') ||
120+ needs.validate-permissions.result == 'success' || needs.update-permissions.result == 'success') }}
48121 permissions :
49122 id-token : write
50123 steps :
85158 name : Terraform apply
86159 runs-on : ubuntu-latest
87160 needs : plan
161+ if : ${{ !cancelled() && needs.plan.result == 'success' }}
88162 environment : ${{ inputs.environment }}
89163 permissions :
90164 id-token : write
0 commit comments