状況
Github Actionを使用する際に、別のリポジトリで用意しているインフラ設定(Terraform)を流用したかったので、その際の対応方法のメモ。
workflow.yml
以下のci/cdは、mainブランチへのマージ時に、アプリケーションのdocker imageをbulidし、ECSで稼働させます。
ハイライト部分がインフラ設定のリポジトリをチェックアウトする部分です。
name: update ECS # mainへのpushをトリガーとする on: push: branches: - main permissions: id-token: write contents: read env: ENV: stg jobs: build-deploy: name: build image and update ECS runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v2 - name: login aws oicd uses: aws-actions/configure-aws-credentials@v1 with: role-to-assume: arn:aws:iam::${{ secrets.AWS_ACCOUNT_ID }}:role/github-actions-oidc aws-region: ap-northeast-1 - name: Login to Amazon ECR id: login-ecr uses: aws-actions/amazon-ecr-login@v1 # アプリケーションリポジトリでECSで稼働させるdocke imageをbuildし、ECRにpushする。 - name: push image to Amazon ECR id: build-image env: ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} IMAGE_TAG: ${{ github.sha }} run: | ECR_REPOSITORY=my-repo-${{ env.ENV }} docker build . -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG -f docker/rails/Dockerfile IMAGE_URL=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG docker push $IMAGE_URL echo "::set-output name=image::$IMAGE_URL" # インフラ設定を持つリポジトリをチェックアウト - name: checkout uses: actions/checkout@v2 with: repository: my-org/my-infra-repo ref: main # checkout対象のブランチの指定 path: infra # checkoutしたリポジトリを配置するディレクトリを指定 # インフラのリポジトリがprivateの場合は、pull権限を持つtokenを与える必要があります。 token: ${{ secrets.GET_PRIVATE_REPOS }} # Terraform環境を用意するためのtemplate - name: terraform setup uses: hashicorp/setup-terraform@v1 with: terraform_version: 0.14.5 # terraform outputの実行のために必要な設定 # https://blog.nillsf.com/index.php/2020/08/25/how-to-use-terraform-output-in-a-github-action/ terraform_wrapper: false # ECSのterraform applyに必要なvariablesを用意する - name: fetch outputs of ALB id: alb-output working-directory: infra/modules/alb run: | terraform init ALB_TARGET_GROUP_ARN=`terraform output -raw alb_target_group_arn` echo "::set-output name=alb_target_group_arn::$ALB_TARGET_GROUP_ARN" # 今回はTerraformを使うのですが、backend.tfはインフラのリポジトリにも用意してないので、作成します。 - name: set remote state of ECS working-directory: infra/modules/ecs run: | cat > backend.tf << EOF terraform { backend "s3" { bucket = "my-repo-${{ env.ENV }}-tfstate" key = "ecs.tfstate" region = "ap-northeast-1" encrypt = true } } EOF # Terraform initを実行するstep - name: terraform init ECS run: terraform init # 処理を実行させるディレクトリを指定します working-directory: infra/modules/ecs # Terraform applyを実行するstep - name: terraform apply ECS id: terraform-apply env: IMAGE_URL: ${{ steps.build-image.outputs.image }} ALB_TARGET_GROUP: ${{ steps.alb-output.outputs.alb_target_group_arn }} working-directory: infra/modules/ecs run: | terraform apply -auto-approve -no-color \ -var="service=my-repo" \ -var="environment=${{ env.ENV }}" \ -var="image=$IMAGE_URL" \ -var="alb_target_group_arn=$ALB_TARGET_GROUP" \ -var="aws_iam_role_ecs_task_role_arn=arn:aws:iam::${{ secrets.AWS_ACCOUNT_ID }}:role/my-repo-stg-task-role" \ -var="aws_iam_role_ecs_task_execution_role_arn=arn:aws:iam::${{ secrets.AWS_ACCOUNT_ID }}:role/my-repo-stg-task-execution-role" \ -var="ecs_task_count=2"
参考: https://zenn.dev/jerome/articles/cc07ad73e017ad