Skip to content

Commit 4fd0669

Browse files
authored
Merge pull request #3 from smalruby/feature/enable-automatic-deployment
feat: enable automatic deployment in CI/CD pipeline
2 parents 17e52ef + 9974c40 commit 4fd0669

File tree

2 files changed

+280
-32
lines changed

2 files changed

+280
-32
lines changed

.github/workflows/ci-cd.yml

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -69,35 +69,52 @@ jobs:
6969
path: .aws-sam/
7070
retention-days: 7
7171

72-
# Deploy job is temporarily disabled for initial testing
73-
# Will be enabled after manual deployment verification
74-
# deploy:
75-
# runs-on: ubuntu-latest
76-
# needs: [lint-and-test, validate-sam-template]
77-
# if: github.ref == 'refs/heads/main' && github.event_name == 'push'
78-
# environment: production
79-
# steps:
80-
# - name: Checkout code
81-
# uses: actions/checkout@v4
82-
# - name: Set up Ruby
83-
# uses: ruby/setup-ruby@v1
84-
# with:
85-
# ruby-version: '3.3'
86-
# bundler-cache: true
87-
# - name: Set up AWS SAM CLI
88-
# uses: aws-actions/setup-sam@v2
89-
# - name: Configure AWS credentials
90-
# uses: aws-actions/configure-aws-credentials@v4
91-
# with:
92-
# aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
93-
# aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
94-
# aws-region: ap-northeast-1
95-
# - name: Deploy to AWS
96-
# run: |
97-
# sam deploy \
98-
# --template-file .aws-sam/build/template.yaml \
99-
# --stack-name smalruby-infra-prod \
100-
# --parameter-overrides Stage=prod \
101-
# --capabilities CAPABILITY_IAM \
102-
# --no-confirm-changeset \
103-
# --no-fail-on-empty-changeset
72+
deploy:
73+
runs-on: ubuntu-latest
74+
needs: [lint-and-test, validate-sam-template]
75+
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
76+
environment: production
77+
permissions:
78+
id-token: write
79+
contents: read
80+
steps:
81+
- name: Checkout code
82+
uses: actions/checkout@v4
83+
84+
- name: Set up Ruby
85+
uses: ruby/setup-ruby@v1
86+
with:
87+
ruby-version: '3.3'
88+
bundler-cache: true
89+
90+
- name: Set up AWS SAM CLI
91+
uses: aws-actions/setup-sam@v2
92+
93+
- name: Configure AWS credentials
94+
uses: aws-actions/configure-aws-credentials@v4
95+
with:
96+
role-to-assume: ${{ secrets.AWS_ROLE_ARN }}
97+
role-session-name: GitHubActions-smalruby-infra-deploy
98+
aws-region: ap-northeast-1
99+
100+
- name: Build SAM application
101+
run: sam build --template template.yaml
102+
103+
- name: Deploy to AWS
104+
run: |
105+
sam deploy \
106+
--template-file .aws-sam/build/template.yaml \
107+
--stack-name smalruby-infra-prod \
108+
--parameter-overrides Stage=prod \
109+
--capabilities CAPABILITY_IAM \
110+
--resolve-s3 \
111+
--no-confirm-changeset \
112+
--no-fail-on-empty-changeset
113+
114+
- name: Get deployment outputs
115+
run: |
116+
echo "=== Deployment Outputs ==="
117+
aws cloudformation describe-stacks \
118+
--stack-name smalruby-infra-prod \
119+
--query 'Stacks[0].Outputs[*].{Key:OutputKey,Value:OutputValue}' \
120+
--output table

