教程:设置 CI/CD 步骤

教程:设置 CI/CD 步骤

本教程向您展示如何在流水线中创建和使用步骤。

步骤是可重用和可组合的作业片段。每个步骤定义了可以被其他步骤使用的结构化输入和输出。您可以在本地文件、JihuLab.com 仓库或任何其他 Git 源中配置步骤。

在本教程中,使用极狐GitLab CLI (glab) 来:

  1. 创建一个输出“hello world”的步骤。
  2. 配置一个流水线以使用该步骤。
  3. 向作业添加多个步骤。
  4. 使用远程步骤来回显所有输出。

在您开始之前#

创建一个步骤#

首先,创建一个步骤:

  • 类型为 exec
  • command 由系统的执行 API 启动。
  1. 在您的命名空间中创建一个名为 zero-to-steps 的极狐GitLab 项目:

    shell
    glab project create zero-to-steps
  2. 转到项目仓库的根目录:

    shell
    cd zero-to-steps
  3. 创建一个 step.yml 文件。

    shell
    touch step.yml
  4. 使用文本编辑器向 step.yml 添加规范:

    yaml
    spec: inputs: who: type: string default: world
    • spec 有一个名为 who 的输入。
    • 输入 who 是可选的,因为有默认值。
  5. 要向 step.yml 添加实现,在 spec 后添加第二个 YAML 文档,带有 exec 键:

    yaml
    1spec: 2 inputs: 3 who: 4 type: string 5 default: world 6--- 7exec: 8 command: 9 - bash 10 - -c 11 - echo 'hello ${{inputs.who}}'

三个短划线 (---) 将文件分成两个 YAML 文档:

  • 第一个文档是规范,类似于函数签名。
  • 第二个文档是实现,类似于函数体。

bash-c 参数启动一个 Bash shell 并从命令行参数中获取脚本输入。除了 shell 脚本,您还可以使用 command 执行像 dockerterraform 这样的程序。

echo 'hello ${{inputs.who}}' 参数在 ${{}} 之间包含一个表达式。表达式在最后一刻被评估,并可以访问当前的执行上下文。此表达式访问 inputs 并读取 who 的值:

  • 如果 who 是由调用者提供的,该值将替换表达式。
  • 如果 who 被省略,那么默认 world 将替换表达式。

配置流水线以使用该步骤#

  1. 在仓库的根目录下创建一个 .gitlab-ci.yml 文件:

    shell
    touch .gitlab-ci.yml
  2. .gitlab-ci.yml 中添加以下作业:

    yaml
    hello-world: run: - name: hello_world step: .
    • run 关键字有一个步骤调用列表。
      • 每个调用都有一个 name,以便您可以在后续步骤中引用输出。
      • 每个调用指定一个要运行的 step。本地引用 (.) 指向仓库的根目录。
  3. 提交两个文件并推送项目仓库。这将触发一个运行作业的流水线:

    shell
    git add . git commit -m 'Part 1 complete' git push --set-upstream origin main glab ci status
  4. 在“查看日志”下关注作业,直到流水线完成。以下是成功作业的示例:

    shell
    1Step Runner version: a7c7c8fd 2See https://jihulab.com/gitlab-cn/step-runner/-/blob/main/CHANGELOG.md for changes. 3... 4hello world 5Cleaning up project directory and file based variables 6Job succeeded

现在您已经创建并使用了您的第一个步骤!

向作业添加多个步骤#

您可以在一个作业中包含多个步骤。

  1. .gitlab-ci.yml 文件中,向您的作业添加另一个名为 hello_steps 的步骤:

    yaml
    1hello-world: 2 run: 3 - name: hello_world 4 step: . 5 - name: hello_steps 6 step: . 7 inputs: 8 who: gitlab steps

    hello_steps 步骤提供了一个非默认输入 who,值为 gitlab steps

  2. 提交并推送更改:

    shell
    git commit -a -m 'Added another step' git push glab ci status
  3. 在终端中选择 查看日志 并跟踪流水线,直到它完成。以下是成功输出的示例:

    shell
    1Step Runner version: a7c7c8fd 2See https://jihulab.com/gitlab-cn/step-runner/-/blob/main/CHANGELOG.md for changes. 3... 4hello world 5hello gitlab steps 6Cleaning up project directory and file based variables 7Job succeeded

重构您的步骤#

