generated from ministryofjustice/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 290
135 lines (117 loc) · 5.45 KB
/
core-network-services-deployment.yml
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
---
name: core-network-services-deployment
on:
push:
branches:
- main
paths:
- 'terraform/environments/core-network-services/**'
- 'terraform/modules/vpc-hub/**'
- 'terraform/modules/core-monitoring/**'
- 'terraform/modules/firewall-policy/**'
- '.github/workflows/core-network-services-deployment.yml'
pull_request:
branches:
- main
paths:
- 'terraform/environments/core-network-services/**'
- 'terraform/modules/vpc-hub/**'
- 'terraform/modules/core-monitoring/**'
- '.github/workflows/core-network-services-deployment.yml'
workflow_dispatch:
env:
TF_IN_AUTOMATION: true
AWS_REGION: "eu-west-2"
ENVIRONMENT_MANAGEMENT: ${{ secrets.MODERNISATION_PLATFORM_ENVIRONMENTS }}
permissions:
id-token: write # This is required for requesting the JWT
contents: read # This is required for actions/checkout
defaults:
run:
shell: bash
jobs:
core-network-services-deployment-plan:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
- name: Setup Go
uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4.1.0
with:
go-version: 1.21
- name: Set Account Number
run: echo "ACCOUNT_NUMBER=$(jq -r -e '.modernisation_platform_account_id' <<< $ENVIRONMENT_MANAGEMENT)" >> $GITHUB_ENV
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@010d0da01d0b5a38af31e9c3470dbfdabdecca3a # v4.0.1
with:
role-to-assume: "arn:aws:iam::${{ env.ACCOUNT_NUMBER }}:role/github-actions"
role-session-name: githubactionsrolesession
aws-region: ${{ env.AWS_REGION }}
- name: Set up Terraform
uses: hashicorp/setup-terraform@a1502cd9e758c50496cc9ac5308c4843bcd56d36 # v3.0.0
with:
terraform_version: "~1"
terraform_wrapper: false
- name: Run terraform plan in terraform/environments/core-network-services
run: |
git_dir=`git rev-parse --show-toplevel`
terraform --version
# Test if this is a PR or PULL event
if [ ! -z ${{ github.event.pull_request.number }} ]
then
#CONFIGURE TERRAFORM AND WORKSPACE
bash scripts/terraform-init.sh terraform/environments/core-network-services
terraform -chdir="terraform/environments/core-network-services" workspace select core-network-services-production
#RUN TERRATEST
pushd terraform/environments/core-network-services/test
go mod tidy
TEST=`go test | ${git_dir}/scripts/redact-output.sh | tee /dev/stderr | tail -n 1`
popd
TEST="> TERRATEST RESULT - core-network-services
${TEST}"
bash scripts/update-pr-comments.sh "${TEST}"
#RUN TERRAFORM PLAN
PLAN=`bash scripts/terraform-plan.sh terraform/environments/core-network-services | tee /dev/stderr | grep '^Plan: \|^No changes.'`
PLAN="> TERRAFORM PLAN RESULT - core-network-services
${PLAN}"
bash scripts/update-pr-comments.sh "${PLAN}"
fi
env:
SECRET: ${{ secrets.GITHUB_TOKEN }}
GITHUB_REPOSITORY: ${{ secrets.GITHUB_REPOSITORY }}
PULL_REQUEST_NUMBER: ${{ github.event.pull_request.number }}
core-network-services-deployment-apply:
runs-on: ubuntu-latest
if: github.event.ref == 'refs/heads/main'
needs: [core-network-services-deployment-plan]
steps:
- name: Checkout Repository
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
- name: Set Account Number
run: echo "ACCOUNT_NUMBER=$(jq -r -e '.modernisation_platform_account_id' <<< $ENVIRONMENT_MANAGEMENT)" >> $GITHUB_ENV
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@010d0da01d0b5a38af31e9c3470dbfdabdecca3a # v4.0.1
with:
role-to-assume: "arn:aws:iam::${{ env.ACCOUNT_NUMBER }}:role/github-actions"
role-session-name: githubactionsrolesession
aws-region: ${{ env.AWS_REGION }}
- name: Set up Terraform
uses: hashicorp/setup-terraform@a1502cd9e758c50496cc9ac5308c4843bcd56d36 # v3.0.0
with:
terraform_version: "~1"
terraform_wrapper: false
- name: Run Terraform apply in terraform/environments/core-network-services
run: |
terraform --version
bash scripts/terraform-init.sh terraform/environments/core-network-services
terraform -chdir="terraform/environments/core-network-services" workspace select core-network-services-production
bash scripts/terraform-apply.sh terraform/environments/core-network-services
- name: Slack failure notification
uses: slackapi/slack-github-action@e28cf165c92ffef168d23c5c9000cffc8a25e117 # v1.24.0
with:
payload: |
{"blocks":[{"type": "section","text": {"type": "mrkdwn","text": ":no_entry: Failed GitHub Action:"}},{"type": "section","fields":[{"type": "mrkdwn","text": "*Workflow:*\n<${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}|${{ github.workflow }}>"},{"type": "mrkdwn","text": "*Job:*\n${{ github.job }}"},{"type": "mrkdwn","text": "*Repo:*\n${{ github.repository }}"}]}]}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK
if: ${{ failure() }}