{"meta":{"title":"複合アクションを作成する","intro":"このチュートリアルでは、複合アクションを構築する方法について説明します。","product":"GitHub Actions","breadcrumbs":[{"href":"/ja/actions","title":"GitHub Actions"},{"href":"/ja/actions/tutorials","title":"チュートリアル"},{"href":"/ja/actions/tutorials/create-actions","title":"アクションを作成する"},{"href":"/ja/actions/tutorials/create-actions/create-a-composite-action","title":"複合アクションを作成する"}],"documentType":"article"},"body":"# 複合アクションを作成する\n\nこのチュートリアルでは、複合アクションを構築する方法について説明します。\n\n## はじめに\n\nこのガイドでは、パッケージ化された複合アクションを作成して使用するために必要な基本コンポーネントについて説明します。 アクションのパッケージ化に必要なコンポーネントのガイドに焦点を当てるため、アクションのコードの機能は最小限に留めます。 アクションによって \"Hello World\" と \"Goodbye\" が出力されます。または、カスタム名を指定すると、\"Hello \\[who-to-greet]\" と \"Goodbye\" が出力されます。 このアクションでは、乱数も `random-number` 出力変数にマップされて、`goodbye.sh` という名前のスクリプトが実行されます。\n\nこのプロジェクトを完了すれば、独自の複合アクションを作成し、それをワークフローでテストする方法を理解できます。\n\n> \\[!WARNING]\n> ワークフローとアクションを作成するときは、攻撃者によってコードが信頼されていない入力を実行する可能性があるかどうかを常に考慮する必要があります。 攻撃者が悪意あるコンテンツを挿入してくるかもしれないので、特定のコンテキストは信頼できない入力として扱うべきです。 詳しくは、「[セキュリティで保護された使用に関するリファレンス](/ja/actions/security-guides/security-hardening-for-github-actions#understanding-the-risk-of-script-injections)」をご覧ください。\n\n### 複合アクションと再利用可能なワークフロー\n\n複合アクションを使用すると、一連のワークフロー ジョブ ステップを 1 つのアクションに収集し、複数のワークフローで 1 つのジョブ ステップとして実行できます。 再利用可能なワークフローは、他のワークフロー内から完全なワークフローを実行できるようにすることで、重複を回避する別の方法を提供します。 詳しくは、「[ワークフロー構成の再利用](/ja/actions/using-workflows/avoiding-duplication)」をご覧ください。\n\n## 前提条件\n\n> \\[!NOTE]\n> この例では、別のリポジトリ内に複合アクションを作成する方法について説明します。 ただし、同じリポジトリ内に複合アクションを作成することができます。 詳しくは、「[複合アクションを作成する](/ja/actions/creating-actions/creating-a-composite-action#creating-a-composite-action-within-the-same-repository)」をご覧ください。\n\n開始する前に、 GitHubにリポジトリを作成します。\n\n1. ```\n          GitHubに新しいパブリック リポジトリを作成します。 任意のリポジトリ名を選択することも、次の `hello-world-composite-action` の例を使用することもできます。 これらのファイルは、プロジェクトが GitHubにプッシュされた後に追加できます。 詳しくは、「[AUTOTITLE](/repositories/creating-and-managing-repositories/creating-a-new-repository)」をご覧ください。\n   ```\n\n2. リポジトリをお手元のコンピューターにクローンします。 詳しくは、「[リポジトリをクローンする](/ja/repositories/creating-and-managing-repositories/cloning-a-repository)」をご覧ください。\n\n3. ターミナルから、ディレクトリを新しいリポジトリに変更します。\n\n   ```shell copy\n   cd hello-world-composite-action\n   ```\n\n4. ```\n          `hello-world-composite-action` リポジトリで、`goodbye.sh` という名前の新しいファイルを次のコード例で作成します。\n   ```\n\n   ```shell copy\n   echo \"echo Goodbye\" > goodbye.sh\n   ```\n\n5. ターミナルから、`goodbye.sh` 実行可能ファイルを作成します。\n\n   <div class=\"ghd-tool linux\">\n\n   ```shell copy\n   chmod +x goodbye.sh\n   ```\n\n   </div>\n\n   <div class=\"ghd-tool mac\">\n\n   ```shell copy\n   chmod +x goodbye.sh\n   ```\n\n   </div>\n\n   <div class=\"ghd-tool windows\">\n\n   ```shell copy\n   git add --chmod=+x -- goodbye.sh\n   ```\n\n   </div>\n\n6. ターミナルから、`goodbye.sh` ファイルをチェックインします。\n\n   <div class=\"ghd-tool linux\">\n\n   ```shell copy\n   git add goodbye.sh\n   git commit -m \"Add goodbye script\"\n   git push\n   ```\n\n   </div>\n\n   <div class=\"ghd-tool mac\">\n\n   ```shell copy\n   git add goodbye.sh\n   git commit -m \"Add goodbye script\"\n   git push\n   ```\n\n   </div>\n\n   <div class=\"ghd-tool windows\">\n\n   ```shell copy\n   git commit -m \"Add goodbye script\"\n   git push\n   ```\n\n   </div>\n\n## アクションのメタデータファイルの作成\n\n1. ```\n          `hello-world-composite-action` リポジトリで、`action.yml` という名前の新しいファイルを作成し、次のコード例を追加します。 この構文の詳細については、「[AUTOTITLE](/actions/creating-actions/metadata-syntax-for-github-actions#runs-for-composite-actions)」を参照してください。\n   ```\n\n   ```yaml copy\n   name: 'Hello World'\n   description: 'Greet someone'\n   inputs:\n     who-to-greet:  # id of input\n       description: 'Who to greet'\n       required: true\n       default: 'World'\n   outputs:\n     random-number:\n       description: \"Random number\"\n       value: ${{ steps.random-number-generator.outputs.random-number }}\n   runs:\n     using: \"composite\"\n     steps:\n       - name: Set Greeting\n         run: echo \"Hello $INPUT_WHO_TO_GREET.\"\n         shell: bash\n         env:\n           INPUT_WHO_TO_GREET: ${{ inputs.who-to-greet }}\n\n       - name: Random Number Generator\n         id: random-number-generator\n         run: echo \"random-number=$(echo $RANDOM)\" >> $GITHUB_OUTPUT\n         shell: bash\n\n       - name: Set GitHub Path\n         run: echo \"$GITHUB_ACTION_PATH\" >> $GITHUB_PATH\n         shell: bash\n         env:\n           GITHUB_ACTION_PATH: ${{ github.action_path }}\n\n       - name: Run goodbye.sh\n         run: goodbye.sh\n         shell: bash\n\n   ```\n\n   このファイルでは、`who-to-greet` 入力を定義し、ランダムに生成された数値を `random-number` 出力変数にマップし、アクションのパスをランナーのシステム パスに追加し (実行中に `goodbye.sh` スクリプトを見つけるため)、`goodbye.sh` スクリプトを実行します。\n\n   出力の管理の詳細については、「[メタデータ構文リファレンス](/ja/actions/creating-actions/metadata-syntax-for-github-actions#outputs-for-composite-actions)」を参照してください。\n\n   ```\n          `github.action_path` の使い方の詳細については、「[AUTOTITLE](/actions/learn-github-actions/contexts#github-context)」を参照してください。\n   ```\n\n2. ターミナルから、`action.yml` ファイルをチェックインします。\n\n   ```shell copy\n   git add action.yml\n   git commit -m \"Add action\"\n   git push\n   ```\n\n3. ターミナルから、タグを追加します。 この例では、`v1` という名前のタグを使用します。 詳しくは、「[カスタム アクションについて](/ja/actions/creating-actions/about-custom-actions#using-release-management-for-actions)」をご覧ください。\n\n   ```shell copy\n   git tag -a -m \"Description of this release\" v1\n   git push --follow-tags\n   ```\n\n## ワークフローでアクションをテストする\n\n次のワークフロー コードでは、「[複合アクションを作成する](/ja/actions/creating-actions/creating-a-composite-action#creating-an-action-metadata-file)」で完成した hello world アクションを使います。\n\nワークフロー コードを別のリポジトリの `.github/workflows/main.yml` ファイルにコピーしますが、`OWNER` と`SHA` をリポジトリ オーナーとコミットに使用したい SHA にそれぞれ置き換えます。\n`who-to-greet` 入力を自分の名前に置き換えることもできます。\n\n```yaml copy\non: [push]\n\njobs:\n  hello_world_job:\n    runs-on: ubuntu-latest\n    name: A job to say hello\n    steps:\n      - uses: actions/checkout@v6\n      - id: foo\n        uses: OWNER/hello-world-composite-action@SHA\n        with:\n          who-to-greet: 'Mona the Octocat'\n      - run: echo random-number \"$RANDOM_NUMBER\"\n        shell: bash\n        env:\n          RANDOM_NUMBER: ${{ steps.foo.outputs.random-number }}\n```\n\nリポジトリから **\\[アクション]** タブをクリックして、最新のワークフロー実行を選択します。 出力には、「Hello Mona the Octocat」、\"Goodbye\"スクリプトの結果、および乱数が含まれているはずです。\n\n## 同じリポジトリ内に複合アクションを作成する\n\n1. ```\n          `hello-world-composite-action` という新しいサブフォルダーを作成します。これはリポジトリ内の任意のサブフォルダーに配置できます。 ただし、整理しやすいように、`.github/actions` サブフォルダーに配置することをお勧めします。\n   ```\n\n2. ```\n          `hello-world-composite-action` フォルダーで、同じ手順を実行して `goodbye.sh` スクリプトを作成します\n   ```\n\n   ```shell copy\n   echo \"echo Goodbye\" > goodbye.sh\n   ```\n\n   <div class=\"ghd-tool linux\">\n\n   ```shell copy\n   chmod +x goodbye.sh\n   ```\n\n   </div>\n\n   <div class=\"ghd-tool mac\">\n\n   ```shell copy\n   chmod +x goodbye.sh\n   ```\n\n   </div>\n\n   <div class=\"ghd-tool windows\">\n\n   ```shell copy\n   git add --chmod=+x -- goodbye.sh\n   ```\n\n   </div>\n\n   <div class=\"ghd-tool linux\">\n\n   ```shell copy\n   git add goodbye.sh\n   git commit -m \"Add goodbye script\"\n   git push\n   ```\n\n   </div>\n\n   <div class=\"ghd-tool mac\">\n\n   ```shell copy\n   git add goodbye.sh\n   git commit -m \"Add goodbye script\"\n   git push\n   ```\n\n   </div>\n\n   <div class=\"ghd-tool windows\">\n\n   ```shell copy\n   git commit -m \"Add goodbye script\"\n   git push\n   ```\n\n   </div>\n\n3. ```\n          `hello-world-composite-action` フォルダーに、`action.yml` ファイルを[AUTOTITLE](/actions/creating-actions/creating-a-composite-action#creating-an-action-metadata-file)の手順に基づいて作成します。\n   ```\n\n4. アクションを使うときは、複合アクションの `action.yml` ファイルが配置されているフォルダーへの相対パスを `uses` キーに使います。 以下の例では、`.github/actions/hello-world-composite-action` フォルダー内にあることを前提としています。\n\n```yaml copy\non: [push]\n\njobs:\n  hello_world_job:\n    runs-on: ubuntu-latest\n    name: A job to say hello\n    steps:\n      - uses: actions/checkout@v6\n      - id: foo\n        uses: ./.github/actions/hello-world-composite-action\n        with:\n          who-to-greet: 'Mona the Octocat'\n      - run: echo random-number \"$RANDOM_NUMBER\"\n        shell: bash\n        env:\n          RANDOM_NUMBER: ${{ steps.foo.outputs.random-number }}\n```"}