kakakakakku blog

Weekly Tech Blog: Keep on Learning!

source-version-override: aws-actions/aws-codebuild-run-build でプルリクエストブランチを AWS CodeBuild のビルド対象にする

AWS CodeBuild Run Build for GitHub Actions (aws-actions/aws-codebuild-run-build) を使って GitHub Actions から AWS CodeBuild のビルドを実行すると buildspec.yml やビルド環境タイプを上書きできて便利〜という話は前にまとめた👌

kakakakakku.hatenablog.com

今回は source-version-override パラメータを活用して,プルリクエストを出したときにプルリクエストブランチを AWS CodeBuild のビルド対象にする仕組みを試してみた.以下に検証用の GitHub Actions ワークフローを載せておく📝プルリクエストをマージする前に動作確認ができるようになるから便利なパラメータだと思う❗️

name: Start AWS CodeBuild build

on:
  workflow_dispatch:
  push:
    branches:
      - master
  pull_request:
    branches:
      - master

permissions:
  id-token: write
  contents: read

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: aws-actions/configure-aws-credentials@v4
        with:
          role-to-assume: ${{ secrets.AWS_ROLE_ARN }}
          aws-region: ap-northeast-1
      - name: Start AWS CodeBuild build
        uses: aws-actions/aws-codebuild-run-build@v1
        with:
          project-name: sandbox
          source-version-override: ${{ github.head_ref }}
        if: github.event_name == 'pull_request'
      - name: Start AWS CodeBuild build
        uses: aws-actions/aws-codebuild-run-build@v1
        with:
          project-name: sandbox
          source-version-override: pr/${{ github.event.pull_request.number }}
        if: github.event_name == 'pull_request'
      - name: Start AWS CodeBuild build
        uses: aws-actions/aws-codebuild-run-build@v1
        with:
          project-name: sandbox
          source-version-override: ${{ github.ref_name }}
        if: github.event_name == 'push'

AWS CodeBuild Run Build for GitHub Actions の source-version-override パラメータには GitHub の場合「コミット ID/プルリクエスト ID/ブランチ名/タグ名」を指定できる.AWS CodeBuild の API Reference (StartBuild) に以下のように書いてあった📝

The commit ID, pull request ID, branch name, or tag name that corresponds to the version of the source code you want to build. If a pull request ID is specified, it must use the format pr/pull-request-ID (for example pr/25). If a branch name is specified, the branch's HEAD commit ID is used. If not specified, the default branch's HEAD commit ID is used.

プルリクエストブランチ

プルリクエストを作った場合は github.event_name == 'pull_request' で判定しつつ,source-version-override パラメータにブランチ名を表す ${{ github.head_ref }} を設定すれば OK👌

- name: Start AWS CodeBuild build
  uses: aws-actions/aws-codebuild-run-build@v1
  with:
    project-name: sandbox
    source-version-override: ${{ github.head_ref }}
  if: github.event_name == 'pull_request'

もしブランチ名ではなく「プルリクエスト ID」を指定する場合は pr/pull-request-ID というフォーマットにする必要がある.source-version-override パラメータにプルリクエスト ID を表す pr/${{ github.event.pull_request.number }} を設定すれば OK👌

- name: Start AWS CodeBuild build
  uses: aws-actions/aws-codebuild-run-build@v1
  with:
    project-name: sandbox
    source-version-override: pr/${{ github.event.pull_request.number }}
  if: github.event_name == 'pull_request'

メインブランチ

プルリクエストをマージした場合は github.event_name == 'push' で判定しつつ,source-version-override パラメータにブランチ名を表す ${{ github.ref_name }} を設定すれば OK👌

- name: Start AWS CodeBuild build
  uses: aws-actions/aws-codebuild-run-build@v1
  with:
    project-name: sandbox
    source-version-override: ${{ github.ref_name }}
  if: github.event_name == 'push'

動作確認

期待通りに動いたー👏