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?

CICDパイプラインとは

Last updated at Posted at 2025-03-30

CICDパイプラインとは

ソフトウェア開発でコーディングコミットが終わったあとにする
ビルドテストデプロイの流れのことである。
(基本的にはビルドテストデプロイは自動化する)

CICDパイプラインのサービス

CICDパイプラインのサービスはGithubActionsが人気である。

GithubActionsの使い方

CICDパイプラインを使用したいアプリの中に.github/workflowsを作成してその中にCICDパイプラインの設定をするymlファイルを作成する。

onはどのコマンドでCICDパイプライン動作するのかを指定できる
jobsでどんな作業を実行するのかを指定できる(stepsの集まり)
runs-onは実行環境を指定する
stepsはjobsの中でどんな作業を実行するのかを具体的に書く
runは実行するコマンドのこと

例の猫のことを出力する簡単なアプリの例

.github/workflows/cicd.yml
name: Neko_Introduction # このCICDの名前

on:
  push: # push の時にCICDパイプラインを動作させる

jobs:
  Neko_Japan: # job名
    runs-on: ubuntu-latest # 実行環境の指定
    steps:
      - name: Say something in Japanese #stepsの名
        run: |
          echo "neko_wakaii" #実行コマンド
          echo "neko_utukusii"

  Neko_English:
    runs-on: ubuntu-latest 
    steps:
      - name: Say something in English
        run: |
          echo "cat_cute"
          echo "cat_beautiful"

DockerとAWSを使ったGitHubActionsのやり方

例のコード

name:  <CICDの名前>
on:
  push:
    paths:
      - '.github/workflows/**'
      - 'app/**'
env:
  AWS_REGION: <AWSのリージョン名>
  ECS_CLUSTER: <AWSのcluster名>
  ECS_SERVICE: <AWSのservice名>
  ECR_REPOSITORY: <AWSのrepository名>
  ECS_TASK_DEFINITION_API: <Task定義を行ったjsonファイルを指定する>

permissions:
  contents: read
  id-token: write

jobs:
  test-and-build:
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: cicd-section/api
    steps:
      - uses: actions/checkout@v4
      - name: Run Tests and Build an Image
        run: docker image build -t temp_api_image:latest .
      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v4
        with:
          aws-region: ${{ env.AWS_REGION }}
          role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }}
      - name: Login to Amazon ECR
        id: login-ecr
        uses: aws-actions/amazon-ecr-login@v2

      - name: Push the image to Amazon ECR
        env:
          ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
        run: |
          docker image tag temp_api_image:latest $ECR_REGISTRY/$ECR_REPOSITORY:${{ github.sha }}
          docker image push $ECR_REGISTRY/$ECR_REPOSITORY:${{ github.sha }}
          echo $ECR_REGISTRY/$ECR_REPOSITORY:${{ github.sha }} > api-image-url.txt
      
      - name: Upload the image url file as an artifact
        uses: actions/upload-artifact@v4
        with:
          name: api-image-url
          path: cicd-section/api/api-image-url.txt

  deploy:
    runs-on: ubuntu-latest
    needs: [test-and-build]
    steps:
      - name: Checkout
        uses: actions/checkout@v4

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

      - name: Download the artifact
        uses: actions/download-artifact@v4
        with:
          name: api-image-url
          path: artifacts
    
      - name: Define the image URI
        run: |
          echo "API_IMAGE_URI=$(cat artifacts/api-image-url.txt)" >> $GITHUB_ENV
    
      - name: Fill in the new image URI in the amazon ECS task definition
        id: render-task-def
        uses: aws-actions/amazon-ecs-render-task-definition@v1
        with:
          task-definition: ${{ env.ECS_TASK_DEFINITION_API }}
          container-name: api
          image: ${{ env.API_IMAGE_URI }}
  
      - name: Deploy  ECS task
        uses: aws-actions/amazon-ecs-deploy-task-definition@v1
        with:
          task-definition: ${{ steps.render-task-def.outputs.task-definition }}
          service: ${{ env.ECS_SERVICE }}
          cluster: ${{ env.ECS_CLUSTER }}
          wait-for-service-stability: true

上のコードの説明

まず、CICDパイプラインの名前とどのコマンドのときにCICDパイプラインを動作させるのかを指定する。

.github/workflows/cicd.yml
name: <CICDの名前>
on:
  push:
    paths: #このパスの中のファイルに変更のあったときにだけCICDパイプラインが動作する
      - '.github/workflows/**'
      - 'app/**'

