-
Notifications
You must be signed in to change notification settings - Fork 0
163 lines (145 loc) · 6.52 KB
/
sub_deploy_to_prod.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
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
name: Sub - deploy to aws-prod
on:
workflow_call:
inputs:
IMAGE_TAG:
required: true
type: string
env:
KOSLI_DRY_RUN: ${{ vars.KOSLI_DRY_RUN }}
KOSLI_HOST: "https://app.kosli.com"
KOSLI_HOST_STAGING: "https://staging.app.kosli.com"
KOSLI_ORG: ${{ vars.KOSLI_ORG }} # cyber-dojo
KOSLI_FLOW: ${{ vars.KOSLI_FLOW }} # dashboard-ci
KOSLI_TRAIL: ${{ github.sha }}
SERVICE_NAME: ${{ github.event.repository.name }} # dashboard
jobs:
variables:
runs-on: ubuntu-latest
outputs:
service_name: ${{ steps.vars.outputs.service_name }}
image_tag: ${{ steps.vars.outputs.image_tag }}
aws_account_id: ${{ steps.vars.outputs.aws_account_id }}
ecr_registry: ${{ steps.vars.outputs.ecr_registry }}
aws_region: ${{ steps.vars.outputs.aws_region }}
gh_actions_iam_role_name: ${{ steps.vars.outputs.gh_actions_iam_role_name }}
steps:
- name: Prepare outputs for fivexl deployment workflow
id: vars
run: |
echo "service_name=${{ env.SERVICE_NAME }}" >> ${GITHUB_OUTPUT}
echo "image_tag=${{ inputs.IMAGE_TAG }}" >> ${GITHUB_OUTPUT}
echo "aws_account_id=274425519734" >> ${GITHUB_OUTPUT}
echo "ecr_registry=244531986313.dkr.ecr.eu-central-1.amazonaws.com" >> ${GITHUB_OUTPUT}
echo "aws_region=eu-central-1" >> ${GITHUB_OUTPUT}
echo "gh_actions_iam_role_name=gh_actions_services" >> ${GITHUB_OUTPUT}
deploy-to-aws-prod:
needs: [variables]
permissions:
id-token: write
contents: write
uses: fivexl/gh-workflow-tf-plan-apply/.github/workflows/base.yml@v0.0.18
with:
aws_region: ${{ needs.variables.outputs.aws_region }}
aws_role_arn: arn:aws:iam::${{ needs.variables.outputs.aws_account_id }}:role/${{ needs.variables.outputs.gh_actions_iam_role_name }}
aws_default_region: ${{ needs.variables.outputs.aws_region }}
aws_role_duration: 900
working_directory: deployment/terraform/
tf_apply: 'true'
tf_version: v1.4.5
tf_additional_env_vars: '{"TF_VAR_TAGGED_IMAGE": "${{ needs.variables.outputs.ecr_registry }}/${{ needs.variables.outputs.service_name }}:${{ needs.variables.outputs.image_tag }}"}'
# tf_sh_version: 0.2.1
# tf_upload_artifact_name_suffix: "_prod"
# tf_upload_artifact_state_file: 'true'
# tf_upload_artifact_plan: 'true'
# tf_state_file_dir_on_s3: "terraform/${{ needs.variables.outputs.service_name }}"
# tf_state_file_name: "${{ needs.variables.outputs.service_name }}.tfstate"
trail-attest-terraform:
needs: deploy-to-aws-prod
if: false # WIP
permissions:
actions: write
contents: read
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup kosli cli
uses: kosli-dev/setup-cli-action@v2
with:
version:
${{ vars.KOSLI_CLI_VERSION }}
- name: Download a plan artifact
uses: actions/download-artifact@v4.1.1
with:
name: tf_artifacts_plan_prod
- name: Download a state file artifact
uses: actions/download-artifact@v4.1.1
with:
name: tf_artifacts_state_file_prod
- name: Attest tf plan to Kosli trail (production)
run:
kosli attest generic
--name tf-apply-plan-prod
--flow ${{ env.KOSLI_FLOW }}
--trail ${{ env.KOSLI_TRAIL }}
--attachments ./tfplan
--host ${{ env.KOSLI_HOST }}
--api-token ${{ secrets.KOSLI_API_TOKEN }}
- name: Attest tf plan to Kosli trail (staging)
run:
kosli attest generic
--name tf-apply-plan-prod
--flow ${{ env.KOSLI_FLOW }}
--trail ${{ env.KOSLI_TRAIL }}
--attachments ./tfplan
--host ${{ env.KOSLI_HOST_STAGING }}
--api-token ${{ secrets.KOSLI_API_TOKEN_STAGING }}
- name: Delete tf plan artifact
run: |
# Get the list of artifacts
artifact_id=$(curl -s -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/${{ github.repository}}/actions/runs/${{ github.run_id }}/artifacts | jq -r '.artifacts[] | select(.name == "tf_artifacts_${{ matrix.environment }}").id')
# Delete each artifact
curl -L \
-X DELETE \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/${{ github.repository}}/actions/artifacts/${artifact_id}
- name: Attest tf state artifact (production)
run: |
kosli attest artifact \
--artifact-type file ./${{ needs.variables.outputs.service_name }}.tfstate \
--name state-prod \
--flow ${{ env.KOSLI_FLOW }} \
--trail ${{ env.KOSLI_TRAIL }} \
--host ${{ env.KOSLI_HOST }} \
--api-token ${{ secrets.KOSLI_API_TOKEN }}
- name: Attest tf state artifact (staging)
run: |
kosli attest artifact \
--artifact-type file ./${{ needs.variables.outputs.service_name }}.tfstate \
--name state-prod \
--flow ${{ env.KOSLI_FLOW }} \
--trail ${{ env.KOSLI_TRAIL }} \
--host ${{ env.KOSLI_HOST_STAGING }} \
--api-token ${{ secrets.KOSLI_API_TOKEN_STAGING }}
- name: Delete tf state file artifact
run: |
# Get the list of artifacts
artifact_id=$(curl -s -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/${{ github.repository}}/actions/runs/${{ github.run_id }}/artifacts | jq -r '.artifacts[] | select(.name == "tf_artifacts_state_file_${{ matrix.environment }}").id')
# Delete each artifact
curl -L \
-X DELETE \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/${{ github.repository}}/actions/artifacts/${artifact_id}