shell Azure Devops -如何从主yaml模板中的其他yaml引用/读取特定脚本?

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

我有下面两个yaml模板
test-template.yml

jobs:
   
   -job: Pytest

    steps:
    - task: UsepythonVersion
      displayName: 'Pytest1'
      inputs:
        verion_secific: some_pthon_version
        script: pytest -v tests -k "pretest_case1"

jobs:
   
   -job: Pytest

    steps:
    - task: UsepythonVersion
      displayName: 'Pytest2'
      inputs:
        verion_secific: some_pthon_version
        script: pytest -v tests -k "postTest_case2"

字符串
Main.yaml

steps:
- task: Bash@3
  displayName: 'Before inserted step'
  inputs:
    targetType: inline
    script: echo "This step is before the inserted step."

stages
- stage:test_int1
  pool: 
    vmImage : ubuntu

  jobs:
    - template: test-template.yml<Pytest1 >

- stage:test_int2
  pool: 
    vmImage : ubuntu

  jobs:
    - template: test-template.yml<Pytest2 >


如何从test-template.yml中分别调用Main.yaml中“Pytest 1”和“Pytest 2”?
我尝试了下面的脚本不使用python

parameters:
- name: jobName
  type: string
- name: proj_name
  type: string

jobs:
- job: ${{ parameters.jobName }}
  steps:
  - script: |
      # Set the test_case_name based on jobName
      if [ "${{ parameters.jobName }}" = "Pytest2" ]; then
        test_case_name="not Pytest2"
      else
        test_case_name="Pytest2"
      fi

      # Run pytest with the computed test_case_name
      pytest -v tests -k "$test_case_name"

      # Capture the exit code of pytest
      exit_code=$?

      # Handle the exit_code as needed (e.g., print a message or exit with an error code)
      if [ $exit_code -ne 0 ]; then
        echo "Tests failed"
        exit 1
      fi


但看起来下面的部分没有评估if条件,即使值是“Pytest 2”

if [ "${{ parameters.jobName }}" = "Pytest2" ]


有正确的方法吗?

ecfsfe2w

ecfsfe2w1#

看下面的代码工作

# 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

个字符

相关问题