Azure DevOps Pipeline使用本地模板时出现序列异常报错求助
Azure Pipeline模板验证错误:A sequence was not expected
错误信息
/templates/transform-settings.yml (Line: 1, Col: 1): A sequence was not expected
相关代码片段
主流水线文件(azure-pipelines.yml)
imagePullSecret: ' fd bfgbgf ' dockerfilePath: '**/Dockerfile' vmImageName: 'ubuntu-latest' testVar: 'test' tag: '$(Build.BuildId)' ConnectionStrings.DefaultConnection: "" stages: - template: templates/transform-settings.yml - stage: Build displayName: Build stage jobs: - job: Build displayName: Build pool: vmImage: $(vmImageName) steps: - task: Docker@2 inputs: command: buildAndPush repository: $(imageRepository) dockerfile: $(dockerfilePath) containerRegistry: $(dockerRegistryServiceConnection)
模板文件(templates/transform-settings.yml)
- stage: TransformFiles displayName: TransformFiles variables: - ${{ if eq(variables['Build.SourceBranchName'], 'development') }}: - group: dev-secrets - name: testVar value: 'dev' - name: ConnectionStrings.DefaultConnection value: $(psql-conn-str-dev) - ${{ if eq(variables['Build.SourceBranchName'], 'qa') }}: - group: qa-secrets - name: testVar value: 'qa' jobs: - job: Transform_AppSettings steps: - bash: echo "===== Transforming appsettings.json for $(variables['Build.SourceBranchName']) environment =====" displayName: 'File Transform' - task: FileTransform@1 inputs: folderPath: '$(System.DefaultWorkingDirectory)' fileType: 'json' targetFiles: 'appsettings.json' - upload: appsettings.json artifact: appsettings
错误原因
主流水线的顶层变量定义存在YAML结构错误:
- 缺少
variables:关键字包裹所有顶层变量,直接缩进的键值对被YAML解析器识别为无效结构,进而干扰模板文件的解析流程,导致错误提示指向模板第一行。 - 第一行变量
imagePullSecret前有多余空格,缩进不规范。
解决方案
修正主流水线的变量定义部分,添加variables:关键字并调整缩进:
variables: imagePullSecret: ' fd bfgbgf ' dockerfilePath: '**/Dockerfile' vmImageName: 'ubuntu-latest' testVar: 'test' tag: '$(Build.BuildId)' ConnectionStrings.DefaultConnection: "" stages: - template: templates/transform-settings.yml - stage: Build displayName: Build stage jobs: - job: Build displayName: Build pool: vmImage: $(vmImageName) steps: - task: Docker@2 inputs: command: buildAndPush repository: $(imageRepository) dockerfile: $(dockerfilePath) containerRegistry: $(dockerRegistryServiceConnection)
当前模板文件的条件变量定义和阶段结构均符合Azure Pipeline规范,无需修改。修正主流水线后,重新验证YAML即可解决错误。
内容的提问来源于stack exchange,提问作者Vladimir Petukhov
相关产品推荐
相关产品推荐

