generated from ministryofjustice/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 291
131 lines (120 loc) · 5.45 KB
/
core-logging-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
---
name: core-logging-deployment
on:
push:
branches:
- main
paths:
- 'terraform/environments/core-logging/**'
- 'terraform/modules/vpc-hub/**'
- 'terraform/modules/core-monitoring/**'
- '.github/workflows/core-logging-deployment.yml'
pull_request:
branches:
- main
paths:
- 'terraform/environments/core-logging/**'
- 'terraform/modules/vpc-hub/**'
- 'terraform/modules/core-monitoring/**'
- '.github/workflows/core-logging-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-logging-deployment-plan:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab # v3.5.2
- 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@e1e17a757e536f70e52b5a12b2e8d1d1c60e04ef # v2.0.0
with:
role-to-assume: "arn:aws:iam::${{ env.ACCOUNT_NUMBER }}:role/github-actions"
role-session-name: githubactionsrolesession
aws-region: ${{ env.AWS_REGION }}
- uses: hashicorp/setup-terraform@633666f66e0061ca3b725c73b2ec20cd13a8fdd1 # v2.0.3
with:
terraform_version: "~1"
terraform_wrapper: false
- name: Run terraform plan in terraform/environments/core-logging
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-logging
terraform -chdir="terraform/environments/core-logging" workspace select core-logging-production
#RUN TERRATEST
pushd terraform/environments/core-logging/test
# Install GOLANG 1.20.3
if [ `whoami` != "runner" ]
then
wget -q https://dl.google.com/go/go1.20.3.linux-amd64.tar.gz
tar -zxvf go1.20.3.linux-amd64.tar.gz
sudo mv go /usr/local
echo "-- running newly installed go --"
TEST=`/usr/local/go/bin/go test | ${git_dir}/scripts/redact-output.sh | tee /dev/stderr | tail -n 1`
else
echo "-- running native go --"
TEST=`go test | ${git_dir}/scripts/redact-output.sh | tee /dev/stderr | tail -n 1`
fi
popd
TEST="> TERRATEST RESULT - core-logging
${TEST}"
bash scripts/update-pr-comments.sh "${TEST}"
#RUN TERRAFORM PLAN
PLAN=`bash scripts/terraform-plan.sh terraform/environments/core-logging | tee /dev/stderr | grep '^Plan: \|^No changes.'`
PLAN="> TERRAFORM PLAN RESULT - core-logging
${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-logging-deployment-apply:
runs-on: ubuntu-latest
if: github.event.ref == 'refs/heads/main'
needs: [core-logging-deployment-plan]
steps:
- name: Checkout Repository
uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab # v3.5.2
- 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@e1e17a757e536f70e52b5a12b2e8d1d1c60e04ef # v2.0.0
with:
role-to-assume: "arn:aws:iam::${{ env.ACCOUNT_NUMBER }}:role/github-actions"
role-session-name: githubactionsrolesession
aws-region: ${{ env.AWS_REGION }}
- uses: hashicorp/setup-terraform@633666f66e0061ca3b725c73b2ec20cd13a8fdd1 # v2.0.3
with:
terraform_version: "~1"
terraform_wrapper: false
- name: Run Terraform apply in terraform/environments/core-logging
run: |
terraform --version
bash scripts/terraform-init.sh terraform/environments/core-logging
terraform -chdir="terraform/environments/core-logging" workspace select core-logging-production
bash scripts/terraform-apply.sh terraform/environments/core-logging
- 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() }}