1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

UnityでGitHub Actions(3) -GitHub Pagesにデプロイ・結果をDiscordに通知する

Posted at

前回と前々回の続きです。

今回は、GitHub Pagesにデプロイする部分およびビルド結果をDiscordに通知する部分を書きます。

ワークフロー

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: Deploy to GitHub Pages
        uses: JamesIves/github-pages-deploy-action@4.1.3
        with:
          branch: gh-pages
          folder: build/WebGL/WebGL
    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

      - name: Discord Notify
        uses: Ilshidur/action-discord@0c4b27844ba47cb1c7bee539c8eead5284ce9fa9 #0.3.2
        env:
          DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK_URL }}
          DISCORD_USERNAME: Build Bot
          DISCORD_EMBEDS: >
            [{"title":"Build Succeeded","color":4781122,"description":
            "[Deployed Page](https://${{ github.repository_owner }}.github.io/${{ github.event.repository.name }})"}]

  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

      - name: Discord Notify
        uses: Ilshidur/action-discord@0c4b27844ba47cb1c7bee539c8eead5284ce9fa9 #0.3.2
        env:
          DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK_URL }}
          DISCORD_USERNAME: Build Bot
          DISCORD_EMBEDS: '[{"title":"Build Failed","color":13959168}]'

secretsの設定

Settings/Secrets/Actionsから、DISCORD_WEBHOOK_URLとして通知したいチャンネルのウェブフックURLを設定します。
https://discord.com/api/webhooks/[id]/[token]のような形式のURLです。
スクリーンショット 2022-12-19 23.46.21.png

GitHub Pagesの有効化

1回このワークフローを走らせて成功すると、リポジトリにgh-pagesブランチが作成されます。
このブランチをSettings/PagesBranchに設定します。
スクリーンショット 2022-12-19 23.50.07.png

結果

このようにウェブフックに設定したチャンネルにビルド通知が送られるようになります(アイコンは手動で設定したものです)。
Deployed Pageからは、GitHub Pagesにデプロイしたページに直接アクセスすることができます。
スクリーンショット 2022-12-19 23.52.49.png

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?