我在 Azure DevOps 中有一个项目,用于为其他 FE 项目构建和发布 e2e 测试。我们正在从带有任务组的传统 GUI 管道转向 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
尝试将变量从变量组作为参数传递到将使用这些变量的模板。
例如:
variables:
- group: End2EndV3
# code omitted for brevity
- stage: RunTests
jobs:
- job: RunTests
steps:
- template: template-test-release.yml
parameters:
foo: "$(myVariableFromLibraryValue)" # from variable group
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
# .....
推荐阅读:
创建
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
中的所有文件。