要重构您的步骤,将它们从 .gitlab-ci.yml 移动到一个专用文件:

  1. 将您创建的第一个步骤移动到一个名为 hello 的目录中:

    shell
    mkdir hello mv step.yml hello/
  2. 在仓库的根目录下创建一个新步骤。

    shell
    touch step.yml
  3. 向新的 step.yml 添加以下配置:

    yaml
    1spec: 2--- 3run: 4 - name: hello_world 5 step: ./hello 6 - name: hello_steps 7 step: ./hello 8 inputs: 9 who: gitlab steps

    这个新步骤没有输入,所以 spec 是空的。 它是一个 steps 类型,语法与 .gitlab-ci.yml 中的步骤相同。 但是,本地引用现在指向 hello 目录中的步骤。

  4. 要使用新步骤,请修改 .gitlab-ci.yml

    yaml
    hello-world: run: - name: hello_everybody step: .

    现在您的作业仅调用没有输入的新步骤。您已将作业的详细信息重构到一个单独的文件中。

  5. 提交并推送更改:

    shell
    git add . git commit -m 'Refactored step config' git push glab ci status
  6. 在终端中选择 查看日志

  7. 要验证重构的步骤是否与您最初创建的步骤执行相同的功能,请查看日志输出。日志输出应与您之前创建的步骤的输出匹配。以下是一个示例:

    shell
    $ /step-runner ci hello world hello gitlab steps Cleaning up project directory and file based variables Job succeeded

向步骤添加输出#

向您的 hello 步骤添加输出。

  1. hello/step.yml 中,向 spec 添加一个 outputs 结构:

    yaml
    1spec: 2 inputs: 3 who: 4 type: string 5 default: world 6 outputs: 7 greeting: 8 type: string 9--- 10exec: 11 command: 12 - bash 13 - -c 14 - echo '{"name":"greeting","value":"hello ${{inputs.who}}"}' | tee ${{output_file}}
    • 在这个 spec 中,您定义了一个没有默认值的单一输出 greeting。因为没有默认值,输出 greeting 是必需的。
    • 输出以 JSON 行格式写入运行时提供的 ${{output_file}} 文件。写入输出文件的每一行必须是一个具有两个键 namevalue 的 JSON 对象。
    • 此步骤运行 echo '{"name":"greeting","value":"hello ${{inputs.who}}"}' 并将输出发送到作业日志和输出文件(tee ${{output_file}})。
  2. step.yml 中,向步骤添加一个输出:

    yaml
    1spec: 2 outputs: 3 all_greetings: 4 type: string 5--- 6run: 7 - name: hello_world 8 step: ./hello 9 - name: hello_steps 10 step: ./hello 11 inputs: 12 who: gitlab steps 13outputs: 14 all_greetings: "${{steps.hello_world.outputs.greeting}} and ${{steps.hello_steps.outputs.greeting}}"

    现在,您已向此步骤添加一个名为 all_greetings 的输出。

    此输出展示了表达式语法:${{steps.hello_world.outputs.greeting}}all_greetings 读取两个子步骤 hello_worldhello_steps 的输出。 两个子步骤的输出被连接成一个字符串输出。

使用远程步骤#

在您提交和运行代码之前,向您的作业添加另一个步骤,以查看主 step.yml 的最终 all_greetings 输出。

此步骤调用引用了一个名为 echo-step 的远程步骤。 echo 步骤接受一个输入 echo,将其打印到日志,并将其作为 echo 输出。

  1. 编辑 .gitlab-ci.yml

    yaml
    1hello-world: 2 run: 3 - name: hello_everybody 4 step: . 5 - name: all_my_greetings 6 step: gitlab.com/gitlab-org/ci-cd/runner-tools/echo-step@main 7 inputs: 8 echo: "all my greetings say ${{steps.hello_everybody.outputs.all_greetings}}"
  2. 提交并推送更改:

    shell
    git commit -a -m 'Added outputs' git push glab ci status
  3. 在“查看日志”下关注作业,直到流水线完成。以下是成功输出的示例:

    shell
    1Step Runner version: a7c7c8fd 2See https://jihulab.com/gitlab-cn/step-runner/-/blob/main/CHANGELOG.md for changes. 3... 4{"name":"greeting","value":"hello world"} 5{"name":"greeting","value":"hello gitlab steps"} 6all my greetings say hello world and hello gitlab steps 7Cleaning up project directory and file based variables 8Job succeeded

就是这样!您刚刚在流水线中创建并实现了步骤。有关步骤语法的更多信息,请参见 CI/CD 步骤