2
0

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.

この記事は お題は不問!Qiita Engineer Festa 2023 で記事投稿! - Qiita の参加記事です。

はじめに

GitHub Actions の機能に Workflow を再利用できるものがあります。

リポジトリごとに同じような Workflow を記述している場合、この機能を使ってまとめることができそうだったので試しに触ってみました。

再利用する Workflow へのアクセス

同一リポジトリにある場合はもちろん、別リポジトリの Workflow を参照することもできます。
また、プライベートリポジトリの Workflow も設定を許可すれば可能です。

試す

今回は、

  • my-github-workflows
  • sample-app

のリポジトリがあり、sample-app リポジトリ側で my-github-workflows リポジトリの Workflow ファイルを参照することをやります。

my-github-workflows リポジトリに共通で使いたい Workflow ファイルを用意します。

onフックでworkflow_callを指定する必要があります。
また、inputs, secretsを設定するとその値を my-github-workflows 側に渡すことができます。

なお、以下の Workflow 実行時の Context は参照元(sample-app)側のものが使用されます。

.github/workflows/notify-slack.yml
name: Notify Slack

on:
  workflow_call:
    inputs:
      message:
        required: true
        type: string
    secrets:
      slack_bot_token:
        required: true
      slack_channel_ids:
        required: true

jobs:
  notify:
    runs-on: ubuntu-latest
    timeout-minutes: 5
    steps:
      - uses: slackapi/slack-github-action@v1.24.0
        with:
          channel-id: ${{ secrets.slack_channel_ids }}
          payload: |
            {
              "blocks": [
                {
                  "type": "section",
                  "text": {
                    "type": "plain_text",
                    "text": "${{ inputs.message }}",
                    "emoji": true
                  }
                },
                {
                  "type": "section",
                  "text": {
                    "type": "mrkdwn",
                    "text": ">*<${{ env.GITHUB_REPOSITORY_URL }}|${{ github.repository }}>*\n>*Job*\n><${{ env.GITHUB_ACTIONS_JOB_URL }}|${{ github.run_id }}>"
                  }
                }
              ],
              "text": "Repository: ${{ env.GITHUB_REPOSITORY_URL }}\nJob: ${{ env.GITHUB_ACTIONS_JOB_URL }}"
            }
        env:
          SLACK_BOT_TOKEN: ${{ secrets.slack_bot_token }}
          GITHUB_REPOSITORY_URL: ${{ github.server_url}}/${{ github.repository }}
          GITHUB_ACTIONS_JOB_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}

そして、sample-app リポジトリで my-github-workflows の Workflow を参照すると完成です。

.github/workflows/sample.yml
name: Sample

on:
  workflow_dispatch:

jobs:
  sample:
    uses: "ohakutsu/my-github-workflows/.github/workflows/notify-slack.yml@main"
    with:
      message: test sample
    secrets:
      slack_bot_token: ${{ secrets.SLACK_BOT_TOKEN }}
      slack_channel_ids: ${{ secrets.SLACK_CHANNEL_IDS }}

無事動作確認できました。

さいごに

簡単にですが、Workflow 再利用を試してみました。

Workflow を入れ子のように呼び出したり、Action を使用するときと同様に@mainブランチ指定やタグ指定もできそうなので便利そうです。
他にも、Reusable workflow から output を渡すことができたりもするようで、様々な用途に使えそうです。

詳しくは、↓ に載っています。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?