Gatsby BlogをCodeCommitとCodeBuildでDevOps実現する / DevOps for Gatsby blog using by CodeCommit and CodeBuild
English follows Japanese
AWSのCodeCommitとCodeBuildを使ってGatsby BlogをPublishすることにしました。また、CodePipelineを利用して自働化も実現しています。全体アーキテクチャはこんな感じ↓です。
CodeCommit
現状はブログ記事をMacにローカル保存しているので永続的な保存と履歴管理のためにGitを導入することに決めました。GitHubと悩みましたがせっかくなのでAWS内で完結させるためにCodeCommitを選びました。
CodeBuild
CodeCommitで作成したレポジトリを選択し、以下のように設定すればOKです。
- 環境イメージ : マネージド型イメージ
- オペレーティングシステム : Amazon Linux2
- ランタイム : Standard
- イメージ : aws/codebuild/amazonlinux2-x86_64-standard:2.0
- イメージのバージョン : 最新を選択
- 環境タイプ : Linux
- 特権付与 : チェックしない
- サービスロール : アカウントから既存のサービスロールを選択
- AWS CodeBuild にこのサービスロールの編集を許可し、このビルドプロジェクトでの使用を可能にする : チェック
Buildの設定を行うBuildspecは「ビルドコマンドの挿入」を選択し、以下を記載します。実はここにくるまでエラーが多発し、最終的にはレポジトリ内のnode_moduleを削除した状態で実施しました・・・。deployでs3へのUploadも実施しています。
version: 0.2
phases:
install:
runtime-versions:
nodejs: 12
pre_build:
commands:
- npm install
build:
commands:
- npm run build
post_build:
commands:
- npm run deploy
CodePipeline
CodeCommitにCommitされたタイミングでBuildが走るようにCodePipelineを利用して自動化を実現しています。
確認
ブログコンテンツを更新し、Commit後にCodeBuildが実行されコンテンツが更新されればOKです。これで仮にMacが壊れてもコンテンツを失うことは無くなりました。
English
Publish Gatsby blog contents using by AWS CodeCommit and CodeBuild. In addition, using CodePipeline enable automation deploy. Overview architecture is here.
CodeCommit
I wanted to manage and keep blog contents because I saved my contents to Mac locally. GitHub is one of options, however this is good opportunity to use AWS services, so I decided to use AWS CodeCommit.
CodeBuild
Select repository created in CodeCommit and choose following settings.
- Environment image : Managed image
- Operating system : Amazon Linux2
- Runtime(s) : Standard
- Image : aws/codebuild/amazonlinux2-x86_64-standard:2.0
- Image version : Always use latest image
- Environment type : Linux
- Privileged : Uncheck
- Service role : Choose an existing service role from your account
- Allow AWS CodeBuild to modify this service role so it can be used with this build project : Check
Choose "Insert build commands" in Buildspec and add following code. Actually, I had many errors to set this configuration, finally I delete all "node_modules" in repository. In this deploy include s3 Upload also.
version: 0.2
phases:
install:
runtime-versions:
nodejs: 12
pre_build:
commands:
- npm install
build:
commands:
- npm run build
post_build:
commands:
- npm run deploy
CodePipeline
Run build automatically when commit code to CodeCommit, use CodePipeline.
Confirmation
Update blog contents and commit it, check running CodeBuild automatically. A couple of minutes later, you can check blog contents is updated. Finally I can keep my blog contents if my Mac was stopped.
bebenosuke