乐闻世界logo
搜索文章和话题

How to combine two action.yml files in one repo?

1个答案

1

In GitHub Actions, it is common to combine multiple action.yml files, which typically means you need to integrate multiple independently defined actions into a single workflow.

First, we need to understand that action.yml files are actually configuration files used to define individual actions. If you want to use multiple actions in a repository, you typically reference them in a workflow file, such as .github/workflows/main.yml.

Steps to Follow:

  1. Define Actions: Each action should have its own directory and action.yml file. For example:

    • action-a/action.yml
    • action-b/action.yml
  2. Create a Workflow: Create a workflow file in the .github/workflows directory, such as main.yml.

  3. Reference Actions in the Workflow: In the workflow file main.yml, you can reference actions in the repository using the uses keyword. For example:

yaml
name: Example Workflow on: [push] jobs: job1: runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v2 - name: Run Action A uses: ./action-a - name: Run Action B uses: ./action-b

Here, uses: ./action-a and uses: ./action-b point to the two different actions defined in the repository.

Example:

Suppose you have two actions: one for setting up the environment (e.g., configuring Node.js) and another for running tests. You can structure your repository as follows:

  • .github/actions/setup-node/action.yml: This file defines the action for setting up the Node.js environment.
  • .github/actions/run-tests/action.yml: This file defines the action for running tests.

Then, your workflow file .github/workflows/main.yml might look like this:

yaml
name: CI Workflow on: [push, pull_request] jobs: build-and-test: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v2 - name: Setup Node.js uses: ./.github/actions/setup-node - name: Run Tests uses: ./.github/actions/run-tests

In this example, each action is organized in a separate directory and is called individually through the uses field in the workflow. This allows you to combine and reuse actions flexibly across different workflows.

2024年6月29日 12:07 回复

你的答案