Azure DevOps 管道构建 Makefile

Azure DevOps pipeline building Makefile

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

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

问题在于检测构建是否失败。在命令行脚本任务中,在 "Advanced" 部分下,只有 "Fail on Standard Error" 选项。描述说,如果向 stderr 写入任何内容,任务将失败。

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

没有 "Fail on Standard Error" 检查导致构建被标记为成功,无论有多少真正的错误。

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

命令行脚本任务只是将提供的脚本作为 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 应该可以解决问题,除非您希望脚本本身具有更复杂的逻辑。