简介
在本指南中,您将了解创建和使用打包的复合操作所需的组件。为了将本指南重点放在打包操作所需的组件上,操作代码的功能是最小的。该操作打印“Hello World”,然后打印“Goodbye”,或者如果您提供自定义名称,它会打印“Hello [who-to-greet]”,然后打印“Goodbye”。该操作还将随机数映射到random-number输出变量,并运行名为goodbye.sh的脚本。
完成此项目后,您应该了解如何构建自己的复合操作并在工作流中对其进行测试。
警告:在创建工作流和操作时,您应始终考虑您的代码是否可能会执行来自潜在攻击者的不可信输入。某些上下文应被视为不可信输入,因为攻击者可能会插入自己的恶意内容。有关更多信息,请参阅“GitHub Actions 的安全强化”。
复合操作和可重用工作流
复合操作允许您将一系列工作流作业步骤收集到单个操作中,然后您可以将其作为单个作业步骤在多个工作流中运行。可重用工作流提供了另一种避免重复的方法,允许您从其他工作流中运行完整的工作流。有关更多信息,请参阅“避免重复”。
先决条件
在开始之前,您将在 GitHub.com 上创建一个仓库。
- 
在 GitHub.com 上创建一个新的公共仓库。您可以选择任何仓库名称,或使用以下 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.shShell chmod +x goodbye.sh chmod +x goodbye.shShell 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 pushShell git add goodbye.sh git commit -m "Add goodbye script" git push git add goodbye.sh git commit -m "Add goodbye script" git pushShell git commit -m "Add goodbye script" git push git commit -m "Add goodbye script" git push
创建操作元数据文件
- 
在 `hello-world-composite-action` 仓库中,创建一个名为 `action.yml` 的新文件,并添加以下示例代码。有关此语法格式的更多信息,请参阅 "GitHub 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 Actions 元数据语法"。 有关如何使用 `github.action_path` 的更多信息,请参阅 "上下文"。 
- 
在终端中,将 `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` 输入替换为你的名字。
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 "$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@v4
      - id: foo
        uses: actions/hello-world-composite-action@v1
        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" 脚本的结果,以及一个随机数。
GitHub.com 上的复合操作示例
你可以在 GitHub.com 上找到许多复合操作的示例。