pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://docs.github.com/zh/actions/tutorials/create-actions/create-a-composite-action

data-next-head=""/>
Skip to main content

创建组合操作

在本教程中,你将学习如何构建组合操作。

Platform navigation

简介

在本指南中,您将了解创建和使用打包的组合操作所需的基本组件。 本指南的重点是打包操作所需的组件,因此很少讲操作代码的功能。 该操作将依次打印 "Hello World" 和 "Goodbye",如果您提供自定义名称,则将依次打印 "Hello [who-to-greet]" 和 "Goodbye"。 该操作还会将随机数映射到 random-number 输出变量,并运行名为 goodbye.sh 的脚本。

完成此项目后,您应了解如何构建自己的组合操作和在工作流程测试该操作。

警告

创建工作流和操作时,应始终考虑代码是否可能执行潜在攻击者的不受信任的输入。 某些上下文应被视为不受信任的输入,因为攻击者可能会插入自己的恶意内容。 有关详细信息,请参阅“安全使用指南”。

复合操作和可重用工作流

复合操作允许你将一系列工作流作业步骤收集到单个操作中,然后可将其作为多个工作流中的单个作业步骤运行。 可重用工作流提供了另一种避免重复的方法,允许你从其他工作流中运行完整的工作流。 有关详细信息,请参阅“重用工作流配置”。

先决条件

注意

此示例介绍如何在单独的存储库中创建组合操作。 但可以在同一存储库中创建组合操作。 有关详细信息,请参阅“创建组合操作”。

在开始之前,将在 GitHub 上创建一个存储库。

  1. 在 GitHub 上创建一个新的公共存储库。 可以选择任何存储库名称,或使用以下 hello-world-composite-action 示例。 你可以在项目推送到 GitHub 之后添加这些文件。 有关详细信息,请参阅“创建新仓库”。

  2. 将仓库克隆到计算机。 有关详细信息,请参阅“克隆仓库”。

  3. 从您的终端,将目录更改为新仓库。

    Shell
    cd hello-world-composite-action
    
  4. hello-world-composite-action 存储库中,新建一个名为 goodbye.sh 的文件以及示例代码:

    Shell
    echo "echo Goodbye" > goodbye.sh
    
  5. 在终端中,生成 goodbye.sh 可执行文件。

    Shell
    chmod +x goodbye.sh
    
Shell
chmod +x goodbye.sh
Shell
git add --chmod=+x -- goodbye.sh
  1. 从终端签入 goodbye.sh 文件。

    Shell
    git add goodbye.sh
    git commit -m "Add goodbye script"
    git push
    
Shell
git add goodbye.sh
git commit -m "Add goodbye script"
git push
Shell
git commit -m "Add goodbye script"
git push

创建操作元数据文件

  1. hello-world-composite-action 存储库中,新建一个名为 action.yml 的文件,并添加以下示例代码。 有关此语法的详细信息,请参阅“元数据语法参考”。

    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: bash
    
    

    此文件定义 who-to-greet 输入,将随机生成的数字映射到 random-number 输出变量,将操作路径添加到运行器系统路径(以在执行期间查找 goodbye.sh 脚本),并运行 goodbye.sh 脚本。

    有关管理输出的详细信息,请参阅“元数据语法参考”。

    有关如何使用 github.action_path 的详细信息,请参阅“上下文参考”。

  2. 从终端签入 action.yml 文件。

    Shell
    git add action.yml
    git commit -m "Add action"
    git push
    
  3. 从终端添加标记。 本示例使用名为 v1 的标记。 有关详细信息,请参阅“关于自定义操作”。

    Shell
    git tag -a -m "Description of this release" v1
    git push --follow-tags
    

在工作流程中测试您的操作

以下工作流代码使用你在“创建组合操作”中完成的 Hello World 操作。

将工作流代码复制到另一个存储库中的 .github/workflows/main.yml 文件中,但将 OWNERSHA 分别替换为存储库所有者和要使用的提交的 SHA。 还可以将 who-to-greet 输入替换为你的名称。

YAML
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" 脚本的结果以及随机数字。

在同一存储库中创建组合操作

  1. 创建名为 hello-world-composite-action 的新子文件夹,该文件夹可以放置在存储库中的任何子文件夹中。 但建议将其放置在 .github/actions 子文件夹中,以便于组织和整理。

  2. hello-world-composite-action 文件夹中,执行相同的步骤以创建 goodbye.sh 脚本

    Shell
    echo "echo Goodbye" > goodbye.sh
    
    Shell
    chmod +x goodbye.sh
    
Shell
chmod +x goodbye.sh
Shell
git add --chmod=+x -- goodbye.sh
Shell
git add goodbye.sh
git commit -m "Add goodbye script"
git push
Shell
git add goodbye.sh
git commit -m "Add goodbye script"
git push
Shell
git commit -m "Add goodbye script"
git push
  1. hello-world-composite-action 文件夹中,根据 创建组合操作 中的步骤创建 action.yml 文件。
  2. 使用该操作时,在 uses 键中使用组合操作的 action.yml 文件所在的文件夹的相对路径。 下面的示例假定其位于 .github/actions/hello-world-composite-action 文件夹中。
YAML
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 上找到许多组合操作示例。

pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy