-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathaction.yml
177 lines (166 loc) · 6.34 KB
/
action.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
name: "Run ECS task"
description: "GitHub Action to run a task on Amazon ECS and output CloudWatch Logs"
author: yyoshiki41
branding:
icon: "cloud"
color: "orange"
inputs:
region:
description: "AWS Region"
required: false
default: "ap-northeast-1"
task-definition:
required: true
description: "The path to the task definition file"
task-definition-family:
description: "The name for your task definition"
required: true
launch-type:
description: "Launch type"
required: false
default: "FARGATE"
cluster:
description: "ECS Cluster name"
required: true
subnets:
description: "awsvpcConfiguration values: ['subnet-00', ...]"
required: true
security-groups:
description: "awsvpcConfiguration values: ['sg-00', ...]"
required: true
assign-public-ip:
description: "awsvpcConfiguration values"
required: false
default: "DISABLED"
container-name:
description: "The name of the container that run command. This parameter is required if any override is specified."
required: false
command:
description: "Overrides the default command from the Docker image or the task definition. This parameter is required if any override is specified."
required: false
outputs:
task-id:
description: "Task ID"
value: ${{ steps.run-task.outputs.task_id }}
revision:
description: "Task definition's revision number"
value: ${{ steps.task-def-register.outputs.revision }}
logs:
description: "CloudWatch Logs"
value: ${{ steps.logs.outputs.out }}
runs:
using: "composite"
steps:
- name: Install AWS CLI
shell: bash
run: |
if ! [ -x "$(command -v aws)" ]; then
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install --update
awscli --version
fi
- name: Register ECS task definition
shell: bash
id: task-def-register
run: |
revision=$(aws ecs register-task-definition \
--region ${{ inputs.region }} \
--family ${{ inputs.task-definition-family }} \
--cli-input-json file://${{ inputs.task-definition }} \
--query taskDefinition.revision)
echo "revision=$revision" >> $GITHUB_OUTPUT
- name: Run ECS task
shell: bash
id: run-task
env:
vpc_configuration: "{subnets=${{ inputs.subnets }},securityGroups=${{ inputs.security-groups }},assignPublicIp=${{ inputs.assign-public-ip }}}"
container_overrides: '{"containerOverrides": [{"name": "${{ inputs.container-name}}", "command": ${{ inputs.command }} }]}'
run: |
if [ '${{ inputs.container-name }}' != '' ] && [ '${{ inputs.command }}' != '' ]; then
task_arn=$(aws ecs run-task \
--region ${{ inputs.region }} \
--launch-type ${{ inputs.launch-type }} \
--cluster ${{ inputs.cluster }} \
--network-configuration 'awsvpcConfiguration=${{ env.vpc_configuration }}' \
--task-definition ${{ inputs.task-definition-family }}:${{ steps.task-def-register.outputs.revision }} \
--overrides '${{ env.container_overrides }}' \
--query tasks[0].taskArn --output text)
else
task_arn=$(aws ecs run-task \
--region ${{ inputs.region }} \
--launch-type ${{ inputs.launch-type }} \
--cluster ${{ inputs.cluster }} \
--network-configuration 'awsvpcConfiguration=${{ env.vpc_configuration }}' \
--task-definition ${{ inputs.task-definition-family }}:${{ steps.task-def-register.outputs.revision }} \
--query tasks[0].taskArn --output text)
fi
echo "task_arn=$task_arn" >> $GITHUB_OUTPUT
echo "task_id=${task_arn/*\//}" >> $GITHUB_OUTPUT
- name: Wait until a task stopped
shell: bash
env:
max_attempt_count: 3
run: |
attempt_count=0
while [ $attempt_count -lt ${{ env.max_attempt_count }} ]
do
aws ecs wait tasks-stopped \
--cluster ${{ inputs.cluster }} \
--tasks ${{ steps.run-task.outputs.task_arn }}
task_status=$(aws ecs describe-tasks \
--cluster ${{ inputs.cluster }} \
--tasks ${{ steps.run-task.outputs.task_arn }} \
--query "tasks[0].lastStatus" \
--output text)
if [ "${task_status}" = "STOPPED" ];then
break
fi
echo "Waiting for the task to stop... (attempt: $attempt_count)"
attempt_count=$((attempt_count+1))
done
- name: Install ecs-cli
shell: bash
env:
ecs_cli_version: v1.21.0
run: |
sudo curl -Lo /usr/local/bin/ecs-cli https://amazon-ecs-cli.s3.amazonaws.com/ecs-cli-linux-amd64-${{ env.ecs_cli_version }}
sudo chmod +x /usr/local/bin/ecs-cli
- name: Retrieve container logs from CloudWatch logs
shell: bash
id: logs
run: |
echo "::group::Retrieve container logs from CloudWatch logs"
echo '::echo::on'
if [ '${{ inputs.container-name }}' != '' ]; then
out=$(ecs-cli logs --timestamps \
--cluster ${{ inputs.cluster }} \
--task-id ${{ steps.run-task.outputs.task_id }} \
--task-def ${{ inputs.task-definition-family }}:${{ steps.task-def-register.outputs.revision }} \
--container-name ${{ inputs.container-name }})
else
out=$(ecs-cli logs --timestamps \
--cluster ${{ inputs.cluster }} \
--task-id ${{ steps.run-task.outputs.task_id }} \
--task-def ${{ inputs.task-definition-family }}:${{ steps.task-def-register.outputs.revision }})
fi
out="${out//'%'/'%25'}"
out="${out//$'\n'/'%0A'}"
out="${out//$'\r'/'%0D'}"
out="${out//$'^'/'d'}"
echo "out=$out" >> $GITHUB_OUTPUT
echo "::endgroup::"
- name: Check exit code in tasks
shell: bash
id: task-exit-code
run: |
exitCode=$(aws ecs describe-tasks \
--region ${{ inputs.region }} \
--cluster ${{ inputs.cluster }} \
--tasks ${{ steps.run-task.outputs.task_arn }} \
--query tasks[0].containers[0].exitCode \
--output text)
if [ "$exitCode" != "0" ]; then
echo "detected run-task failure"
exit $exitCode
fi