LoginSignup
1
0

More than 1 year has passed since last update.

UnityでGitHub Actions(2) -PRと特定コメントをフックにしてビルドを行う

Posted at

前回

今回は、PRとPRにつけられたコメントをフックにしてUnityのWebGLビルドを行うワークフローを作ります。

ワークフロー

初めにワークフローの全体を示します。
このwebgl_build.ymlを前回と同様リポジトリ直下の.github/workflowsディレクトリに配置します。

webgl_build.yml
name: Run the WebGL build
on:
  pull_request:
    types: [opened, reopened]

  issue_comment:
    types: [created, edited]

jobs:
  build:
    name: Run the WebGL build
    if: |
      github.event_name == 'pull_request' ||
      ( contains(github.event.comment.html_url, '/pull/') && startsWith(github.event.comment.body, '/retry build') )
    runs-on: ubuntu-latest
    steps:
      - name: Get upstream branch
        uses: xt0rted/pull-request-comment-branch@29fe0354c01b30fb3de76f193ab33abf8fe5ddb0 #1.2.0
        id: upstream_branch
        with:
          repo_token: ${{ secrets.GITHUB_TOKEN }}
        
      - name: Notify pending status
        uses: hkusu/status-create-action@1040f888115fc10b2e0fa8efc8ef3b85a916af2e #1.0.0
        if: contains(github.event.comment.html_url, '/pull/')
        with:
          sha: ${{ steps.upstream_branch.outputs.sha }}
          state: pending

      - name: Check out when pr #PRで起動した時
        uses: actions/checkout@v2
        if: github.event_name == 'pull_request'

      - name: Check out when comment #コメントで起動した時
        uses: actions/checkout@v2
        if: contains(github.event.comment.html_url, '/pull/')
        with:
          ref: ${{ steps.upstream_branch.outputs.head_ref }}

      - name: Cache library
        uses: actions/cache@v3
        with:
          path: Library
          key: Library-WebGL
          restore-keys: |
            Library-

      - name: Run the WebGL build
        uses: game-ci/unity-builder@v2
        env:
          UNITY_LICENSE: ${{ secrets.UNITY_LICENSE }}
        with:
          targetPlatform: WebGL
          unityVersion: 2021.3.12f1

      - name: Upload the WebGL Build
        uses: actions/upload-artifact@v2
        with:
          name: Build
          path: build
    outputs:
      commit_sha: ${{ steps.upstream_branch.outputs.head_sha }}

  notify_succeed:
    if: ${{ success() }}
    name: Notify succeed
    needs: build
    runs-on: ubuntu-latest
    steps:
      - name: Notify success status
        uses: hkusu/status-create-action@1040f888115fc10b2e0fa8efc8ef3b85a916af2e #1.0.0
        if: contains(github.event.comment.html_url, '/pull/')
        with:
          sha: ${{ needs.build.outputs.commit_sha }}
          state: success

  notify_failure:
    if: ${{ failure() }}
    name: Notify failure
    needs: build
    runs-on: ubuntu-latest
    steps:
      - name: Notify failure status
        uses: hkusu/status-create-action@1040f888115fc10b2e0fa8efc8ef3b85a916af2e #1.0.0
        if: contains(github.event.comment.html_url, '/pull/')
        with:
          sha: ${{ needs.build.outputs.commit_sha }}
          state: failure

PRのコメントをフックにする時

PRのコメントをフックにしたいときは、pr_commentのようなイベントはないためissue_commentを指定して、ワークフロー内で条件分岐を行います。コード内ではこの部分です。

if: |
    github.event_name == 'pull_request' ||
    ( contains(github.event.comment.html_url, '/pull/') && startsWith(github.event.comment.body, '/retry build') )

他の判定方法もあると思いますが、ここではgithub.event_name == 'pull_request'でPRでフックされたかどうかの確認を行なっており、2行目の( contains(github.event.comment.html_url, '/pull/') && startsWith(github.event.comment.body, '/retry build') )で、プルリクエストのコメントかどうかと、コメントの内容についての検証をしています。
ここでは/retry buildというコメントでワークフローを起動したいので、startsWith(github.event.comment.body, '/retry build')のようにコメントが/retry buildで始まっているのかどうかを確認しています。

次回

次回は、このワークフローにGitHub Pagesにデプロイ、またDiscordに通知する処理を加えます。

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