メモ
GitHub ホステッド ランナーは、現在 GitHub Enterprise Server ではサポートされていません。
はじめに
このガイドでは、パッケージ化された複合アクションを作成して使用するために必要な基本コンポーネントについて説明します。 アクションのパッケージ化に必要なコンポーネントのガイドに焦点を当てるため、アクションのコードの機能は最小限に留めます。 アクションによって "Hello World" と "Goodbye" が出力されます。または、カスタム名を指定すると、"Hello [who-to-greet]" と "Goodbye" が出力されます。 このアクションでは、乱数も random-number 出力変数にマップされて、goodbye.sh という名前のスクリプトが実行されます。
このプロジェクトを完了すれば、独自の複合アクションを作成し、それをワークフローでテストする方法を理解できます。
データ 再利用可能なアクション.コンテキスト挿入警告 %}
複合アクションと再利用可能なワークフロー
複合アクションを使用すると、一連のワークフロー ジョブ ステップを 1 つのアクションに収集し、複数のワークフローで 1 つのジョブ ステップとして実行できます。 再利用可能なワークフローは、他のワークフロー内から完全なワークフローを実行できるようにすることで、重複を回避する別の方法を提供します。 詳しくは、「ワークフロー構成の再利用」をご覧ください。
前提条件
メモ
この例では、別のリポジトリ内に複合アクションを作成する方法について説明します。 ただし、同じリポジトリ内に複合アクションを作成することができます。 詳しくは、「複合アクションを作成する」をご覧ください。
始める前に、GitHub にリポジトリを作成します。
-
GitHub に新しいパブリック リポジトリを作成します。 任意のリポジトリ名を選択することも、次の
hello-world-composite-actionの例を使用することもできます。 これらのファイルは、プロジェクトが GitHub にプッシュされた後に追加できます。 詳しくは、「新しいリポジトリの作成」をご覧ください。 -
リポジトリをお手元のコンピューターにクローンします。 詳しくは、「リポジトリをクローンする」をご覧ください。
-
ターミナルから、ディレクトリを新しいリポジトリに変更します。
Shell cd hello-world-composite-action
cd hello-world-composite-action -
`hello-world-composite-action` リポジトリで、`goodbye.sh` という名前の新しいファイルを次のコード例で作成します。Shell echo "echo Goodbye" > goodbye.sh
echo "echo Goodbye" > goodbye.sh -
ターミナルから、
goodbye.sh実行可能ファイルを作成します。Shell chmod +x goodbye.sh
chmod +x goodbye.sh
chmod +x goodbye.sh
chmod +x goodbye.sh
git add --chmod=+x -- goodbye.sh
git add --chmod=+x -- goodbye.sh
-
ターミナルから、
goodbye.shファイルをチェックインします。Shell git add goodbye.sh git commit -m "Add goodbye script" git push
git add goodbye.sh git commit -m "Add goodbye script" git push
git add goodbye.sh git commit -m "Add goodbye script" git push ```</div> <div class="ghd-tool windows"> ```shell copy git commit -m "Add goodbye script" git push
git add goodbye.sh
git commit -m "Add goodbye script"
git push
```</div>
<div class="ghd-tool windows">
```shell copy
git commit -m "Add goodbye script"
git push
アクションのメタデータファイルの作成
-
`hello-world-composite-action` リポジトリで、`action.yml` という名前の新しいファイルを作成し、次のコード例を追加します。 この構文の詳細については、「[AUTOTITLE](/actions/creating-actions/metadata-syntax-for-github-actions#runs-for-composite-actions)」を参照してください。YAML name: 'Hello World' description: 'Greet someone' inputs: who-to-greet: # id of input description: 'Who to greet' required: true default: 'World' outputs: random-number: description: "Random number" value: ${{ steps.random-number-generator.outputs.random-number }} runs: using: "composite" steps: - name: Set Greeting run: echo "Hello $INPUT_WHO_TO_GREET." shell: bash env: INPUT_WHO_TO_GREET: ${{ inputs.who-to-greet }} - name: Random Number Generator id: random-number-generator run: echo "random-number=$(echo $RANDOM)" >> $GITHUB_OUTPUT shell: bash - name: Set GitHub Path run: echo "$GITHUB_ACTION_PATH" >> $GITHUB_PATH shell: bash env: GITHUB_ACTION_PATH: ${{ github.action_path }} - name: Run goodbye.sh run: goodbye.sh shell: bashname: 'Hello World' description: 'Greet someone' inputs: who-to-greet: # id of input description: 'Who to greet' required: true default: 'World' outputs: random-number: description: "Random number" value: ${{ steps.random-number-generator.outputs.random-number }} runs: using: "composite" steps: - name: Set Greeting run: echo "Hello $INPUT_WHO_TO_GREET." shell: bash env: INPUT_WHO_TO_GREET: ${{ inputs.who-to-greet }} - name: Random Number Generator id: random-number-generator run: echo "random-number=$(echo $RANDOM)" >> $GITHUB_OUTPUT shell: bash - name: Set GitHub Path run: echo "$GITHUB_ACTION_PATH" >> $GITHUB_PATH shell: bash env: GITHUB_ACTION_PATH: ${{ github.action_path }} - name: Run goodbye.sh run: goodbye.sh shell: bashこのファイルでは、
who-to-greet入力を定義し、ランダムに生成された数値をrandom-number出力変数にマップし、アクションのパスをランナーのシステム パスに追加し (実行中にgoodbye.shスクリプトを見つけるため)、goodbye.shスクリプトを実行します。出力の管理の詳細については、「メタデータ構文リファレンス」を参照してください。
`github.action_path` の使い方の詳細については、「[AUTOTITLE](/actions/learn-github-actions/contexts#github-context)」を参照してください。 -
ターミナルから、
action.ymlファイルをチェックインします。Shell git add action.yml git commit -m "Add action" git push
git add action.yml git commit -m "Add action" git push -
ターミナルから、タグを追加します。 この例では、
v1という名前のタグを使用します。 詳しくは、「カスタム アクションについて」をご覧ください。Shell git tag -a -m "Description of this release" v1 git push --follow-tags
git tag -a -m "Description of this release" v1 git push --follow-tags
ワークフローでアクションをテストする
次のワークフロー コードでは、「複合アクションを作成する」で完成した hello world アクションを使います。
ワークフロー コードを別のリポジトリの .github/workflows/main.yml ファイルにコピーしますが、OWNER とSHA をリポジトリ オーナーとコミットに使用したい SHA にそれぞれ置き換えます。
who-to-greet 入力を自分の名前に置き換えることもできます。
on: [push]
jobs:
hello_world_job:
runs-on: ubuntu-latest
name: A job to say hello
steps:
- uses: actions/checkout@v5
- id: foo
uses: OWNER/hello-world-composite-action@SHA
with:
who-to-greet: 'Mona the Octocat'
- run: echo random-number "$RANDOM_NUMBER"
shell: bash
env:
RANDOM_NUMBER: ${{ steps.foo.outputs.random-number }}
on: [push]
jobs:
hello_world_job:
runs-on: ubuntu-latest
name: A job to say hello
steps:
- uses: actions/checkout@v5
- id: foo
uses: OWNER/hello-world-composite-action@SHA
with:
who-to-greet: 'Mona the Octocat'
- run: echo random-number "$RANDOM_NUMBER"
shell: bash
env:
RANDOM_NUMBER: ${{ steps.foo.outputs.random-number }}
リポジトリから [アクション] タブをクリックして、最新のワークフロー実行を選択します。 出力には、「Hello Mona the Octocat」、"Goodbye"スクリプトの結果、および乱数が含まれているはずです。
同じリポジトリ内に複合アクションを作成する
-
`hello-world-composite-action` という新しいサブフォルダーを作成します。これはリポジトリ内の任意のサブフォルダーに配置できます。 ただし、整理しやすいように、`.github/actions` サブフォルダーに配置することをお勧めします。 -
`hello-world-composite-action` フォルダーで、同じ手順を実行して `goodbye.sh` スクリプトを作成しますShell echo "echo Goodbye" > goodbye.sh
echo "echo Goodbye" > goodbye.shShell chmod +x goodbye.sh
chmod +x goodbye.sh
chmod +x goodbye.sh
chmod +x goodbye.sh
git add --chmod=+x -- goodbye.sh
git add --chmod=+x -- goodbye.sh
git add goodbye.sh git commit -m "Add goodbye script" git push
git add goodbye.sh
git commit -m "Add goodbye script"
git push
git add goodbye.sh git commit -m "Add goodbye script" git push ```</div> <div class="ghd-tool windows"> ```shell copy git commit -m "Add goodbye script" git push
git add goodbye.sh
git commit -m "Add goodbye script"
git push
```</div>
<div class="ghd-tool windows">
```shell copy
git commit -m "Add goodbye script"
git push
-
`hello-world-composite-action` フォルダーに、`action.yml` ファイルを[AUTOTITLE](/actions/creating-actions/creating-a-composite-action#creating-an-action-metadata-file)の手順に基づいて作成します。 - アクションを使うときは、複合アクションの
action.ymlファイルが配置されているフォルダーへの相対パスをusesキーに使います。 以下の例では、.github/actions/hello-world-composite-actionフォルダー内にあることを前提としています。
on: [push]
jobs:
hello_world_job:
runs-on: ubuntu-latest
name: A job to say hello
steps:
- uses: actions/checkout@v5
- id: foo
uses: ./.github/actions/hello-world-composite-action
with:
who-to-greet: 'Mona the Octocat'
- run: echo random-number "$RANDOM_NUMBER"
shell: bash
env:
RANDOM_NUMBER: ${{ steps.foo.outputs.random-number }}
on: [push]
jobs:
hello_world_job:
runs-on: ubuntu-latest
name: A job to say hello
steps:
- uses: actions/checkout@v5
- id: foo
uses: ./.github/actions/hello-world-composite-action
with:
who-to-greet: 'Mona the Octocat'
- run: echo random-number "$RANDOM_NUMBER"
shell: bash
env:
RANDOM_NUMBER: ${{ steps.foo.outputs.random-number }}
GitHub
での複合アクションの例
複合アクションの例は、GitHub に多数あります。
-
[microsoft/action-python](https://github.com/microsoft/action-python) -
[microsoft/gpt-review](https://github.com/microsoft/gpt-review) -
[tailscale/github-action](https://github.com/tailscale/github-action)