Skip to content

Commit ca72891

Browse files
committed
feat: 🎸 deploy from github actions
1 parent 6c1d0e4 commit ca72891

File tree

6 files changed

+112
-13
lines changed

6 files changed

+112
-13
lines changed

‎.github/workflows/deploy.yaml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: deploy benchmarker
2+
on:
3+
push:
4+
branches:
5+
- feat/deploy.bench
6+
workflow_dispatch:
7+
inputs:
8+
TAG:
9+
description: 'tag'
10+
required: true
11+
jobs:
12+
bench:
13+
- uses: actions/checkout@v4
14+
- name: Configure AWS credentials from IAM Role
15+
uses: aws-actions/configure-aws-credentials@v4
16+
with:
17+
role-to-assume: ${{ vars.AWS_DEPLOY_ROLE_ARN }}
18+
aws-region: ap-northeast-1
19+
- uses: kayac/ecspresso@v2
20+
with:
21+
version: latest
22+
- name: deploy
23+
working-directory: ecs
24+
run: |
25+
ecspresso deploy --config benchmarker.yml
26+
env:
27+
TAG: ${{ github.event.inputs.TAG || 'latest' }}
28+
ENV: stg

‎.github/workflows/nop.yaml

Lines changed: 0 additions & 10 deletions
This file was deleted.

‎terraform/modules/github/main.tf

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ resource "aws_iam_openid_connect_provider" "github_actions" {
1212
thumbprint_list = data.tls_certificate.github_actions.certificates[*].sha1_fingerprint
1313
}
1414

15-
resource "aws_iam_role" "main" {
16-
name = "github-actions-ecr-push-example-role"
15+
resource "aws_iam_role" "push_image" {
16+
name = "github-actions-ecr-push-role"
1717
assume_role_policy = data.aws_iam_policy_document.main_assume_role_policy.json
1818
}
1919

@@ -46,7 +46,7 @@ data "aws_iam_policy_document" "main_assume_role_policy" {
4646

4747
resource "aws_iam_role_policy" "main" {
4848
name = "allow-ecr-push-image"
49-
role = aws_iam_role.main.name
49+
role = aws_iam_role.push_image.name
5050
policy = data.aws_iam_policy_document.main_policy.json
5151
}
5252

@@ -73,3 +73,61 @@ data "aws_iam_policy_document" "main_policy" {
7373
resources = var.ecr_repositories
7474
}
7575
}
76+
77+
resource "aws_iam_role" "update_taskdef" {
78+
name = "github-actions-ecs-update-taskdef-role"
79+
assume_role_policy = data.aws_iam_policy_document.update_taskdef_assume_role_policy.json
80+
}
81+
82+
data "aws_iam_policy_document" "update_taskdef_assume_role_policy" {
83+
statement {
84+
effect = "Allow"
85+
actions = ["sts:AssumeRoleWithWebIdentity"]
86+
87+
principals {
88+
type = "Federated"
89+
identifiers = [aws_iam_openid_connect_provider.github_actions.arn]
90+
}
91+
92+
condition {
93+
test = "StringEquals"
94+
variable = "token.actions.githubusercontent.com:aud"
95+
values = ["sts.amazonaws.com"]
96+
}
97+
98+
dynamic "condition" {
99+
for_each = var.github_repos
100+
content {
101+
test = "StringLike"
102+
variable = "token.actions.githubusercontent.com:sub"
103+
values = ["repo:isucon/${condition.value}:*"]
104+
}
105+
}
106+
}
107+
}
108+
109+
resource "aws_iam_role_policy" "update_taskdef" {
110+
name = "allow-ecs-update-taskdef"
111+
role = aws_iam_role.update_taskdef.name
112+
policy = data.aws_iam_policy_document.update_taskdef_policy.json
113+
}
114+
115+
data "aws_iam_policy_document" "update_taskdef_policy" {
116+
statement {
117+
effect = "Allow"
118+
actions = ["ecs:RegisterTaskDefinition"]
119+
resources = ["*"]
120+
}
121+
122+
statement {
123+
effect = "Allow"
124+
actions = ["iam:PassRole"]
125+
resources = var.task_role_arns
126+
}
127+
128+
statement {
129+
effect = "Allow"
130+
actions = ["ecs:UpdateService", "ecs:DescribeServices"]
131+
resources = var.service_arns
132+
}
133+
}

‎terraform/modules/github/variables.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,11 @@ variable "github_repos" {
55
variable "ecr_repositories" {
66
type = list(string)
77
}
8+
9+
variable "task_role_arns" {
10+
type = list(string)
11+
}
12+
13+
variable "service_arns" {
14+
type = list(string)
15+
}

‎terraform/modules/isuxportal/output.tf

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,16 @@ output "ecr_repositories" {
55
aws_ecr_repository.benchmarker.arn,
66
]
77
}
8+
9+
output "task_role_arns" {
10+
value = [
11+
aws_iam_role.ecs-task.arn,
12+
]
13+
}
14+
15+
output "service_arns" {
16+
value = [
17+
"${aws_ecs_cluster.main.arn}/*",
18+
"${aws_ecs_cluster.benchmarker.arn}/*",
19+
]
20+
}

‎terraform/stg/main.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ module "github" {
4646
source = "../modules/github"
4747
github_repos = ["isucon14", "isucon14-portal"]
4848
ecr_repositories = module.isuxportal.ecr_repositories
49+
service_arns = module.isuxportal.service_arns
50+
task_role_arns = module.isuxportal.task_role_arns
4951
}
5052

5153
module "ami" {

0 commit comments

Comments
 (0)