-
Notifications
You must be signed in to change notification settings - Fork 0
190 lines (164 loc) · 5.91 KB
/
setup.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
name: Setup
on:
push:
branches:
- 'blue-green-deploy'
workflow_dispatch:
env:
aws_account_id: ${{ vars.AWS_ACCOUNT_ID }}
aws_region: ${{ vars.AWS_REGION }}
aws_role: ${{ vars.AWS_ROLE }}
permissions:
id-token: write
contents: read
jobs:
init:
uses: ./.github/workflows/build.yml
with:
aws_account_id: ${{ vars.AWS_ACCOUNT_ID }}
aws_region: ${{ vars.AWS_REGION }}
aws_role: ${{ vars.AWS_ROLE }}
repository_name: ${{ github.repository }}
service:
needs:
- init
runs-on: ubuntu-latest
env:
TF_VAR_task_definition_arn: ${{ needs.init.outputs.task_definition_arn }}
outputs:
cluster_name: ${{ steps.set-envs.outputs.CLUSTER_NAME }}
service_name: ${{ steps.set-envs.outputs.SERVICE_NAME }}
lb_listener_arn: ${{ steps.set-envs.outputs.LB_LISTENER_ARN }}
steps:
- uses: actions/checkout@v4
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-region: ${{ env.aws_region }}
role-to-assume: arn:aws:iam::${{ env.aws_account_id }}:role/${{ env.aws_role }}
role-session-name: GitHubActions
- name: Init
shell: bash
run: |
cd tf/service
terraform init
# - name: Deploy
# shell: bash
# id: deploy
# run: |
# cd tf/service
# terraform apply -auto-approve
# - name: Set env vars
# id: set-envs
# shell: bash
# run: |
# cd tf/service
# CLUSTER_NAME=$(terraform output -raw cluster_name)
# echo "CLUSTER_NAME=$CLUSTER_NAME" >> $GITHUB_OUTPUT
# SERVICE_NAME=$(terraform output -raw service_name)
# echo "SERVICE_NAME=$SERVICE_NAME" >> $GITHUB_OUTPUT
# LB_LISTENER_ARN=$(terraform output -raw lb_listener_arn)
# echo "LB_LISTENER_ARN=$LB_LISTENER_ARN" >> $GITHUB_OUTPUT
# network:
# needs: service
# runs-on: ubuntu-latest
# env:
# TF_VAR_load_balancer_listener_arn: ${{ needs.service.outputs.lb_listener_arn }}
# outputs:
# api_invoke_url: ${{ steps.set-envs.outputs.API_INVOKE_URL }}
# steps:
# - uses: actions/checkout@v4
# - name: Configure AWS Credentials
# uses: aws-actions/configure-aws-credentials@v4
# with:
# aws-region: ${{ env.aws_region }}
# role-to-assume: arn:aws:iam::${{ env.aws_account_id }}:role/${{ env.aws_role }}
# role-session-name: GitHubActions
# - name: Init
# shell: bash
# run: |
# cd tf/network
# terraform init
# - name: Deploy
# shell: bash
# id: deploy
# run: |
# cd tf/network
# terraform apply -auto-approve
# - name: Set env vars
# id: set-envs
# shell: bash
# run: |
# cd tf/network
# API_INVOKE_URL=$(terraform output -raw api_invoke_url)
# echo "API_INVOKE_URL=$API_INVOKE_URL" >> $GITHUB_OUTPUT
# check_image:
# needs:
# - task
# - service
# runs-on: ubuntu-latest
# steps:
# - name: Configure AWS Credentials
# uses: aws-actions/configure-aws-credentials@v4
# with:
# aws-region: ${{ env.aws_region }}
# role-to-assume: arn:aws:iam::${{ env.aws_account_id }}:role/${{ env.aws_role }}
# role-session-name: GitHubActions
# - name: Get current task definition
# id: task-def
# shell: bash
# run: |
# TASK_DEF_ARN=$(aws ecs describe-services \
# --cluster ${{ needs.service.outputs.cluster_name }} \
# --services ${{ needs.service.outputs.service_name }} \
# --region ${{ env.aws_region }} \
# --query 'services[0].taskDefinition' \
# --output text)
# echo "TASK_DEF_ARN=$TASK_DEF_ARN" >> $GITHUB_ENV
# - name: Get image from task definition
# id: check-image
# shell: bash
# run: |
# CURRENT_IMAGE=$(aws ecs describe-task-definition \
# --task-definition ${{ env.TASK_DEF_ARN }} \
# --region ${{ env.aws_region }} \
# --query 'taskDefinition.containerDefinitions[*].image' \
# --output text)
# echo "CURRENT_IMAGE=$CURRENT_IMAGE"
# echo "CURRENT_IMAGE=$CURRENT_IMAGE" >> $GITHUB_ENV
# - name: Compare with Terraform image
# id: compare
# run: |
# if [ "${{ env.CURRENT_IMAGE }}" != "${{ needs.task.outputs.image_uri }}" ]; then
# echo "ERROR: Image mismatch! Current image: ${{ env.CURRENT_IMAGE }}, Expected: ${{ needs.task.outputs.image_uri }}"
# exit 1
# else
# echo "Image matched: ${{ env.CURRENT_IMAGE }}"
# fi
# check_response:
# needs:
# - network
# - service
# runs-on: ubuntu-latest
# steps:
# - name: Make API request
# id: curl_request
# run: |
# RESPONSE=$(curl -s ${{ needs.network.outputs.api_invoke_url}}/host)
# echo "Response: $RESPONSE"
# echo "$RESPONSE" | jq
# - name: Assert API response contains expected fields
# run: |
# RESPONSE=$(curl -s ${{ needs.network.outputs.api_invoke_url}}/host)
# echo "$RESPONSE" | jq
# # Extract values from response
# MESSAGE=$(echo "$RESPONSE" | jq -r '.message')
# IMAGE_URI=$(echo "$RESPONSE" | jq -r '.imageUri')
# HOSTNAME=$(echo "$RESPONSE" | jq -r '.hostname')
# # Check if expected fields exist
# if [[ "$MESSAGE" == "null" || "$IMAGE_URI" == "null" || "$HOSTNAME" == "null" ]]; then
# echo "Error: Response does not contain expected fields."
# exit 1
# else
# echo "API response contains all expected fields."
# fi