在Azure管道作业中使用for循环 [英] using for-loop in azure pipeline jobs

查看:110
本文介绍了在Azure管道作业中使用for循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将使用一个for循环来扫描文件夹中的文件(value-f1.yaml,values-f2.yaml等),每次使用文件名作为变量并在其中运行作业Azure管道作业,用于基于该Values文件部署Helmchart.该文件夹位于GitHub存储库中.所以我在想这样的事情:

I'm gonna use a for-loop which scans the files (value-f1.yaml, values-f2.yaml,...) in a folder and each time use a filename as a varibale and run the job in Azure pipeline job to deploy the helmchart based on that values file. The folder is located in the GitHub repository. So I'm thinking of something like this:

pipeline.yaml

pipeline.yaml

 stages:
  - stage: Deploy
    variables:
      azureResourceGroup: ''
      kubernetesCluster: ''
      subdomain: ''
    jobs:
    ${{ each filename in /myfolder/*.yaml}}:
       valueFile: $filename 
       - template: Templates/deploy-helmchart.yaml@pipelinetemplates    

deploy-helmchart.yaml

deploy-helmchart.yaml

 jobs:
    - job: Deploy
      pool:
        vmImage: 'ubuntu-latest'
      steps:
      - task: HelmInstaller@1
        displayName: 'Installing Helm'
        inputs:
          helmVersionToInstall: '2.15.1'
        condition: and(succeeded(), startsWith(variables['build.sourceBranch'], 'refs/tags/v'))  

      - task: HelmDeploy@0
        displayName: 'Initializing Helm'
        inputs:
          connectionType: 'Azure Resource Manager'
          azureSubscription: $(azureSubscription)
          azureResourceGroup: $(azureResourceGroup)
          kubernetesCluster: $(kubernetesCluster)
          command: 'init'
        condition: and(succeeded(), startsWith(variables['build.sourceBranch'], 'refs/tags/v'))  

      - task: PowerShell@2
        displayName: 'Fetching GitTag'
        inputs:
          targetType: 'inline'
          script: |
            # Write your PowerShell commands here.
            Write-Host "Fetching the latest GitTag"
            $gt = git describe --abbrev=0
            Write-Host "##vso[task.setvariable variable=gittag]$gt"
        condition: and(succeeded(), startsWith(variables['build.sourceBranch'], 'refs/tags/v'))    


      - task: Bash@3
        displayName: 'Fetching repo-tag'
        inputs:
          targetType: 'inline'
          script: |
            echo GitTag=$(gittag)
            echo BuildID=$(Build.BuildId)
            echo SourceBranchName=$(Build.SourceBranchName)
            echo ClusterName= $(kubernetesCluster)

      - 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: $(valueFile)
          arguments: '--set image.tag=$(gittag) --set subdomain=$(subdomain)'
        condition: and(succeeded(), startsWith(variables['build.sourceBranch'], 'refs/tags/v'))  

另一件事是,如果这些作业可以默认访问GitHub存储库,或者我需要在该作业级别做些什么?

Another thing is that if the jobs can get access to the GitHub repo by default or do I need to do something in the job level?

在这种情况下,我该如何在工作中使用for循环?

Besides how can I use for-loop in the job for this case?

任何帮助将不胜感激.

在收到@Leo的评论后更新

Updated after getting comments from @Leo

这是我在deploy-helmchart.yaml中添加的PowerShell任务,用于从GitHub中的文件夹中获取文件.

Here is a PowerShell task that I added in deploy-helmchart.yaml for fetching the files from a folder in GitHub.

 - task: PowerShell@2
   displayName: 'Fetching Files'
   inputs:
     targetType: 'inline'
     script: |
       Write-Host "Fetching values files"
       cd myfolder
       $a=git ls-files
       foreach ($i in $a) {
          Write-Host "##vso[task.setvariable variable=filename]$i"
          Write-Host "printing"$i
       }

现在的问题是我该如何运行任务:使用参数为每个文件运行HelmDeploy @ 0?

Now the question is how can I run the task: HelmDeploy@0 for each files using parameters?

推荐答案

默认情况下,如果作业可以访问GitHub存储库,还是我需要在作业级别执行某些操作?

if the jobs can get access to the GitHub repo by default or do I need to do something in the job level?

答案是肯定的.

我们可以在作业中添加一个命令行任务,例如job1以通过Github PAT克隆GitHub存储库,然后我们可以访问$(Build.SourcesDirectory)中的那些文件(value-f1.yamlvalues-f2.yaml,...) :

We could add a command line task in the jobs, like job1 to clone the GitHub repository by Github PAT, then we could access those files (value-f1.yaml, values-f2.yaml,...) in $(Build.SourcesDirectory):

git clone https://<GithubPAT>@github.com/XXXXX/TestProject.git

在这种情况下,我该如何在工作中使用for循环?

Besides how can I use for-loop in the job for this case?

您可以创建一个模板,该模板将具有一组操作,并在构建过程中传递参数,例如:

You could create a template which will have a set of actions, and pass parameters across during your build, like:

deploy-helmchart.yaml:

deploy-helmchart.yaml:

parameters:
  param : []

steps:
  - ${{each filename in parameters.param}}
    - scripts: 'echo ${{ filename  }}'

pipeline.yaml:

pipeline.yaml:

steps:
 - template: deploy-helmchart.yaml
   parameters:
     param: ["filaname1","filaname2","filaname3"]

检查文档解决Azure DevOps管道中的循环问题以获取更多详细信息.

Check the document Solving the looping problem in Azure DevOps Pipelines for some more details.

命令行在foler中获取最新的文件名:

Command line get the latest file name in the foler:

   FOR /F "delims=|" %%I IN ('DIR "$(Build.SourcesDirectory)\*.txt*" /B /O:D') DO SET NewestFile=%%I

   echo "##vso[task.setvariable variable=NewFileName]NewestFile"

更新:

现在的问题是我该如何运行任务:每个任务都运行HelmDeploy @ 0 文件使用参数?

Now the question is how can I run the task: HelmDeploy@0 for each files using parameters?

s depends on whether your HelmDeploy`任务具有接受文件名参数的选项.

Its depends on whether yourHelmDeploy` task has options to accept the filename parameter.

正如我之前说的,我们可以使用以下yaml来调用带有参数的模板yaml:

As I said before, we could use following yaml to invoke the template yaml with parameters:

 - template: deploy-helmchart.yaml
   parameters:
     param: ["filaname1","filaname2","filaname3"]

但是,如果任务HelmDeploy没有接受参数的选项,我们将无法使用参数为每个文件运行任务HelmDeploy@0.

But, if the task HelmDeploy has no options to accept parameters, we could not run the task HelmDeploy@0 for each files using parameters.

然后我检查HelmDeploy@0,我发现只有一个选项可以接受 Helm命令参数:

Then I check the HelmDeploy@0, I found there is only one option that can accept Helm command parameters:

因此,此问题的答案是取决于您的文件名是否可以用作Helm命令,否则,您将无法使用参数为每个文件运行任务HelmDeploy@0.如果是,则可以.

So, the answer for this question is depends on whether your file name can be used as a Helm command, if not, you could not run the task HelmDeploy@0 for each files using parameters. If yes, you can do it.

请检查正式文档模板以获取更多详细信息.

Please check the official document Templates for some more details.

希望这会有所帮助.

这篇关于在Azure管道作业中使用for循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