shell Azure Devops-如何在脚本中添加if-else条件

z8dt9xmd  于 2023-08-07  发布在  Shell
关注(0)|答案(1)|浏览(104)

我想从另一个pytest模板yaml在主管道中执行pytest

# test-template.yml

parameters:
- name: jobName
  type: string

jobs:
- job: ${{ parameters.jobName }}
  steps:
  - task: UsePythonVersion@0
    displayName: ${{ parameters.jobName }}
    inputs:
      versionSpec: some_python_version
      script: |
        if [ "${{ parameters.jobName }}" = "Pytest1" ]; then
          pytest -v tests -k "pretest_case1"
        elif [ "${{ parameters.jobName }}" = "Pytest2" ]; then
          pytest -v tests -k "postTest_case2"
        else
          # Handle any other jobName value or add an error message if needed.
          echo "Invalid jobName: ${{ parameters.jobName }}"
          exit 1
        fi

字符串

Main.yaml

trigger:
- main

stages:
- stage: test_int1
  pool:
    vmImage: 'ubuntu'
  jobs:
    - template: test-template.yml
      parameters:
        jobName: 'Pytest1'

- stage: test_int2
  pool:
    vmImage: 'ubuntu'
  jobs:
    - template: test-template.yml
      parameters:
        jobName: 'Pytest2'


但是在测试这个管道时,它抛出错误
“上下文中不允许使用if指令。嵌入在字符串”“中的表达式不应使用指令
对此有什么解决办法吗?

hivapdat

hivapdat1#

UsePythonVersion没有input script

# Use Python version v0
# Use the specified version of Python from the tool cache, optionally adding it to the PATH.
- task: UsePythonVersion@0
  inputs:
    versionSpec: '3.x' # string. Required. Version spec. Default: 3.x.
    #disableDownloadFromRegistry: false # boolean. Disable downloading releases from the GitHub registry. Default: false.
    #allowUnstable: false # boolean. Optional. Use when disableDownloadFromRegistry = false. Allow downloading unstable releases. Default: false.
    #githubToken: # string. Optional. Use when disableDownloadFromRegistry = false. GitHub token for GitHub Actions python registry. 
    #addToPath: true # boolean. Add to PATH. Default: true.
  # Advanced
    architecture: 'x64' # 'x86' | 'x64'. Required. Architecture. Default: x64.

字符串
你应该使用PythonScript

# Python script v0
# Run a Python file or inline script.
- task: PythonScript@0
  inputs:
    scriptSource: 'filePath' # 'filePath' | 'inline'. Required. Script source. Default: filePath.
    scriptPath: # string. Required when scriptSource = filePath. Script path. 
    #script: # string. Required when scriptSource = inline. Script. 
    #arguments: # string. Arguments. 
  # Advanced
    #pythonInterpreter: # string. Python interpreter. 
    #workingDirectory: # string. Working directory. 
    #failOnStderr: false # boolean. Fail on standard error. Default: false.


你可以试试这样的

# test-template.yml

parameters:
- name: jobName
  type: string

jobs:
- job: ${{ parameters.jobName }}
  steps:
  - task: UsePythonVersion@0
    displayName: ${{ parameters.jobName }}
    inputs:
      versionSpec: some_python_version
  - ${{ if eq(parameters.jobName, 'Pytest1') }}:
    - task: PythonScript@0
      inputs:
        scriptSource: 'inline' 
        script: pytest -v tests -k "pretest_case1"
  - ${{ elseif eq(parameters.jobName, 'Pytest2') }}:
    - task: PythonScript@0
      inputs:
        scriptSource: 'inline' 
        script: pytest -v tests -k "postTest_case2"
  - ${{ else }}:
    - task: PythonScript@0
      inputs:
        scriptSource: 'inline' 
        script: |
          echo "Invalid jobName: ${{ parameters.jobName }}"
          exit 1

相关问题