-
Notifications
You must be signed in to change notification settings - Fork 4
174 lines (149 loc) · 6.62 KB
/
deploy-account-wide-infra.yml
File metadata and controls
174 lines (149 loc) · 6.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
name: Deploy Account-wide infrastructure
run-name: Account-wide infra deployment to ${{ 'account-dev' }} of ${{ github.ref_name }} by ${{ github.actor }}
# An action environment would need
# name=acc-test
# ACCOUNT_NAME=test
# AWS_ACCOUNT_ID
# envs_to_pull: "qa" "ref" "int" "perftest" - use aws session assume.py - pull out
# OR json format: ["qa", "ref", "int", "perftest"] - feels better
# OR json obj lookup in repo-wide variable instead + same for below
# aws_account_id: 123456789 - get this from tf vars or something maybe? - use get_account_name script
# MGMT_ROLE_ARN is in repo secrets, so not needed in env
# Looping through envs to pull certs
on:
# workflow_dispatch:
# inputs:
# environment:
# description: "Environment to deploy to"
# required: true
# default: "account-dev"
# type: environment
# branch_name:
# description: Branch to deploy
# required: true
push:
branches:
- NRL-1595**
permissions:
id-token: write
contents: read
actions: write
jobs:
check-selected-environment:
name: Check Workflow Env
runs-on: codebuild-nhsd-nrlf-ci-build-project-${{ github.run_id }}-${{ github.run_attempt }}
steps:
- name: Validate environment
env:
IS_VALID_ENV: ${{ startsWith('account-dev', 'account-') }}
run: |
echo "valid workflow environment selected:" $IS_VALID_ENV
if [[ $IS_VALID_ENV == true ]]; then
exit 0
fi
exit 1
terraform-plan:
name: Terraform Plan - ${{ 'account-dev' }}
environment: ${{ 'account-dev' }}
needs: [check-selected-environment]
runs-on: codebuild-nhsd-nrlf-ci-build-project-${{ github.run_id }}-${{ github.run_attempt }}
steps:
- name: Git clone - ${{ github.ref_name }}
uses: actions/checkout@v4
with:
ref: ${{ github.ref_name }}
- name: Setup environment
run: |
echo "${HOME}/.asdf/bin" >> $GITHUB_PATH
poetry install --no-root
- name: Configure Management Credentials
uses: aws-actions/configure-aws-credentials@7474bc4690e29a8392af63c5b98e7449536d5c3a #v4.3.1
with:
aws-region: eu-west-2
role-to-assume: ${{ secrets.MGMT_ROLE_ARN }}
role-session-name: github-actions-ci-${{ 'account-dev' }}-${{ github.run_id }}
- name: Retrieve Server Certificates
env:
ACCOUNT_NAME: ${{ vars.ACCOUNT_NAME }}
run: |
make truststore-pull-all-for-account ACCOUNT=${ACCOUNT_NAME}
- name: Terraform Init
env:
ACCOUNT_NAME: ${{ vars.ACCOUNT_NAME }}
run: |
terraform -chdir=terraform/account-wide-infrastructure/${ACCOUNT_NAME} init
terraform -chdir=terraform/account-wide-infrastructure/${ACCOUNT_NAME} workspace new ${ACCOUNT_NAME} || \
terraform -chdir=terraform/account-wide-infrastructure/${ACCOUNT_NAME} workspace select ${ACCOUNT_NAME}
- name: Terraform Plan
env:
ACCOUNT_NAME: ${{ vars.ACCOUNT_NAME }}
ACCOUNT_ID: ${{ secrets.AWS_ACCOUNT_ID }}
run: |
terraform -chdir=terraform/account-wide-infrastructure/${ACCOUNT_NAME} plan \
-var assume_account=${ACCOUNT_ID} \
-var assume_role=terraform \
-out tfplan
- name: Save Terraform Plan
env:
ACCOUNT_NAME: ${{ vars.ACCOUNT_NAME }}
run: |
terraform -chdir=terraform/account-wide-infrastructure/${ACCOUNT_NAME} show -no-color tfplan > terraform/account-wide-infrastructure/$ACCOUNT_NAME/tfplan.txt
ls terraform/account-wide-infrastructure/$ACCOUNT_NAME/
aws s3 cp terraform/account-wide-infrastructure/$ACCOUNT_NAME/tfplan s3://nhsd-nrlf--mgmt--github-ci-logging/acc-$ACCOUNT_NAME/${{ github.run_id }}/tfplan
aws s3 cp terraform/account-wide-infrastructure/$ACCOUNT_NAME/tfplan.txt s3://nhsd-nrlf--mgmt--github-ci-logging/acc-$ACCOUNT_NAME/${{ github.run_id }}/tfplan.txt
terraform-apply:
name: Terraform Apply - ${{ 'account-dev' }}
needs: [terraform-plan]
runs-on: codebuild-nhsd-nrlf-ci-build-project-${{ github.run_id }}-${{ github.run_attempt }}
environment: ${{ 'account-dev' }}
steps:
- name: Git clone - ${{ github.ref_name }}
uses: actions/checkout@v4
with:
ref: ${{ github.ref_name }}
- name: Setup environment
run: |
echo "${HOME}/.asdf/bin" >> $GITHUB_PATH
poetry install --no-root
- name: Configure Management Credentials
uses: aws-actions/configure-aws-credentials@7474bc4690e29a8392af63c5b98e7449536d5c3a #v4.3.1
with:
aws-region: eu-west-2
role-to-assume: ${{ secrets.MGMT_ROLE_ARN }}
role-session-name: github-actions-ci-${{ 'account-dev' }}-${{ github.run_id}}
- name: Download Terraform Plan artifact
env:
ACCOUNT_NAME: ${{ vars.ACCOUNT_NAME }}
run: aws s3 cp s3://nhsd-nrlf--mgmt--github-ci-logging/acc-$ACCOUNT_NAME/${{ github.run_id }}/tfplan terraform/account-wide-infrastructure/tfplan
- name: Retrieve Server Certificates
env:
ACCOUNT_NAME: ${{ vars.ACCOUNT_NAME }}
run: |
make truststore-pull-all-for-account ACCOUNT=${ACCOUNT_NAME}
- name: Terraform Init
env:
ACCOUNT_NAME: ${{ vars.ACCOUNT_NAME }}
run: |
terraform -chdir=terraform/account-wide-infrastructure/${ACCOUNT_NAME} init
terraform -chdir=terraform/account-wide-infrastructure/${ACCOUNT_NAME} workspace new ${ACCOUNT_NAME} || \
terraform -chdir=terraform/account-wide-infrastructure/${ACCOUNT_NAME} workspace select ${ACCOUNT_NAME}
# - name: Terraform Apply
# env:
# AWS_ACCOUNT_ID: ${{ secrets.AWS_ACCOUNT_ID }}
# run: |
# terraform -chdir=terraform/account-wide-infrastructure apply tfplan \
# -var 'assume_account=AWS_ACCOUNT_ID' \
# -var 'assume_role=terraform'
# TODO: fix this
# # Is this where we'd burn commit & datetime into state?
# - name: Update environment config version
# env:
# ENVIRONMENT: ${{ 'account-dev' }}
# run: |
# deployed_version=$(terraform -chdir=terraform/account-wide-infrastructure/${ACCOUNT_NAME} output --raw version)
# poetry run python ./scripts/set_env_config.py inactive-version ${deployed_version} $ENVIRONMENT
# Slack notif: starting deploy of account-wide infra <branch deets>
# tf-plan: ensure output is visible in job output
# pre-apply: check current commit deployed in state
# post-apply: update current deployed commit in state
# no auto rollback