0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【備忘録】ECR push + ECS deploy GitHub Actions

Posted at

GitHub Actions入門

  • name ⇒ ワークフロー名を指定
  • on ⇒ ワークフローを発火させる条件を指定
  • jobs ⇒ 子要素の名前をjob名とする個別のjobを定義する
  • runs-onjobを実行するOSを指定
  • steps ⇒ 子要素に記載した内容をステップとして逐次実行する
name: Hello GitHub Actions
on: push

jobs:
  hello-job:
    runs-on: ubuntu-latest
    steps:
      - run: echo "Hello"
      - run: echo "GitHub Actions!!"
  goodbye-job:
    runs-on: ubuntu-latest
    steps:
      - run: echo "Goodbye, GitHub Actions."

以下のようになる

image.png

ECR push + ECS Deploy Actions

AWS側の事前設定

  • image push用のECRリポジトリを作成
  • 適当なタスク定義を作成
  • 仮置きでECS Serviceを起動する
  • タスク定義JSON.aws配下に保存する

ディレクトリ構成の前提

.
├── .aws/
│   └── cicd-task-def.json
├── .github/
│   └── workflows/
│       └── ecr_push_ecs_deploy.yml
└── cicd-section/
    └── api/
        ├── Dockerfile
        ├── production-code
        └── ...

ワークフロー

.github/workflows/ecr_push_ecs_deploy.yml
name: ECR Push and ECS Deploy
on:
  push:
    paths:
      - cicd-section/api/**
      - .github/workflows/**

permissions:
  id-token: write
  contents: read

env:
  AWS_REGION: <REGION_NAME>
  ECS_CLUSTER: <CLUSTER_NAME>
  ECS_SERVICE: <SERVICE_NAME>
  ECR_REPOSITORY: <REPOSITORY_NAME>
  TASK_DEF_JSON: <FILE PATH>
  
jobs:
  ecr-push:
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: cicd-section/api
    outputs:
      image-uri: ${{ steps.export-image.outputs.image-uri }}

    steps:
      - name: git pull
        uses: actions/checkout@v4

      - name: AWS credential set
        uses: aws-actions/configure-aws-credentials@v4
        with:
          aws-region: ${{ env.AWS_REGION }}
          role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }}

      - name: ECR login
        id: ecr-login
        uses: aws-actions/amazon-ecr-login@v2

      - name: docker image build
        run: docker image build -t ${{ steps.ecr-login.outputs.registry }}/${{ env.ECR_REPOSITORY }}:${{ github.sha }} .

      - name: docker image push
        run: docker image push ${{ steps.ecr-login.outputs.registry }}/${{ env.ECR_REPOSITORY }}:${{ github.sha }}

      - name: export image URI
        id: export-image
        run: echo "image-uri=${{ steps.ecr-login.outputs.registry }}/${{ env.ECR_REPOSITORY }}:${{ github.sha }}" >> $GITHUB_OUTPUT

  ecs-deploy:
    runs-on: ubuntu-latest
    needs: [ecr-push]

    steps:
      - name: git pull
        uses: actions/checkout@v4

      - name: AWS credential set
        uses: aws-actions/configure-aws-credentials@v4
        with:
          aws-region: ${{ env.AWS_REGION }}
          role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }}

      - name: update ecs task def
        id: update-ecs-task
        uses: aws-actions/amazon-ecs-render-task-definition@v1
        with:
          task-definition: ${{ env.TASK_DEF_JSON }}
          container-name: web
          image: ${{ needs.ecr-push.outputs.image-uri }}

      - name: deploy ecs task def
        uses: aws-actions/amazon-ecs-deploy-task-definition@v2
        with:
          task-definition: ${{ steps.update-ecs-task.outputs.task-definition }}
          cluster: ${{ env.ECS_CLUSTER }}
          service: ${{ env.ECS_SERVICE }}
          wait-for-service-stability: true

ワークフローの説明

  • permissionsOIDCを使用してAWSとの認証を行うために必要
  • env ⇒ 変数宣言
  • working-directoryジョブ中にステップが実行されるディレクトリを変更
  • outputs後続ジョブに対してステップ内の情報をexportする
  • usesmarketplaceに存在する外部アクションを実行する
    • with外部アクションに対して引数を与える
      ⇒ 必要な引数は外部アクションを持つリポジトリのaction.ymlに記載されている
  • needsジョブの実行を直列にする

outputwithについての詳細は以下リンク

GitHub ActionsからAWSリソースにアクセスするためのAWS側設定

IdPを作成する

  • IAM > IDプロバイダ > プロバイダーを追加を選択

設定項目は以下の通り

  • OpenID Connect
  • プロバイダのURL ⇒ https://token.actions.githubusercontent.com
  • 対象者 ⇒ sts.amazonaws.com

image.png

ロールを作成する

  • IAM > ロール > ロールを作成を選択

設定項目は以下の通り

  • エンティティタイプ ⇒ ウェブアイデンティティ
  • IdP ⇒ token.actions.githubusercontent.com
  • Audience ⇒ sts.amazonaws.com
  • GitHub organization
  • GitHub リポジトリ
  • GitHub ブランチ

image.png

  • 許可ポリシー
    ⇒ ActionsからAWS側への実行を許可するポリシーを選択する
      外部Actionで必要なポリシーはmarketplacerepositoryREADMEを見る

image.png

  • ロール名

image.png

Secretsを活用したGitHub Actionsへのロール権限付与

  • リポジトリ > settings > Secrets and valiables > Actions
    にIAMロールのARNを登録する

paint_temp.png

uwagakiyou.png

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?