gpt4 book ai didi

github-actions - "run |"与 github 操作中的多次运行之间的区别

转载 作者:行者123 更新时间:2023-12-04 11:06:47 24 4
gpt4 key购买 nike

有什么区别

steps:
- name: npm install, build, and test
run: |
npm ci
npm run build --if-present
npm test



steps:
- name: npm install, build, and test
- run: npm ci
- run: npm run build --if-present
- run: npm test

在 github 操作中?
我试图阅读 documentation在步骤上,但它没有提到类似的东西

最佳答案

不同之处在于,第一个示例作为具有三个命令的单个脚本执行,第二个示例作为三个不同的单行脚本执行(旁注:第二个示例无效,因为您使用 step with name 而没有 run ,i'将忽略该行)。

让我们假设一秒钟 npm运行时不创建任何输出。在第一个示例中,如果其中一个命令失败,则确定哪个命令可能会出现问题 - 您只有一个步骤标记为失败。在第二个示例中,您将确切地知道问题出在哪里,因为每个命令都是它自己的步骤。

让我们假设一秒钟 npm需要在特定的子目录中运行。我们需要记住,每一步总是从工作空间目录/repo的根目录开始,所以我们需要先进入我们的东西所在的目录。

- run: |
cd my/directory
npm ci
npm run build --if-present
npm test
- run: npm ci
working-directory: my/directory
- run: npm run build --if-present
working-directory: my/directory
- run: npm test
working-directory: my/directory

或者
- run: cd my/directory && npm ci
- run: cd my/directory && npm run build --if-present
- run: cd my/directory && npm test

让我们假设第二个 npm test只需要在 push 上运行事件,但工作流配置为运行 on: [push, pull_request]
- run:   |
npm ci
npm run build --if-present
if [ "${{ github.event_name }}" == "push" ]; then
npm test
fi
shell: bash
- run: npm ci
- run: npm run build --if-present
- run: npm test
if: github.event_name == 'push'

在操作选项卡下,处理时 pull_request事件,第二个示例将显示为...
- Run npm ci
- Run npm run build...
- Run npm test <-- this one will be grayed out

...您只需要快速浏览一下即可看到 npm test步骤被跳过。在第一个示例中,您必须首先展开 step 并检查日志以注意任何差异。

依此类推,当使用多合一步骤更容易/更好时,有许多场景,以及当逐个命令步骤是可行的方法时,也有许多场景;由您决定哪一种最适合您。

毕竟,在一天结束时,两个示例都做完全相同的事情。但是,如果在此过程中出现任何问题,选择一种方式在另一种方式上运行命令(这也会改变它们的显示方式)可能会影响准备修复所需的时间。

关于github-actions - "run |"与 github 操作中的多次运行之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59529042/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com