はじめに
このガイドでは、パッケージ化された複合アクションを作成して使用するために必要な基本コンポーネントについて説明します。 アクションのパッケージ化に必要なコンポーネントのガイドに焦点を当てるため、アクションのコードの機能は最小限に留めます。 アクションは「Hello World」と「Goodbye」を出力するか、カスタムの名前を指定すると「Hello [who-to-greet]」と「Goodbye」を出力します。 このアクションでは、乱数も random-number
出力変数にマップされて、goodbye.sh
という名前のスクリプトが実行されます。
このプロジェクトを完了すれば、独自の複合アクションを作成し、それをワークフローでテストする方法を理解できます。
警告: ワークフローとアクションを作成するときは、コードが攻撃者からの信頼されていない入力を実行する可能性があるかどうかを常に考慮する必要があります。 攻撃者が悪意あるコンテンツを挿入してくるかもしれないので、特定のコンテキストは信頼できない入力として扱うべきです。 詳しくは、「GitHub Actions のセキュリティ強化」を参照してください。
前提条件
始める前に、GitHub.com にリポジトリを作成します。
-
GitHub.com に新しいパブリック リポジトリを作成します。 任意のリポジトリ名を選択することも、次の
hello-world-composite-action
の例を使用することもできます。 これらのファイルは、プロジェクトを GitHub Enterprise Cloudにプッシュした後で追加できます。 詳しくは、「新しいリポジトリの作成」を参照してください。 -
リポジトリをお手元のコンピューターにクローンします。 詳しくは、「リポジトリをクローンする」を参照してください。
-
ターミナルから、ディレクトリを新しいリポジトリに変更します。
Shell cd hello-world-composite-action
cd hello-world-composite-action
-
hello-world-composite-action
リポジトリで、goodbye.sh
という名前の新しいファイルを作成し、次のコード例を追加します。Bash echo "Goodbye"
echo "Goodbye"
-
ターミナルから、
goodbye.sh
実行可能ファイルを作成します。Shell chmod +x goodbye.sh
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
アクションのメタデータファイルの作成
-
hello-world-composite-action
リポジトリで、action.yml
という名前の新しいファイルを作成し、次のコード例を追加します。 この構文について詳しくは、「GitHub Actions のメタデータ構文」をご覧ください。
action.yml
```yaml copy
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:
- run: echo Hello ${{ inputs.who-to-greet }}.
shell: bash
- id: random-number-generator
run: echo "random-number=$(echo $RANDOM)" >> $GITHUB_OUTPUT
shell: bash
- run: echo "${{ github.action_path }}" >> $GITHUB_PATH
shell: bash
- run: goodbye.sh
shell: bash
```
このファイルでは、`who-to-greet` 入力を定義し、ランダムに生成された数値を `random-number` 出力変数にマップし、アクションのパスをランナーのシステム パスに追加し (実行中に `goodbye.sh` スクリプトを見つけるため)、`goodbye.sh` スクリプトを実行します。
出力の管理について詳しくは、「[AUTOTITLE](/actions/creating-actions/metadata-syntax-for-github-actions#outputs-for-composite-actions)」をご覧ください。
`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
ファイルにコピーしますが、actions/hello-world-composite-action@v1
を作成したリポジトリとタグに置き換えます。 who-to-greet
入力を自分の名前に置き換えることもできます。
.github/workflows/main.yml
on: [push] jobs: hello_world_job: runs-on: ubuntu-latest name: A job to say hello steps: - uses: actions/checkout@v4 - id: foo uses: actions/hello-world-composite-action@v1 with: who-to-greet: 'Mona the Octocat' - run: echo random-number ${{ steps.foo.outputs.random-number }} shell: bash
on: [push]
jobs:
hello_world_job:
runs-on: ubuntu-latest
name: A job to say hello
steps:
- uses: actions/checkout@v4
- id: foo
uses: actions/hello-world-composite-action@v1
with:
who-to-greet: 'Mona the Octocat'
- run: echo random-number ${{ steps.foo.outputs.random-number }}
shell: bash
リポジトリから [アクション] タブをクリックして、最新のワークフロー実行を選択します。 出力には、「Hello Mona the Octocat」、"Goodbye"スクリプトの結果、および乱数が含まれているはずです。
GitHub.com
に対する複合アクションの例
複合アクションの例は、GitHub.com に多数あります。