【问题标题】:How to setup Gitlab ci stage with multiple shell commands如何使用多个 shell 命令设置 Gitlab ci 阶段
【发布时间】:2021-09-25 09:24:24
【问题描述】:

鉴于此示例,Gitlab 的 .Net CI 管道配置

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 gitlab continuous-integration yaml gitlab-ci


    【解决方案1】:

    有三种方法可以做到这一点:

    1. 将您的 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
    
    1. 在运行时创建 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
    
    1. 使用正确的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
    

    【讨论】:

    • 我更喜欢第三种解决方案。不幸的是,这不起作用,因为 Gitlab 认为该脚本是 Powershell 脚本……至少我可以在错误日志中看到它In C:\Windows\TEMP\build_script975465063\script.ps1,但我会尝试将其转换为 PS 脚本并尝试一下
    猜你喜欢
    • 1970-01-01
    • 2021-05-10
    • 2017-10-06
    • 1970-01-01
    • 2020-12-13
    • 2020-07-28
    • 2019-09-21
    • 2016-11-19
    • 1970-01-01
    相关资源
    最近更新 更多