OIDC_SETUP.md

Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
# GitHub Actions用AWS OIDC設定手順
2+
3+
## 概要
4+
5+
GitHub ActionsからAWSリソースにアクセスする際、従来のIAMユーザーのアクセスキー・シークレットキーの代わりに、OIDC(OpenID Connect)を使用した一時的な認証を行う設定手順です。
6+
7+
これにより以下の利点があります:
8+
- **セキュリティ向上**: 長期間有効なクレデンシャルを保存する必要がない
9+
- **自動ローテーション**: トークンは短期間で自動的に無効化される
10+
- **最小権限原則**: 特定のリポジトリ・ブランチからのみアクセス可能
11+
12+
## 手順1: AWS Identity Provider(OIDC)の作成
13+
14+
### 1.1 AWSマネジメントコンソールにログイン
15+
- IAM サービスに移動
16+
17+
### 1.2 Identity Provider作成
18+
1. **左メニュー「Identity providers」をクリック**
19+
2. **「Add provider」ボタンをクリック**
20+
3. **Provider type**: 「OpenID Connect」を選択
21+
4. **Provider URL**: `https://token.actions.githubusercontent.com` を入力
22+
5. **Audience**: `sts.amazonaws.com` を入力
23+
6. **「Get thumbprint」をクリック**して証明書のサムプリントを取得
24+
7. **「Add provider」をクリック**
25+
26+
## 手順2: IAMロールの作成
27+
28+
### 2.1 新しいロール作成
29+
1. **IAM → Roles → 「Create role」**
30+
2. **Trusted entity type**: 「Web identity」を選択
31+
3. **Identity provider**: 先ほど作成したGitHub OIDCプロバイダーを選択
32+
4. **Audience**: `sts.amazonaws.com` を選択
33+
34+
### 2.2 信頼関係の設定
35+
**「Next」をクリック後、信頼関係を以下のように設定:**
36+
37+
```json
38+
{
39+
"Version": "2012-10-17",
40+
"Statement": [
41+
{
42+
"Effect": "Allow",
43+
"Principal": {
44+
"Federated": "arn:aws:iam::007325983811:oidc-provider/token.actions.githubusercontent.com"
45+
},
46+
"Action": "sts:AssumeRole",
47+
"Condition": {
48+
"StringEquals": {
49+
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com"
50+
},
51+
"StringLike": {
52+
"token.actions.githubusercontent.com:sub": "repo:smalruby/smalruby-infra:ref:refs/heads/main"
53+
}
54+
}
55+
}
56+
]
57+
}
58+
```
59+
60+
**重要**: `007325983811` は実際のAWSアカウントIDに置き換えてください。
61+
62+
### 2.3 権限ポリシーの追加
63+
デプロイに必要な権限を持つポリシーを添付します:
64+
65+
#### 2.3.1 Lambda関数管理権限
66+
```json
67+
{
68+
"Version": "2012-10-17",
69+
"Statement": [
70+
{
71+
"Effect": "Allow",
72+
"Action": [
73+
"lambda:CreateFunction",
74+
"lambda:UpdateFunctionCode",
75+
"lambda:UpdateFunctionConfiguration",
76+
"lambda:DeleteFunction",
77+
"lambda:GetFunction",
78+
"lambda:ListFunctions",
79+
"lambda:AddPermission",
80+
"lambda:RemovePermission"
81+
],
82+
"Resource": "*"
83+
}
84+
]
85+
}
86+
```
87+
88+
#### 2.3.2 API Gateway管理権限
89+
```json
90+
{
91+
"Version": "2012-10-17",
92+
"Statement": [
93+
{
94+
"Effect": "Allow",
95+
"Action": [
96+
"apigateway:*"
97+
],
98+
"Resource": "*"
99+
}
100+
]
101+
}
102+
```
103+
104+
#### 2.3.3 CloudFormation管理権限
105+
```json
106+
{
107+
"Version": "2012-10-17",
108+
"Statement": [
109+
{
110+
"Effect": "Allow",
111+
"Action": [
112+
"cloudformation:CreateStack",
113+
"cloudformation:UpdateStack",
114+
"cloudformation:DeleteStack",
115+
"cloudformation:DescribeStacks",
116+
"cloudformation:DescribeStackEvents",
117+
"cloudformation:DescribeStackResources",
118+
"cloudformation:GetTemplate",
119+
"cloudformation:ValidateTemplate",
120+
"cloudformation:CreateChangeSet",
121+
"cloudformation:DescribeChangeSet",
122+
"cloudformation:ExecuteChangeSet",
123+
"cloudformation:DeleteChangeSet"
124+
],
125+
"Resource": "*"
126+
}
127+
]
128+
}
129+
```
130+
131+
#### 2.3.4 S3とIAM権限
132+
```json
133+
{
134+
"Version": "2012-10-17",
135+
"Statement": [
136+
{
137+
"Effect": "Allow",
138+
"Action": [
139+
"s3:CreateBucket",
140+
"s3:DeleteBucket",
141+
"s3:GetObject",
142+
"s3:PutObject",
143+
"s3:DeleteObject",
144+
"s3:ListBucket",
145+
"s3:GetBucketLocation",
146+
"s3:GetBucketVersioning",
147+
"s3:PutBucketVersioning"
148+
],
149+
"Resource": [
150+
"arn:aws:s3:::aws-sam-cli-managed-default-samclisourcebucket-*",
151+
"arn:aws:s3:::aws-sam-cli-managed-default-samclisourcebucket-*/*"
152+
]
153+
},
154+
{
155+
"Effect": "Allow",
156+
"Action": [
157+
"iam:CreateRole",
158+
"iam:DeleteRole",
159+
"iam:GetRole",
160+
"iam:PassRole",
161+
"iam:AttachRolePolicy",
162+
"iam:DetachRolePolicy",
163+
"iam:CreatePolicy",
164+
"iam:DeletePolicy",
165+
"iam:GetPolicy"
166+
],
167+
"Resource": "*"
168+
}
169+
]
170+
}
171+
```
172+
173+
### 2.4 ロール名設定
174+
- **Role name**: `GitHubActions-smalruby-infra-deploy` (推奨)
175+
- **Description**: `Role for GitHub Actions to deploy smalruby infrastructure`
176+
177+
### 2.5 ロール作成完了
178+
「Create role」をクリックしてロールを作成します。
179+
180+
## 手順3: GitHub Secretsの設定
181+
182+
### 3.1 リポジトリのSecrets設定
183+
1. **GitHubリポジトリ「smalruby/smalruby-infra」に移動**
184+
2. **Settings → Secrets and variables → Actions**
185+
3. **「New repository secret」をクリック**
186+
187+
### 3.2 必要なSecret
188+
以下のSecretを追加:
189+
190+
| Secret名 || 説明 |
191+
|----------|---|------|
192+
| `AWS_ROLE_ARN` | `arn:aws:iam::007325983811:role/GitHubActions-smalruby-infra-deploy` | 作成したIAMロールのARN |
193+
194+
**注意**: `007325983811` は実際のAWSアカウントIDに置き換えてください。
195+
196+
### 3.3 従来のSecretsの削除(推奨)
197+
OIDCが正常に動作することを確認後、以下の従来のSecretsは削除できます:
198+
- `AWS_ACCESS_KEY_ID`
199+
- `AWS_SECRET_ACCESS_KEY`
200+
201+
## 手順4: デプロイテスト
202+
203+
### 4.1 GitHub Actionsの実行
204+
mainブランチにpushしてGitHub Actionsが正常に実行されることを確認します。
205+
206+
### 4.2 ログの確認
207+
GitHub Actions実行ログで以下を確認:
208+
- OIDC認証が成功している
209+
- AWS CLIコマンドが正常に実行されている
210+
- デプロイが完了している
211+
212+
## 設定完了後の利点
213+
214+
**セキュリティ向上**: 長期クレデンシャルの漏洩リスクなし
215+
**自動管理**: トークンの自動ローテーション
216+
**アクセス制御**: 特定リポジトリ・ブランチからのみアクセス可能
217+
**監査**: CloudTrailでアクセスログが記録される
218+
219+
## トラブルシューティング
220+
221+
### エラー例1: "AssumeRoleFailure"
222+
**原因**: 信頼関係の設定が正しくない
223+
**対処**: ロールの信頼関係でリポジトリ名・ブランチ名を確認
224+
225+
### エラー例2: "Access Denied"
226+
**原因**: ロールに必要な権限がない
227+
**対処**: ロールに適切なポリシーが添付されているか確認
228+
229+
### エラー例3: "Invalid identity token"
230+
**原因**: GitHub Actionsの設定が正しくない
231+
**対処**: `permissions`セクションに`id-token: write`があるか確認

0 commit comments

Comments
 (0)