ChatGPT(4.0)にGithubActionsのワークフローを書いてもらった

Github Actionsのワークフローを一から作るの面倒くさい・・・AIに頼むか!」

というわけで頼んでみた。

Generate Github action workflow code. Specifications are as follows.

1. Build a Docker container from a Dockerfile (./docker/Dockerfile).
2. Push the container to AWS ECR. The repository that pushed container is `XXXXXXXXXXXXXXX.dkr.ecr.ap-northeast-1.amazonaws.com/development-mybook-api`.
3. Update AWS services to use containers stored in ECR repository

実際はXXXXXXXXXXXXXXXにAWSのアカウントIDを入れているのだが、さすがに公開するのはアレなのでマスクしている。 そして出力されたのがこちら。

name: Build and Push Docker image to AWS ECR and Update Services

on:
  push:
    branches:
      - master # Or the branch you want

env:
  AWS_REGION: ap-northeast-1
  ECR_REPOSITORY: XXXXXXXXXXXXXXX.dkr.ecr.ap-northeast-1.amazonaws.com/development-mybook-api

jobs:
  build-push:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout code
      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: ${{ env.AWS_REGION }}

    - 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 }}
        IMAGE_TAG: ${{ github.sha }}
      run: |
        docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG -f ./docker/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
      uses: aws-actions/amazon-ecs-render-task-definition@v1
      with:
        task-definition: task-definition.json
        container-name: mybook-api
        image: ${{ steps.build-image.outputs.image }}

    - name: Deploy Amazon ECS task definition
      uses: aws-actions/amazon-ecs-deploy-task-definition@v1
      with:
        task-definition: ${{ steps.task-def.outputs.task-definition }}
        service: mybook-api-service
        cluster: mybook-api-cluster
        force-new-deployment: true

そして提示された説明がこちら。「当然わかるよね」「とりあえずダミーを入れておいてくれればいいよ」的にこちらが飛ばした指定についても見事に補完してくれている。 欲を言えばactionが最新版だったりOIDCを使ってくれていたりすると尚よかったが、それは望みすぎだろう。

The workflow above does the following:

Checks out your repository.
Configures AWS credentials.
Logs in to ECR.
Builds the Docker image, tags it, and pushes it to your ECR repository.
Updates the ECS task definition with the new image ID.
Deploys the updated task definition.


Please replace task-definition.json, mybook-api-service, and mybook-api-cluster with your actual task definition, service, and cluster.

You also need to add your AWS credentials (AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY) to your GitHub repository secrets.

Remember to replace the event trigger and branches (on.push.branches) with the appropriate ones for your use case. In the example, the workflow is triggered when a push is made to the master branch.

Please ensure the GitHub Actions for AWS (aws-actions) are still supported and actively maintained as the Actions ecosystem evolves.

そのまま使うわけではなく、いくらか手を加えるものの、これをベースにして作業を進められるだけでも作業効率が段違いである。非常に助かる。