【问题标题】:Pipeline is not failing when fail occurs inside python script当 python 脚本中发生故障时,管道不会失败
【发布时间】:2026-01-10 14:50:02
【问题描述】:

所以 - 我正在编写一个脚本来根据项目的变化运行 iOS 测试(可能对主题并不重要)。

在脚本中,有一个命令将运行测试:

cmd = "xcodebuild -workspace xxx.xcworkspace -scheme xxx -destination 'platform=iOS Simulator,name={0},OS=latest' -configuration Debug -derivedDataPath {1} test-without-building {2} -parallel-testing-enabled NO -enableCodeCoverage YES | xcpretty".format(os.environ['TEST_SIMULATOR_NAME'], os.environ['PWD'], result)

并像这样执行:

do(cmd)

do() 方法定义为 (source):

def do(command):
  return_code = call([ '/bin/bash', '-c', 'set -o pipefail; ' + command ])

Gitlab 作业设置:

manualUiTestsBasedOnChanges:
  stage: uiTests
  only: ...some conditions...
  before_script: 
    - set -o pipefail
  script:
    - ../scripts/ci/run_UI_tests_based_on_changes.py

这样做的问题是,如果在此脚本中发生故障,它不会使作业失败,即使在脚本之前和 do() 方法中设置了set -o pipefail。在下图中可见。

你知道为什么会这样吗?

【问题讨论】:

    标签: python ios continuous-integration gitlab


    【解决方案1】:

    好的,我发现唯一可行的解​​决方案是将带有命令的字符串发送回 gitlab 的 shell 并在那里执行它。 像这样:

    在 Python 中:

    cmd = "
    xcodebuild 
       -workspace xxx.xcworkspace 
       -scheme xxx 
       -destination 'platform=iOS Simulator,name={0},OS=latest' 
       -configuration Debug 
       -derivedDataPath {1} test-without-building {2} 
       -parallel-testing-enabled NO 
       -enableCodeCoverage YES | xcpretty"
    .format(os.environ['TEST_SIMULATOR_NAME'], os.environ['PWD'], result)
    
    print(cmd)
    

    YAML:

    manualUiTestsBasedOnChanges:
      stage: uiTests
      only: ...some conditions...
      before_script: 
        - set -o pipefail
      script:
        - result=`../scripts/ci/run_UI_tests_based_on_changes.py`
        - eval "$result"
    

    【讨论】:

      最近更新 更多