@@ -26,7 +26,7 @@ permissions:
2626
2727jobs :
2828 build-permissions :
29- name : Building permissions package for ${{ inputs.environment }}
29+ name : Build permissions for ${{ inputs.environment }}
3030 runs-on : [self-hosted, ci]
3131 environment : ${{ inputs.environment }}
3232
@@ -73,35 +73,140 @@ jobs:
7373 key : ${{ github.run_id }}-nrlf-permissions
7474 path : dist/nrlf_permissions.zip
7575
76- apply-permissions :
77- name : Applying permissions to ${{ inputs.environment }}
76+ pull-deployed-lambdas :
77+ name : Pull deployed lambdas for ${{ inputs.environment }}
7878 runs-on : [self-hosted, ci]
7979 environment : ${{ inputs.environment }}
8080
81- needs : build-permissions
81+ steps :
82+ - name : Git clone - ${{ github.ref }}
83+ uses : actions/checkout@v4
84+ with :
85+ ref : ${{ github.ref }}
86+
87+ - name : Configure Management Credentials
88+ uses : aws-actions/configure-aws-credentials@v4
89+ with :
90+ aws-region : eu-west-2
91+ role-to-assume : ${{ secrets.MGMT_ROLE_ARN }}
92+ role-session-name : github-actions-ci-${{ inputs.environment }}-${{ github.run_id }}
93+
94+ - name : Configure Account Role
95+ uses : aws-actions/configure-aws-credentials@v4
96+ with :
97+ aws-region : eu-west-2
98+ role-chaining : true
99+ role-to-assume : ${{ secrets.DEPLOY_ROLE_ARN }}
100+ role-session-name : github-actions-ci-acc-${{ inputs.environment }}-${{ github.run_id }}
101+
102+ - name : Pull deployed lambda artifacts
103+ run : |
104+ account=$(echo '${{ inputs.environment }}' | cut -d '-' -f1)
105+ ./scripts/pull-lambda-code-for-stack.sh ${{ inputs.stack_name }}
106+
107+ - name : Save lambda artifacts in cache
108+ uses : actions/cache/save@v4
109+ with :
110+ key : ${{ github.run_id }}-pulled-lambda-artifacts
111+ path : dist/*.zip
112+
113+ terraform-plan :
114+ name : Plan changes to ${{ inputs.environment }}
115+ runs-on : [self-hosted, ci]
116+ environment : ${{ inputs.environment }}
117+
118+ needs : [build-permissions, pull-deployed-lambdas]
82119
83120 steps :
84121 - name : Git clone - ${{ github.ref }}
85122 uses : actions/checkout@v4
86123 with :
87124 ref : ${{ github.ref }}
88125
126+ - name : Restore pulled lambda artifacts
127+ uses : actions/cache/restore@v4
128+ with :
129+ key : ${{ github.run_id }}-pulled-lambda-artifacts
130+ path : ./dist
131+ fail-on-cache-miss : true
132+
89133 - name : Restore NRLF permissions cache
90134 uses : actions/cache/restore@v4
91135 with :
92136 key : ${{ github.run_id }}-nrlf-permissions
93137 path : dist/nrlf_permissions.zip
94138 fail-on-cache-miss : true
95139
140+ - name : Configure Management Credentials
141+ uses : aws-actions/configure-aws-credentials@v4
142+ with :
143+ aws-region : eu-west-2
144+ role-to-assume : ${{ secrets.MGMT_ROLE_ARN }}
145+ role-session-name : github-actions-ci-${{ inputs.environment }}-${{ github.run_id }}
146+
96147 - name : Terraform Init
97148 run : |
98149 terraform -chdir=terraform/infrastructure init
99150 terraform -chdir=terraform/infrastructure workspace new ${{ inputs.stack_name }} || \
100151 terraform -chdir=terraform/infrastructure workspace select ${{ inputs.stack_name }}
101152
102- - name : Terraform Apply
153+ - name : Terraform Plan
103154 run : |
104- terraform -chdir=terraform/infrastructure apply -auto-approve \
155+ terraform -chdir=terraform/infrastructure plan \
105156 --var-file=etc/${{ vars.ACCOUNT_NAME }}.tfvars \
106157 --var assume_role_arn=${{ secrets.DEPLOY_ROLE_ARN }} \
107158 --var use_shared_resources=$(poetry run python scripts/are_resources_shared_for_stack.py ${{ inputs.stack_name }}) \
159+ --out tfplan
160+
161+ - name : Save Terraform Plan
162+ run : |
163+ terraform -chdir=terraform/infrastructure show -no-color tfplan > terraform/infrastructure/tfplan.txt
164+ aws s3 cp terraform/infrastructure/tfplan s3://nhsd-nrlf--mgmt--github-ci-logging/${{ inputs.environment }}/${{ github.run_id }}/tfplan
165+ aws s3 cp terraform/infrastructure/tfplan.txt s3://nhsd-nrlf--mgmt--github-ci-logging/${{ inputs.environment }}/${{ github.run_id }}/tfplan.txt
166+
167+ terraform-apply :
168+ name : Apply permissions to ${{ inputs.environment }}
169+ runs-on : [self-hosted, ci]
170+ environment : ${{ inputs.environment }}
171+
172+ needs : terraform-plan
173+
174+ steps :
175+ - name : Git clone - ${{ github.ref }}
176+ uses : actions/checkout@v4
177+ with :
178+ ref : ${{ github.ref }}
179+
180+ - name : Restore pulled lambda artifacts
181+ uses : actions/cache/restore@v4
182+ with :
183+ key : ${{ github.run_id }}-pulled-lambda-artifacts
184+ path : ./dist
185+ fail-on-cache-miss : true
186+
187+ - name : Restore NRLF permissions cache
188+ uses : actions/cache/restore@v4
189+ with :
190+ key : ${{ github.run_id }}-nrlf-permissions
191+ path : dist/nrlf_permissions.zip
192+ fail-on-cache-miss : true
193+
194+ - name : Configure Management Credentials
195+ uses : aws-actions/configure-aws-credentials@v4
196+ with :
197+ aws-region : eu-west-2
198+ role-to-assume : ${{ secrets.MGMT_ROLE_ARN }}
199+ role-session-name : github-actions-ci-${{ inputs.environment }}-${{ github.run_id }}
200+
201+ - name : Download Terraform Plan artifact
202+ run : aws s3 cp s3://nhsd-nrlf--mgmt--github-ci-logging/${{ inputs.environment }}/${{ github.run_id }}/tfplan terraform/infrastructure/tfplan
203+
204+ - name : Terraform Init
205+ run : |
206+ terraform -chdir=terraform/infrastructure init
207+ terraform -chdir=terraform/infrastructure workspace new ${{ inputs.stack_name }} || \
208+ terraform -chdir=terraform/infrastructure workspace select ${{ inputs.stack_name }}
209+
210+ - name : Terraform Apply
211+ run : |
212+ terraform -chdir=terraform/infrastructure apply tfplan
0 commit comments