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

プライベートなリポジトリのGithub Actionsを他リポジトリで共有して運用する

Last updated at Posted at 2023-10-18

はじめに

以前、プライベートなリポジトリのGithub Actionsを他リポジトリで共有して利用する方法を調べましたが、実際に私のチームに適用したのでどのような構成や運用としたか備忘録も兼ねて記事にします。

Github公式のブログの情報やプライベートなリポジトリで共有可能とする設定は以前の記事参照。

利用・運用方法

アクションの利用

  • 以下のドキュメントにもありますが、アクションのymlファイルは制約としてaction.yml or action.yaml というファイル名にする必要があります。
  • 公式ドキュメントでは、 ./.github/actions/ディレクトリに 各アクションのディレクトリを用意して配置する方法が紹介されています。
    • 例えば、./github/actions/{Action名}/action.yml といったかたちです。

アクションの定義

# ./github/actions/testaction/action.ymlとして配置しています
name: test action

runs:
  using: composite
  steps:
    - name: echo
      id: echo
      shell: bash
      run: |
        echo "sample"

利用箇所

# アクションが定義されているリポジトリとは別のリポジトリです
name: use action

on:
  workflow_dispatch:
  
jobs:
  call_private_action:
    runs-on: ubuntu-latest
    steps:
     - uses: UserABC/RepositoryABC/.github/actions/testaction@main

ワークフローの利用

  • actisonの共有はただusesで使うだけで問題ないのですがworkflowsの共有は以下の注意点があります。
    • 共有元となるワークフローのon条件には、workflow_callとし再利用可能ワークフローとして定義すること
    • 呼び出し側では、ワークフローはStepのタスクとしてではなくてjobsの直下で呼び出さないといけない

具体的には以下のように書かないとアクションがエラーとなってしまうため注意が必要です。

ワークフローの定義

on:
  # inputs と secrets を使用して、ワークフローへの入力やシークレットも定義できます。
  workflow_call:
  
name: test workflow
jobs:
  test_job:
    name: test workflow
    runs-on: ubuntu-latest
    steps:
      - name: echo
        id: echo
        shell: bash
        run: |
          echo "sample"

利用箇所

# ワークフローが定義されているリポジトリとは別のリポジトリです
name: use workflow

on:
  workflow_dispatch:
  
jobs:
  call_private_workflow:
    uses: UserABC/RepositoryABC/.github/workflows/testworkflow.yml@main

リポジトリへの格納について

リポジトリには、共有して利用できるアクションあるいはワークフローを.github/workflows以下に以下の構造で配置して運用します。
アクションやワークフローの利用で説明したように制約や条件を満たすように作成してください。

- .github
  - actions
    - {アクションの名称A}
      - action.yml
    - {アクションの名称B}
      - action.yml
    - ・・・
  - workflows
    - {ワークフローの名称A}.yml
    - {ワークフローの名称B}.yml
    - ・・・
2
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
2
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?