LoginSignup
8
6

More than 3 years have passed since last update.

GithubActions, ECR, ECSを用いてサクッと自動デプロイしてみた

Last updated at Posted at 2020-05-16

はじめに

こんにちは。KENです。
GitHubでmasterブランチにpushした際に自動デプロイを行う方法を今回は書いていきます。
前回の続きで、AWSのECR、ECSを使用したコンテナデプロイになります。
特にaws.ymlの記述方法に焦点を当てています。

やるべきこと

aws.yml内で定義すること

  • AmazonECRにログインする
  • 以下のファイル構成の中のnginx/DockerfileからDockerイメージのビルドを行う。ECRのsample-app/ngixというディレクトリにPushする。
  • php/DockerfileからDockerファイルのイメージのビルドを行う。ECRのsample-app/phpというディレクトリにPushする。
  • task-definition.json(タスク定義)に使用するDockeイメージのURLをセットする。
  • 使用するサービスとクラスターを指定して、自動デプロイを行う。

ファイル構成

.
├── README.md
├── .github/workflows
│   ├── aws.yml
│
├── docker
│   ├── mysql
│   │   └── my.cnf
│   ├── nginx
|   |   |__ Dockerfile
│   │   └── default.conf
│   └── php
│       ├── Dockerfile
│       └── php.ini
├── docker-compose.yml
├── task-definition.json
├── logs
│   ├── access.log
│   ├── error.log
│   ├── mysql-error.log
│   ├── mysql-query.log
│   ├── mysql-slow.log
│   └── php-error.log
└── src(アプリケーションのルートディレクトリ)
    └── readme.md

aws.ymlの書き方

# This workflow will build and push a new container image to Amazon ECR,
# and then will deploy a new task definition to Amazon ECS, when a release is created
# 
# To use this workflow, you will need to complete the following set-up steps:
#
# 1. Create an ECR repository to store your images.
#    For example: `aws ecr create-repository --repository-name my-ecr-repo --region us-east-2`.
#    Replace the value of `ECR_REPOSITORY` in the workflow below with your repository's name.
#    Replace the value of `aws-region` in the workflow below with your repository's region.
#
# 2. Create an ECS task definition, an ECS cluster, and an ECS service.
#    For example, follow the Getting Started guide on the ECS console:
#      https://us-east-2.console.aws.amazon.com/ecs/home?region=us-east-2#/firstRun
#    Replace the values for `service` and `cluster` in the workflow below with your service and 
cluster names.
#
# 3. Store your ECS task definition as a JSON file in your repository.
#    The format should follow the output of `aws ecs register-task-definition --generate-cli- 
skeleton`.
#    Replace the value of `task-definition` in the workflow below with your JSON file's name.
#    Replace the value of `container-name` in the workflow below with the name of the container
#    in the `containerDefinitions` section of the task definition.
#
# 4. Store an IAM user access key in GitHub Actions secrets named `AWS_ACCESS_KEY_ID` and 
`AWS_SECRET_ACCESS_KEY`.
#    See the documentation for each action used below for the recommended IAM policies for this IAM 
user,
#    and best practices on handling the access key credentials.

on:
  push:
    branches: [master]

name: Deploy to Amazon ECS

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: Nginx, Build, tag, and push image to Amazon ECR
      id: build-image-nginx
      env:
        ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
        ECR_REPOSITORY: sample-app/nginx
        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 -f docker/nginx/Dockerfile .
        docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
        echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG"

    - name: PHP, Build, tag, and push image to Amazon ECR
      id: build-image-php
      env:
        ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
        ECR_REPOSITORY: sample-app/php
        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 -f docker/php/Dockerfile .
        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-web
      uses: aws-actions/amazon-ecs-render-task-definition@v1
      with:
        task-definition: task-definition.json
        container-name: web
        image: ${{ steps.build-image-nginx.outputs.image }}

    - name: Fill in the new image ID in the Amazon ECS task definition
      id: task-def-app
      uses: aws-actions/amazon-ecs-render-task-definition@v1
      with:
        task-definition: ${{ steps.task-def-web.outputs.task-definition }}
        container-name: app
        image: ${{ steps.build-image-php.outputs.image }}

    - name: Deploy Amazon ECS task definition
      uses: aws-actions/amazon-ecs-deploy-task-definition@v1
      with:
        task-definition: ${{ steps.task-def-app.outputs.task-definition }}
        service: sample-app-service
        cluster: sample-app
        wait-for-service-stability: true

最後に

はい今回のようなファイル構成の場合、以上のようにaws.ymlを記述すると、
masterブランチにpushされたタイミングでwebコンテナとappコンテナが自動デプロイされます。

間違い、コメント等ありましたら、お待ちしております・・

8
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
6