如何将Powershell命令的输出用作Azure管道中的参数? [英] How to use output of a powershell command as parameters in Azure pipeline?
问题描述
我有一个PowerShell脚本任务,该任务从git存储库中的文件夹中获取某些文件的名称,并将其放入变量中.我想在参数中使用这些文件名,并在另一个任务(任务:HelmDeploy @ 0)中使用每个"条件,以便每次使用文件名之一作为valueFile变量运行该任务.
I have a PowerShell script task that gets the names of some files from a folder in my git repo and puts them into a variable. I want to use those file names in parameters and use "each" condition in another task (task: HelmDeploy@0) in order to run that task each time with one of the file names as valueFile variable.
这是我尝试过的方法,但是它给出了错误 Template-Yaml/deploy-jobs.yaml@pipelinetemplates期望的序列或映射.实际值'$ [dependencies.A.outputs ['fileoutput.valuefiles']]'在行$ {{parameters.files}}中的
Here is what I have tried, however it gives an error Template-Yaml/deploy-jobs.yaml@pipelinetemplates Expected a sequence or mapping. Actual value '$[dependencies.A.outputs['fileoutput.valuefiles']]' in line ${{each file in parameters.files}}
deploy-jobs.yaml
deploy-jobs.yaml
parameters:
files: []
jobs:
- job: Deploy
pool:
vmImage: 'ubuntu-latest'
variables:
filenames: ${{ parameters.files }}
steps:
- task: HelmInstaller@1
displayName: 'Installing Helm'
inputs:
helmVersionToInstall: '2.15.1'
- task: HelmDeploy@0
displayName: 'Initializing Helm'
inputs:
connectionType: 'Azure Resource Manager'
azureSubscription: $(azureSubscription)
azureResourceGroup: $(azureResourceGroup)
kubernetesCluster: $(kubernetesCluster)
command: 'init'
- task: AzureCLI@2
inputs:
azureSubscription: $(azureSubscription)
scriptType: 'bash'
scriptLocation: 'inlineScript'
inlineScript:
echo "##vso[task.setvariable variable=imgtag]$(az acr repository show-tags --name myacr --repository myrepo --orderby time_desc --top 1 | awk ' /[[:digit:]]/ { print $0 } ' | tr -d '[:space:]')"
- task: Bash@3
displayName: 'Fetching repo-tag'
inputs:
targetType: 'inline'
script: |
echo tag=$(imgtag)
echo filenames=$(filenames) ### output is: **/myfolder/dev/file1.yaml,**/myfolder/dev/file2.yaml
- ${{each file in parameters.files}}: ##Error
- task: HelmDeploy@0
displayName: 'Upgrading helmchart'
inputs:
connectionType: 'Azure Resource Manager'
azureSubscription: $(azureSubscription)
azureResourceGroup: $(azureResourceGroup)
kubernetesCluster: $(kubernetesCluster)
command: 'upgrade'
chartType: 'FilePath'
chartPath: $(chartPath)
install: true
releaseName: $(releaseName)
##valueFile: $(valuefiles)
valueFile: ${{ file }}
arguments: '--set image.tag=$(imgtag) --set domain=$(domain)'
azure-pipeline.yaml文件如下:
azure-pipeline.yaml file is as following:
trigger:
branches:
include:
- master
- refs/tags/v*
paths:
exclude:
- readme.md
variables:
azureSubscription: 'myazuresubscription'
chartPath: '**/mychart'
containerRegistry: 'mysc'
repository: 'myrepo'
resources:
repositories:
- repository: pipelinetemplates
type: github
name: 'mygitorg/myrepo'
endpoint: 'mygitorg'
stages:
- stage: Deploy_Cluster
variables:
azureResourceGroup: 'myresourcegroup'
kubernetesCluster: 'mycluster'
releaseName: 'mychartreleasename'
#valueFile: '**/mychart/values.yaml'
domain: 'mydomain'
jobs:
- job: A
pool:
vmImage: 'ubuntu-latest'
steps:
- task: PowerShell@2
displayName: 'Fetching ValueFiles'
inputs:
targetType: 'inline'
script: |
Write-Host "Fetching value files"
cd myfolder
$a=git ls-files
$List = $a | foreach {'**/myfolder/dev/' + $_}
Write-Host $List
$d = '"{0}"' -f ($List -join '","')
Write-Host $d ### output is: "**/myfolder/dev/file1.yaml","**/myfolder/dev/file2.yaml"
Write-Host "##vso[task.setvariable variable=valuefiles;isOutput=true]$d"
name: fileoutput
- template: Template-Yaml/deploy-jobs.yaml@pipelinetemplates ##Error expected a sequence or mapping
parameters:
files : $[dependencies.Deploy.outputs['fileoutput.valuefiles']]
I got some idea from this page: https://www.aaron-powell.com/posts/2019-05-24-azure-pipeline-templates-and-parameters/ regarding using dependencies.
我在Google上搜索了很多,但是到目前为止,我仍未找到解决该问题的方法,我们将不胜感激.
I googled a lot, however I couldn't find a solution to this issue so far, any help would be appreciated.
经过测试,由李维建议:
parameters:
files: []
jobs:
#- ${{each file in parameters.files}}:
- job: Deploy
dependsOn: A
pool:
vmImage: 'ubuntu-latest'
variables:
filenames: ${{ parameters.file }}
steps:
- task: Bash@3
displayName: 'Fetching repo-tag'
inputs:
targetType: 'inline'
script: |
##echo files=$(filenames) #output is files=file1.yaml,file2.yaml
for i in $(filenames)
do
echo "valuefiles= $i "
done
OutPut是valuefiles = files = file1.yaml,file2.yaml
OutPut is valuefiles= files=file1.yaml,file2.yaml
使用PowerShell进行测试:
Testing with PowerShell:
- task: PowerShell@2
displayName: 'Fetching ValueFiles'
inputs:
targetType: 'inline'
script: |
foreach ($i in ${{ parameters.files }}) {
Write-Host "filenames=$i"
}
错误:ObjectNotFound:($$ [dependencies.A.ou \ u2026output.valuefiles]:字符串)[],ParentContainsErrorRecordException
Error: ObjectNotFound: ($[dependencies.A.ou\u2026output.valuefiles]]:String) [], ParentContainsErrorRecordException
- task: PowerShell@2
displayName: 'Fetching ValueFiles'
inputs:
targetType: 'inline'
script: |
foreach ($i in $(filenames)) {
Write-Host "filenames=$i"
}
错误:foreach($ i in){ +〜 表达式或语句中出现意外的标记')'. + CategoryInfo:ParserError:(:) [],ParseException
Error: foreach ($i in ) { + ~ Unexpected token ')' in expression or statement. + CategoryInfo : ParserError: (:) [], ParseException
推荐答案
发生错误是因为files
仅在运行时可以计算,而- ${{each file in parameters.files}}
在编译时才计算.检查
The error occurred is because the files
can only be caculated at runtime, but - ${{each file in parameters.files}}
is valuated at compile time. Check here for more information about variable syntax.
- ${{each file in parameters.files}}
将不起作用.
如果您可以设法使用powershell/bash脚本执行HelmDeploy任务,那么我对kubernetes并不太了解.您可以foreach脚本中的文件并部署每个文件.
I doesnot know much about kubernetes, if you can manage to use powershell/bash script to do the HelmDeploy task. You can foreach the files inside script and deploy each file.
- powershell: |
foreach ($i in $(filenames)) {helm.exe upgrade ....}
一个类似的问题已经提交给Microsoft Develop团队,并且可以确定上述问题是设计使然.但是您仍然可以提交一个功能请求,方法是单击"建议功能",然后选择" azure devops "
A similar issue has been submitted to Microsoft Develop team, and it is determined that above issue is by design. But you can still submit a feature request by clicking "Suggest a feature " and choose "azure devops"
更新:
azure-pipeline.yaml
azure-pipeline.yaml
trigger:
branches:
include:
- master
stages:
- stage: Deploy_Cluster
jobs:
- job: A
pool:
vmImage: 'ubuntu-latest'
steps:
- task: PowerShell@2
displayName: 'Fetching ValueFiles'
inputs:
targetType: 'inline'
script: |
Write-Host "Fetching value files"
cd '$(system.defaultworkingdirectory)'
$a=git ls-files
$List = $a | foreach {'**/myfolder/dev/' + $_}
Write-Host $List
$d = '"{0}"' -f ($List -join '","')
Write-Host $d
Write-Host "##vso[task.setvariable variable=valuefiles;isOutput=true]$d"
name: fileoutput
- job: B
dependsOn: A
pool:
vmImage: 'ubuntu-latest'
variables:
allfiles: $[dependencies.A.outputs['fileoutput.valuefiles']]
steps:
- template: deploy-jobs.yaml
parameters:
files : $(allfiles)
deploy-jobs.yaml
deploy-jobs.yaml
parameters:
files: []
steps :
- task: PowerShell@2
displayName: 'Fetching ValueFiles'
inputs:
targetType: 'inline'
script: >
foreach ($i in ${{parameters.files}})
{
Write-Host "filenames=$i"
}
这篇关于如何将Powershell命令的输出用作Azure管道中的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!