如何使用多个 shell 命令设置 Gitlab ci 阶段
How to setup Gitlab ci stage with multiple shell commands
给定此示例 .Net CI Gitlab 的管道配置
image: mcr.microsoft.com/dotnet/sdk:5.0
stages:
- build
build:
stage: build
script:
- |
BUILD_WARNINGS_LOG_FILE="buildWarnings.log";
dotnet build -consoleloggerparameters:"Summary;Verbosity=normal" -m -p:"WarnLevel=5;EnforceCodeStyleInBuild=true" -t:"clean,build" -fl1 "/flp1:logFile=$BUILD_WARNINGS_LOG_FILE;warningsonly";
if [ $? -eq 0 ] && [ $(wc -l < $BUILD_WARNINGS_LOG_FILE) -gt 0 ]
then
# do stuff here
fi
管道因语法无效而失败。这是因为它不知道有多个 shell 命令要执行。
这个 .sh
文件可以在我的本地机器上运行
#!/bin/bash
BUILD_WARNINGS_LOG_FILE="buildWarnings.log";
dotnet build --output build -consoleloggerparameters:"Summary;Verbosity=normal" -m -p:"WarnLevel=5;EnforceCodeStyleInBuild=true" -t:"clean,build" -fl1 "/flp1:logFile=$BUILD_WARNINGS_LOG_FILE;warningsonly";
if [ $? -eq 0 ] && [ $(wc -l < $BUILD_WARNINGS_LOG_FILE) -gt 0 ]
then
# do stuff here
fi
那么如何将它正确地嵌入到 Gitlab ci 阶段?
可以通过三种方式做到这一点:
- 将您的 shell 脚本添加到图像中,以便您可以执行它:(在您的情况下,需要基于
mcr.microsoft.com/dotnet/sdk:5.0
创建一个新图像)
image: mcr.microsoft.com/dotnet/sdk:5.0
stages:
- build
build:
stage: build
script:
- bash /path/to/your/script.sh
- 在 运行 时创建 shell 脚本(只需将脚本回显到 shell 文件中并使其可执行)
image: mcr.microsoft.com/dotnet/sdk:5.0
stages:
- build
build:
stage: build
script:
- echo "BUILD_WARNINGS_LOG_FILE="buildWarnings.log";\ndotnet build\n-consoleloggerparameters:"Summary;Verbosity=normal" -m -p:"WarnLevel=5;EnforceCodeStyleInBuild=true" -t:"clean,build" -fl1 "/flp1:logFile=$BUILD_WARNINGS_LOG_FILE;warningsonly";\nif [ $? -eq 0 ] && [ $(wc -l < $BUILD_WARNINGS_LOG_FILE) -gt 0 ]\nthen\nfi" > my-bash.sh
- chmod +X my-bash.sh
- bash ./my-bash.sh
- 使用 multi-bash 命令的正确语法。 (注意管道后面的破折号)
image: mcr.microsoft.com/dotnet/sdk:5.0
stages:
- build
build:
stage: build
script:
- |-
BUILD_WARNINGS_LOG_FILE="buildWarnings.log";
dotnet build -consoleloggerparameters:"Summary;Verbosity=normal" -m -p:"WarnLevel=5;EnforceCodeStyleInBuild=true" -t:"clean,build" -fl1 "/flp1:logFile=$BUILD_WARNINGS_LOG_FILE;warningsonly";
if [ $? -eq 0 ] && [ $(wc -l < $BUILD_WARNINGS_LOG_FILE) -gt 0 ]
then
# do stuff here
fi
给定此示例 .Net CI Gitlab 的管道配置
image: mcr.microsoft.com/dotnet/sdk:5.0
stages:
- build
build:
stage: build
script:
- |
BUILD_WARNINGS_LOG_FILE="buildWarnings.log";
dotnet build -consoleloggerparameters:"Summary;Verbosity=normal" -m -p:"WarnLevel=5;EnforceCodeStyleInBuild=true" -t:"clean,build" -fl1 "/flp1:logFile=$BUILD_WARNINGS_LOG_FILE;warningsonly";
if [ $? -eq 0 ] && [ $(wc -l < $BUILD_WARNINGS_LOG_FILE) -gt 0 ]
then
# do stuff here
fi
管道因语法无效而失败。这是因为它不知道有多个 shell 命令要执行。
这个 .sh
文件可以在我的本地机器上运行
#!/bin/bash
BUILD_WARNINGS_LOG_FILE="buildWarnings.log";
dotnet build --output build -consoleloggerparameters:"Summary;Verbosity=normal" -m -p:"WarnLevel=5;EnforceCodeStyleInBuild=true" -t:"clean,build" -fl1 "/flp1:logFile=$BUILD_WARNINGS_LOG_FILE;warningsonly";
if [ $? -eq 0 ] && [ $(wc -l < $BUILD_WARNINGS_LOG_FILE) -gt 0 ]
then
# do stuff here
fi
那么如何将它正确地嵌入到 Gitlab ci 阶段?
可以通过三种方式做到这一点:
- 将您的 shell 脚本添加到图像中,以便您可以执行它:(在您的情况下,需要基于
mcr.microsoft.com/dotnet/sdk:5.0
创建一个新图像)
image: mcr.microsoft.com/dotnet/sdk:5.0
stages:
- build
build:
stage: build
script:
- bash /path/to/your/script.sh
- 在 运行 时创建 shell 脚本(只需将脚本回显到 shell 文件中并使其可执行)
image: mcr.microsoft.com/dotnet/sdk:5.0
stages:
- build
build:
stage: build
script:
- echo "BUILD_WARNINGS_LOG_FILE="buildWarnings.log";\ndotnet build\n-consoleloggerparameters:"Summary;Verbosity=normal" -m -p:"WarnLevel=5;EnforceCodeStyleInBuild=true" -t:"clean,build" -fl1 "/flp1:logFile=$BUILD_WARNINGS_LOG_FILE;warningsonly";\nif [ $? -eq 0 ] && [ $(wc -l < $BUILD_WARNINGS_LOG_FILE) -gt 0 ]\nthen\nfi" > my-bash.sh
- chmod +X my-bash.sh
- bash ./my-bash.sh
- 使用 multi-bash 命令的正确语法。 (注意管道后面的破折号)
image: mcr.microsoft.com/dotnet/sdk:5.0
stages:
- build
build:
stage: build
script:
- |-
BUILD_WARNINGS_LOG_FILE="buildWarnings.log";
dotnet build -consoleloggerparameters:"Summary;Verbosity=normal" -m -p:"WarnLevel=5;EnforceCodeStyleInBuild=true" -t:"clean,build" -fl1 "/flp1:logFile=$BUILD_WARNINGS_LOG_FILE;warningsonly";
if [ $? -eq 0 ] && [ $(wc -l < $BUILD_WARNINGS_LOG_FILE) -gt 0 ]
then
# do stuff here
fi