はじめに
このガイドでは、コンテナー化されたアプリケーションをビルドし、それを Amazon Elastic Container Registry (ECR) にプッシュし、それを、main
ブランチへのプッシュがあるとき、Amazon Elastic Container Service (ECS) にデプロイする目的で GitHub Actions を使用する方法について説明します。
GitHub リポジトリで main
に新しくプッシュするたびに、GitHub Actions ワークフローによって新しいコンテナ イメージがビルドされ、Amazon ECR にプッシュされ、新しいタスクの定義が Amazon ECS にデプロイされます。
前提条件
GitHub Actionsワークフローを作成する前に、まず以下のAmazon ECR及びECSのセットアップのステップを完了しておかなければなりません。
-
イメージを保存するAmazon ECRリポジトリの作成
たとえば、AWS CLI を使用する:
Bash --repository-name MY_ECR_REPOSITORY \ --region MY_AWS_REGION
--repository-name MY_ECR_REPOSITORY \ --region MY_AWS_REGION
Ensure that you use the same Amazon ECR repository name (represented here by
MY_ECR_REPOSITORY
) for theECR_REPOSITORY
variable in the workflow below.Ensure that you use the same AWS region value for the
AWS_REGION
(represented here byMY_AWS_REGION
) variable in the workflow below. -
Create an Amazon ECS task definition, cluster, and service.
For details, follow the Getting started wizard on the Amazon ECS console, or the Getting started guide in the Amazon ECS documentation.
Ensure that you note the names you set for the Amazon ECS service and cluster, and use them for the
ECS_SERVICE
andECS_CLUSTER
variables in the workflow below. -
Store your Amazon ECS task definition as a JSON file in your GitHub repository.
The format of the file should be the same as the output generated by:
Bash aws ecs register-task-definition --generate-cli-skeleton
aws ecs register-task-definition --generate-cli-skeleton
Ensure that you set the
ECS_TASK_DEFINITION
variable in the workflow below as the path to the JSON file.Ensure that you set the
CONTAINER_NAME
variable in the workflow below as the container name in thecontainerDefinitions
section of the task definition. -
Create GitHub Actions secrets named
AWS_ACCESS_KEY_ID
andAWS_SECRET_ACCESS_KEY
to store the values for your Amazon IAM access key.For more information on creating secrets for GitHub Actions, see "GitHub Actions でのシークレットの使用."
See the documentation for each action used below for the recommended IAM policies for the IAM user, and methods for handling the access key credentials.
-
Optionally, configure a deployment environment. 環境は、一般的なデプロイ ターゲットを記述するために使用されます (例:
production
、staging
、またはdevelopment
)。 GitHub Actions ワークフローが環境にデプロイされると、その環境がリポジトリのメイン ページに表示されます。 環境を使って、ジョブを進めるには承認を必須にすること、ワークフローをトリガーできるブランチを制限すること、またはシークレットへのアクセスを制限することができます。 環境の作成の詳細については、「デプロイに環境を使用する」を参照してください。
Creating the workflow
Once you've completed the prerequisites, you can proceed with creating the workflow.
The following example workflow demonstrates how to build a container image and push it to Amazon ECR. It then updates the task definition with the new image ID, and deploys the task definition to Amazon ECS.
Ensure that you provide your own values for all the variables in the env
key of the workflow.
デプロイ環境を構成した場合は、environment
の値を環境の名前に変更します。 環境を構成しなかった場合 を使わない場合は、environment
キーを削除します。
# このワークフローはGitHubによって認定されていないアクションを使用します。 # それらはサードパーティによって提供され、 # 別個の利用規約、プライバシーポリシー、 # ドキュメントを参照してください。 # GitHub では、コミット SHA にアクションをピン留めすることが推奨されます。 # 新しいバージョンを取得するには、SHA を更新する必要があります。 # タグまたはブランチを参照することもできますが、アクションは警告なしに変更される可能性があります。 name: Deploy to Amazon ECS on: push: branches: - main env: AWS_REGION: MY_AWS_REGION # set this to your preferred AWS region, e.g. us-west-1 ECR_REPOSITORY: MY_ECR_REPOSITORY # set this to your Amazon ECR repository name ECS_SERVICE: MY_ECS_SERVICE # set this to your Amazon ECS service name ECS_CLUSTER: MY_ECS_CLUSTER # set this to your Amazon ECS cluster name ECS_TASK_DEFINITION: MY_ECS_TASK_DEFINITION # set this to the path to your Amazon ECS task definition # file, e.g. .aws/task-definition.json CONTAINER_NAME: MY_CONTAINER_NAME # set this to the name of the container in the # containerDefinitions section of your task definition jobs: deploy: name: Deploy runs-on: ubuntu-latest environment: production steps: - name: Checkout uses: actions/checkout@v2 - name: Configure AWS credentials uses: aws-actions/configure-aws-credentials@0e613a0980cbf65ed5b322eb7a1e075d28913a83 with: aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} aws-region: ${{ env.AWS_REGION }} - name: Login to Amazon ECR id: login-ecr uses: aws-actions/amazon-ecr-login@62f4f872db3836360b72999f4b87f1ff13310f3a - name: Build, tag, and push image to Amazon ECR id: build-image env: ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} IMAGE_TAG: ${{ github.sha }} run: | # Build a docker container and # push it to ECR so that it can # be deployed to ECS. docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG . docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" - name: Fill in the new image ID in the Amazon ECS task definition id: task-def uses: aws-actions/amazon-ecs-render-task-definition@c804dfbdd57f713b6c079302a4c01db7017a36fc with: task-definition: ${{ env.ECS_TASK_DEFINITION }} container-name: ${{ env.CONTAINER_NAME }} image: ${{ steps.build-image.outputs.image }} - name: Deploy Amazon ECS task definition uses: aws-actions/amazon-ecs-deploy-task-definition@df9643053eda01f169e64a0e60233aacca83799a with: task-definition: ${{ steps.task-def.outputs.task-definition }} service: ${{ env.ECS_SERVICE }} cluster: ${{ env.ECS_CLUSTER }} wait-for-service-stability: true
# このワークフローはGitHubによって認定されていないアクションを使用します。
# それらはサードパーティによって提供され、
# 別個の利用規約、プライバシーポリシー、
# ドキュメントを参照してください。
# GitHub では、コミット SHA にアクションをピン留めすることが推奨されます。
# 新しいバージョンを取得するには、SHA を更新する必要があります。
# タグまたはブランチを参照することもできますが、アクションは警告なしに変更される可能性があります。
name: Deploy to Amazon ECS
on:
push:
branches:
- main
env:
AWS_REGION: MY_AWS_REGION # set this to your preferred AWS region, e.g. us-west-1
ECR_REPOSITORY: MY_ECR_REPOSITORY # set this to your Amazon ECR repository name
ECS_SERVICE: MY_ECS_SERVICE # set this to your Amazon ECS service name
ECS_CLUSTER: MY_ECS_CLUSTER # set this to your Amazon ECS cluster name
ECS_TASK_DEFINITION: MY_ECS_TASK_DEFINITION # set this to the path to your Amazon ECS task definition
# file, e.g. .aws/task-definition.json
CONTAINER_NAME: MY_CONTAINER_NAME # set this to the name of the container in the
# containerDefinitions section of your task definition
jobs:
deploy:
name: Deploy
runs-on: ubuntu-latest
environment: production
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@0e613a0980cbf65ed5b322eb7a1e075d28913a83
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@62f4f872db3836360b72999f4b87f1ff13310f3a
- name: Build, tag, and push image to Amazon ECR
id: build-image
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
IMAGE_TAG: ${{ github.sha }}
run: |
# Build a docker container and
# push it to ECR so that it can
# be deployed to ECS.
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG"
- name: Fill in the new image ID in the Amazon ECS task definition
id: task-def
uses: aws-actions/amazon-ecs-render-task-definition@c804dfbdd57f713b6c079302a4c01db7017a36fc
with:
task-definition: ${{ env.ECS_TASK_DEFINITION }}
container-name: ${{ env.CONTAINER_NAME }}
image: ${{ steps.build-image.outputs.image }}
- name: Deploy Amazon ECS task definition
uses: aws-actions/amazon-ecs-deploy-task-definition@df9643053eda01f169e64a0e60233aacca83799a
with:
task-definition: ${{ steps.task-def.outputs.task-definition }}
service: ${{ env.ECS_SERVICE }}
cluster: ${{ env.ECS_CLUSTER }}
wait-for-service-stability: true
その他のリソース
もともとのスターター ワークフローについては、GitHub Actions starter-workflows
リポジトリの aws.yml
を参照してください。
この例で使われているサービスに関する詳しい情報については、以下のドキュメンテーションを参照してください。
- Amazon AWS ドキュメントの "IAM のセキュリティ ベスト プラクティス"。
- 公式 AWS "AWS 資格情報を構成する" アクション。
- 公式 AWS Amazon ECR "ログイン" アクション。
- 公式 AWS Amazon ECS "タスク定義のレンダリング" アクション。
- 公式 AWS Amazon ECS "タスク定義のデプロイ" アクション。