AWSの環境変数ENVを定義する

env:
  AWS_REGION: <AWSのリージョン名>
  ECS_CLUSTER: <AWSのcluster名>
  ECS_SERVICE: <AWSのservice名>
  ECR_REPOSITORY: <AWSのrepository名>
  ECS_TASK_DEFINITION_API: <Task定義を行ったjsonファイルを指定する>

GitHub Actions ワークフローがリポジトリに対して持つ権限を指定する。

permissions:
  contents: read
  id-token: write

実行環境と作業ディレクトリを指定する。

jobs:
  test-and-build:
    runs-on: ubuntu-latest #実行環境の指定
    defaults:
      run:
        working-directory: api #作業ディレクトリを指定

uses: actions/checkout@v4とはgithubはgithubにあげたコードをそのままでは参照できないためcheckoutを使用してgithubが参照できるようにする。
usesは誰かが作ったrunの集まりを参照するときに使う。

ここでdocker imageを作成する

    steps:
      - uses: actions/checkout@v4
      - name: Run Tests and Build an Image
        run: docker image build -t temp_api_image:latest .

GitHub ActionsAWSにログインし、権限を取得する
secrets.AWS_ROLE_TO_ASSUMEの設定はとてもセキュアにしないといけないためこのファイルにENVでは書かずに、githubのページで設定する。
環境変数の参照は${{}}でくくる。

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

ECRにログインする

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

steps.login-ecr.outputs.registryにはidがlogin-ecrであるstepの出力値を参照している。

そして、rundocker imagetagを付けて、そのdocker imageECRpushしている。

そして、echoコマンドを使用して、$ECR_REGISTRY/$ECR_REPOSITORY:${{ github.sha }}api-image-url.txtに保存している。

      - name: Push the image to Amazon ECR
        env:
          ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
        run: |
          docker image tag temp_api_image:latest $ECR_REGISTRY/$ECR_REPOSITORY:${{ github.sha }}
          docker image push $ECR_REGISTRY/$ECR_REPOSITORY:${{ github.sha }}
          echo $ECR_REGISTRY/$ECR_REPOSITORY:${{ github.sha }} > api-image-url.txt

下のコードはgithubのアーティファクトを用いてdocker imageURLapi-image-url.txtというファイルに保存して、そのファイルをGitHub Actions内で使えるようにアップロードして保存している。

      - name: Upload the image url file as an artifact
        uses: actions/upload-artifact@v4
        with:
          name: api-image-url
          path: cicd-section/api/api-image-url.txt

以下のコードで実行環境とtest-and-buildが終わった後に実行するように設定する。
そして、コードをcheckoutしてGitHub Actionsがコードを参照できるようにする。

  deploy:
    runs-on: ubuntu-latest
    needs: [test-and-build]
    steps:
      - name: Checkout
        uses: actions/checkout@v4

GitHub ActionsがAWSにログインし、権限を取得する

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

アーティファクトにあるapi-image-url.txtをアップロードしてきてdocker image名をGitHub Actionsで使えるようにしている。

      - name: Download the artifact
        uses: actions/download-artifact@v4
        with:
          name: api-image-url
          path: artifacts

artifacts/api-image-url.txtというファイルの中身をAPI_IMAGE_URIという環境変数に代入して、それを$GITHUBに書き込んでいる。

      - name: Define the image URI
        run: |
          echo "API_IMAGE_URI=$(cat artifacts/api-image-url.txt)" >> $GITHUB_ENV

ECS に、新しいコンテナイメージを使うように設定する準備をしている

      - name: Fill in the new image URI in the amazon ECS task definition
        id: render-task-def
        uses: aws-actions/amazon-ecs-render-task-definition@v1
        with:
          task-definition: ${{ env.ECS_TASK_DEFINITION_API }}
          container-name: api
          image: ${{ env.API_IMAGE_URI }}

新しいタスク定義を Amazon ECS に適用して、サービスをデプロイしている。

      - name: Deploy  ECS task
        uses: aws-actions/amazon-ecs-deploy-task-definition@v1
        with:
          task-definition: ${{ steps.render-task-def.outputs.task-definition }}
          service: ${{ env.ECS_SERVICE }}
          cluster: ${{ env.ECS_CLUSTER }}
          wait-for-service-stability: true
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?