GitHub ActionsでECSにデプロイをする
やり方はすでにたくさん公開されているので、自分がやった設定をメモ
やったこと
プルリクをmainブランチにマージしたときに、ECRにイメージをpushしてECS Serviceを更新する
ベースはこれで、下記の部分だけ変更している
- mainブランチにマージしたときに実行
- イメージはlatestタグがついたものを使用するため、id:task-defの部分は不要
- task-definition.jsonはその場でダウンロード
参考: https://github.com/aws-actions/amazon-ecs-deploy-task-definition#task-definition-file
.github/workflow/deploy.yml
on: push: branches: - main name: Deploy to Amazon ECS on Staging jobs: deploy: name: Deploy runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v2 - name: Configure AWS credentials uses: aws-actions/configure-aws-credentials@v1 with: aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} aws-region: ap-northeast-1 - name: Login to Amazon ECR id: login-ecr uses: aws-actions/amazon-ecr-login@v1 - name: Build, tag, and push image to Amazon ECR id: build-image env: ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} ECR_REPOSITORY: app-ecr IMAGE_TAG: latest 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: Download task definition run: | aws ecs describe-task-definition --task-definition app-task --query taskDefinition > task-definition.json - name: Deploy Amazon ECS task definition uses: aws-actions/amazon-ecs-deploy-task-definition@v1 with: task-definition: task-definition.json service: app-service cluster: app-cluster wait-for-service-stability: true
actで動作確認
actというツールを使うと、ローカルでGitHub Actionsの確認ができる
ただ、そのまま使おうとしたところECRへのログイン部分で下記エラーが発生
::error::Unable to locate executable file: docker. Please verify either the file path exists or the file can be found within a directory specified by the PATH environment variable. Also check the file mode to verify the file is executable. Failure - Login to Amazon ECR
GitHubのissueを見た感じ、ローカルだとubuntu-latestをnode:12.6-buster-slimで実行しているからエラーが出てるっぽい?(よくわかってない)
ということで、-pでイメージ指定したら動いた
イメージが数GBあるため、最初のビルドにすごく時間がかかる
act -v -s AWS_ACCESS_KEY_ID=xxx -s AWS_SECRET_ACCESS_KEY=xxx -p ubuntu-latest=nektos/act-environments-ubuntu:18.04