初めに
SSGで良いサービスならAWS copilotでStatic SiteにすればECSとかに比べればかなりお得だし、github actionsとか作らなくて良いからめっちゃ楽じゃんって思ったけど、いまいち情報が少なかったのでまとめました
AWS Copilotについて
NextJSの設定
next.config.mjs
以下のようにoutput: 'export'
を追加してbuild時にHTMLが出力されるようにします
/** @type {import('next').NextConfig} */
const nextConfig = {
output: 'export',
};
export default nextConfig;
npm run build
等、ビルドコマンドを実行してout
ディレクトリにHTMLが出力されているのを確認しておきます
AWS copilotの設定と手動デプロイ
copilot init
でStatic Site
を選択します
-
Which directories or files would you like to upload for サービス名
と聞かれるのでHTMLが吐き出されているout
ディレクトリでスペースを押します(returnだと何も選ばれず次に行ってしまうので注意) -
Would you like to deploy an environment?
と聞かれるのでy
を選び、environmentを作成もしくは選択してデプロイします - デプロイが成功すると
Recommended follow-up action:
にURLが記載されているのでアクセスして、ページが観れていれば成功 - これ以降は
copilot deploy
で手動デプロイできます
copilot
ディレクトリにmanifest.ymlが作成されますが、一応こんな感じ
# The manifest for the "frontend" service.
# Read the full specification for the "Static Site" type at:
# https://aws.github.io/copilot-cli/docs/manifest/static-site/
# Your service name will be used in naming your resources like S3 buckets, etc.
name: サービス名
type: Static Site
files:
- source: frontend/out # 自分の環境に合ったディレクトリにしてください
recursive: true
# You can override any of the values defined above by environment.
# environments:
# test:
# files:
# - source: './blob'
# recursive: true
# destination: 'assets'
# exclude: '*'
# reinclude:
# - '*.txt'
# - '*.png'
自動化する
とりあえずデプロイできるようになったので、これをCodePipelineで自動化していきます
-
copilot pipeline init
を実行します -
What type of continuous delivery pipeline is this?
と聞かれるのでWorkloads
を選択します -
Which environment would you like to add to your pipeline?
で上記で作成もしくは選択したenvironmentをここでも選択します -
copilot/pipelines/pipeline名/buildspec.yml
に以下のようにinstall
とcommands
に追記します。これによってnpm run build
が実行されて、out
にHTMLが出力されるようにします
# Buildspec runs in the build stage of your pipeline.
version: 0.2
phases:
install:
runtime-versions:
nodejs: 18
commands:
- echo "cd into $CODEBUILD_SRC_DIR"
- cd $CODEBUILD_SRC_DIR
# Download the copilot linux binary.
- wget -q https://ecs-cli-v2-release.s3.amazonaws.com/copilot-linux-v1.33.3 -O copilot-linux
- chmod +x ./copilot-linux
build:
commands:
# コマンドは環境に合わせてください
- cd frontend
- npm ci
- npm run build
- cd ..
# 以下省略
これで設定完了です
copilot/pipelines/pipeline名/manifest.yml
のsource.properties.branch
に記載されているブランチにpushするとCodePipelineが実行されてデプロイが行われます