-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.yaml
105 lines (99 loc) · 3.42 KB
/
action.yaml
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
name: build
description: Build and update task definition
inputs:
aws_region:
default: ap-northeast-1
aws_role_arn:
require: true
git_user_name:
require: true
git_user_email:
require: true
git_personal_access_token:
require: true
deploy_ssh_key:
default: ""
task_definition_repo:
require: true
task_definition_repo_ref:
require: true
task_definition_file:
require: true
target:
require: true
build_args:
default: ""
ecr_repository:
require: true
gitsha_image_tag:
require: true
target_image_tag:
require: true
container_name:
require: true
runs:
using: "composite"
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Configure AWS Credentials From Web Identity
uses: aws-actions/configure-aws-credentials@v1-node16
with:
role-to-assume: ${{ inputs.aws_role_arn }}
aws-region: ${{ inputs.aws_region }}
- name: Login to ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
- name: Build, tag, and push image to ECR
id: build
shell: bash
run: |
# Image URL
GITSHA_IMAGE_URL=${{ steps.login-ecr.outputs.registry }}/${{ inputs.ecr_repository }}:${{ inputs.gitsha_image_tag }}
TARGET_IMAGE_URL=${{ steps.login-ecr.outputs.registry }}/${{ inputs.ecr_repository }}:${{ inputs.target_image_tag }}
# SSHエージェント起動
eval "$(ssh-agent)"
# SSHエージェントにキーを登録
# golangのビルド時に、private repositoryにあるパッケージを参照する際に必要
if [ "${{ inputs.deploy_ssh_key }}" != "" ]; then
echo "${{ inputs.deploy_ssh_key }}" > deploy_ssh_key
chmod 600 deploy_ssh_key
ssh-add -k deploy_ssh_key
fi
# Docker build
DOCKER_BUILDKIT=1 docker build \
--ssh default \
--target ${{ inputs.target }} \
${{ inputs.build_args }} \
-t $GITSHA_IMAGE_URL \
.
# Docker push
docker tag $GITSHA_IMAGE_URL $TARGET_IMAGE_URL
docker push $TARGET_IMAGE_URL
- name: Checkout task definition repository
uses: actions/checkout@v3
with:
repository: ${{ github.repository_owner }}/${{ inputs.task_definition_repo }}
ref: ${{ inputs.task_definition_repo_ref }}
token: ${{ inputs.git_personal_access_token }}
path: task-definitions
- name: Update task definition
uses: nick-fields/retry@v2
with:
max_attempts: 3
timeout_seconds: 15
retry_on: error
shell: bash
command: |
cd task-definitions
git config --local user.email ${{ inputs.git_user_email }}
git config --local user.name ${{ inputs.git_user_name }}
git config pull.rebase false
mv ${{ inputs.task_definition_file }} tmp.json
IMAGE_URL=${{ steps.login-ecr.outputs.registry }}/${{ inputs.ecr_repository }}:${{ inputs.target_image_tag }}
cat tmp.json | jq '(.containerDefinitions[] | select(.name == "${{ inputs.container_name }}") | .image) |= "'$IMAGE_URL'"' > ${{ inputs.task_definition_file }}
cat ${{ inputs.task_definition_file }}
git pull
git add ${{ inputs.task_definition_file }}
git commit -m "[UpdateImageTag] ${{ inputs.task_definition_file }} ${{ inputs.container_name }} ${{ inputs.target_image_tag }}"
git push