【问题标题】:Azure DevOps pipeline building MakefileAzure DevOps 管道构建 Makefile
【发布时间】:2020-02-06 02:17:51
【问题描述】:

我正在尝试为 C 项目的 CI 设置 Azure DevOps 构建管道,该项目使用 gnu make 构建,最终运行 gcc 进行编译和链接。

虽然我不确定要将哪个任务添加到管道以构建 Makefile 项目,但我尝试使用一个简单的命令行脚本任务,它只是在正确的目录中执行“make”。

问题在于检测构建是否失败。在命令行脚本任务中,在“高级”部分下,只有“标准错误失败”选项。描述说如果有任何东西被写入stderr,任务就会失败。

因此,如果出现编译或链接错误,任务确实会失败,这是预期的行为。但是,gcc 也会将所有编译警告写入 stderr,这也会导致任务失败,这不是我们想要的。

不检查“标准错误失败”会导致构建被标记为成功,无论有多少实际错误。

当同一构建作为 Jenkins 作业的一部分运行时,它能够以某种方式正确解释 gcc/make 输出,并且只有在报告实际错误时才会使构建失败。有没有办法在 Azure 管道中复制相同的行为?缺少修复所有警告或重新设计构建过程以不向 stderr 写入任何内容,然后使用其他方式测试构建是否成功?

【问题讨论】:

  • 在 stderr 中搜索“错误”这个词是一个选项吗?
  • 这是一个选项,但我觉得这是一种解决方法。我不敢相信没有一种简单的内置方法来检测制造的成功或失败。

标签: c gcc azure-devops azure-pipelines gnu-make


【解决方案1】:

命令行脚本任务只是将提供的脚本作为 shell 脚本运行并检查其退出代码(非零表示错误)。问题是脚本的退出代码是最后一条语句的退出代码,默认情况下(除非给出-e 选项)无论错误如何,shell 都会继续运行,即:

失败的命令

步骤内容:

make -f nonexisting.mk

结果:

========================== Starting Command Output ===========================
/bin/bash --noprofile --norc /home/vsts/work/_temp/bc2da066-dd8a-432d-b15f-b9c2bf7a8e1f.sh
make: nonexisting.mk: No such file or directory
make: *** No rule to make target 'nonexisting.mk'.  Stop.
##[error]Bash exited with code '2'.
Finishing: Command Line Script

make 命令返回退出代码 2 失败,任务被标记为失败。接下来的任务被跳过了。

失败的命令被下一条语句覆盖

步骤内容:

make -f nonexisting.mk
echo Do whatever else

结果:

========================== Starting Command Output ===========================
/bin/bash --noprofile --norc /home/vsts/work/_temp/0b2ceea2-2821-4866-83c7-5d37a832ffe5.sh
make: nonexisting.mk: No such file or directory
make: *** No rule to make target 'nonexisting.mk'.  Stop.
Do whatever else
Finishing: Command Line Script

make 命令失败,但 shell 继续执行下一条语句 (echo),该语句成功,因此退出代码为 0,任务未检测到错误,管道成功执行。

使 shell 在出错时停止

步骤内容:

set -e
make -f nonexisting.mk
echo Do whatever else

结果:

========================== Starting Command Output ===========================
/bin/bash --noprofile --norc /home/vsts/work/_temp/7f6644d3-408c-4b5b-b4d3-1f544e8a80ec.sh
make: nonexisting.mk: No such file or directory
make: *** No rule to make target 'nonexisting.mk'.  Stop.
##[error]Bash exited with code '2'.
Finishing: Command Line Script

shell 可以在遇到任何错误时退出;当make 命令失败时,脚本终止,echo 命令不执行,退出码为失败命令的退出码。任务再次检测到这并终止管道。

顺便说一句。这是 Jenkins 运行 shell 脚本的标准方式,默认传递 -e,因此它可以在 Jenkins 上运行而无需明确执行任何操作。

您可能需要查看您的任务脚本中是否有覆盖make 退出代码的命令。除非您希望脚本本身具有更复杂的逻辑,否则脚本中的 set -e 可能会起到作用。

【讨论】:

  • 像魅力一样工作。谢谢!
猜你喜欢
  • 2021-01-21
  • 2022-11-24
  • 2020-01-30
  • 1970-01-01
  • 1970-01-01
  • 2019-06-12
  • 1970-01-01
  • 1970-01-01
  • 2021-04-28
相关资源
最近更新 更多