Azure Pipelines 中的文件创建器任务中未解析变量组

问题描述 投票:0回答:2

我在 Azure DevOps 中有一个项目,用于为其他 FE 项目构建和发布 e2e 测试。我们正在从带有任务组的传统 GUI 管道转向 YAML 管道,但在使用变量组中的变量时遇到问题。

我有什么:

  • a 构建管道来构建和发布工件 --> 这在 yaml 中也能正常工作
  • 一个包含一些任务的构建管道 - 下载工件、npm 安装、File Creator 任务来创建 .env 文件(变量直接在 GUI 中定义)、npm 命令来运行测试
  • 然后释放。

我现在拥有的:

  • 用于文件创建和测试运行的 YAML 模板(模板,因为我也将使用它而不是其他项目的任务组)
  • 用于构建和发布的 YAML 文件 - 它构建工件然后包含模板。

变量应该从我的项目所在的库中的变量组中读取。变量组可以访问管道。管道验证,但运行测试任务失败,因为变量未解析。我是否以正确的方式引用它们?我想避免对 yml 文件中的变量进行硬编码。

这是我的多级 yml 管道

variables:
- group: End2EndV3
trigger:
  branches:
    include:
    - refs/heads/main
stages:
- stage: BuildArtifact
  jobs:
  - job: Job_1
    displayName: Agent job 1
    pool:
      vmImage: windows-2019
    steps:
    - checkout: self
      fetchDepth: 1
    - task: NodeTool@0
      displayName: Use Node 10.x
      inputs:
        versionSpec: 10.x
    - task: CopyFiles@2
      displayName: 'Copy Files to: $(Build.ArtifactStagingDirectory)'
      inputs:
        TargetFolder: $(Build.ArtifactStagingDirectory)
    - task: PublishBuildArtifacts@1
      displayName: 'Publish Artifact: MyArtifact'
      inputs:
        ArtifactName: MyArtifact
- stage: RunTests
  jobs:
  - job: RunTests
    steps:
    - template: template-test-release.yml
    
...

这里的 yml 模板导致了问题

steps:
- task: eliostruyf.build-task.custom-build-task.file-creator@6
  displayName: 'Create cypress.env.json'
  inputs:
    filepath: 'somefilepath/MyArtifact/cypress.env.json'
    filecontent: |
     {
       "myVariable": "$(myVariableFromLibraryValue)",
     }
    fileoverwrite: true
    verbose: true

- task: Npm@1
  displayName: 'npm ci install'
  inputs:
    command: ci
    workingDir: '$(System.DefaultWorkingDirectory)/somefilepath/MyArtifact'
    verbose: false

- task: Npm@1
  displayName: 'npm run tests'
  inputs:
    command: custom
    workingDir: '$(System.DefaultWorkingDirectory)/somefilepath/MyArtifact'
    verbose: false
    customCommand: 'run cy:run:tests'

- task: PublishTestResults@2
  displayName: 'Publish Test Results *-test-output.xml'
  inputs:
    testResultsFiles: '*-test-output.xml'
    searchFolder: '$(System.DefaultWorkingDirectory)/somefilepath/MyArtifact/cypress/results'
    mergeTestResults: true

yaml environment-variables azure-pipelines azure-pipelines-release-pipeline azure-pipelines-tasks
2个回答
0
投票

尝试将变量从变量组作为参数传递到将使用这些变量的模板。

例如:

主管道
variables:
- group: End2EndV3

# code omitted for brevity

- stage: RunTests
  jobs:
  - job: RunTests
    steps:
    - template: template-test-release.yml
      parameters:
        foo: "$(myVariableFromLibraryValue)" # from variable group

模板测试发布.yml:

parameters:
  - name: foo
    type: string
    displayName: 'Foo'

steps:
- task: eliostruyf.build-task.custom-build-task.file-creator@6
  displayName: 'Create cypress.env.json'
  inputs:
    filepath: 'somefilepath/MyArtifact/cypress.env.json'
    filecontent: |
     {
       "myVariable": "${{ parameters.foo }}",
     }
    fileoverwrite: true
    verbose: true
    
# .....

推荐阅读:


0
投票

创建

DownloadBuildArtifacts@1
文件之前,在
template-test-release.yml
中添加
cypress.env.json
任务。将
downloadPath
设置为 npm 测试的工作目录。

steps:
- task: DownloadBuildArtifacts@1
  inputs:
    buildType: 'current'
    downloadType: 'single'
    artifactName: 'MyArtifact'
    downloadPath: '$(System.DefaultWorkingDirectory)/somefilepath' #The working directory of your npm test
- task: eliostruyf.build-task.custom-build-task.file-creator@6
  displayName: 'Create cypress.env.json'
  inputs:
    filepath: 'somefilepath/MyArtifact/cypress.env.json'
    filecontent: |
     {
       "myVariable": "$(myVariableFromLibraryValue)", 
     }
    fileoverwrite: true
    verbose: true
- task: CmdLine@2
  inputs:
    script: |
      ls
      cat cypress.env.json
    workingDirectory: '$(System.DefaultWorkingDirectory)/somefilepath/MyArtifact'

我不知道

somefilepath
的价值。确保您的工件和
cypress.env.json
位于 npm 测试的工作目录中。您可以运行
ls
列出“npm ci install”之前的
$(System.DefaultWorkingDirectory)/somefilepath/MyArtifact
中的所有文件。

© www.soinside.com 2019 - 2024. All rights reserved.