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

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

问题描述

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

pipeline.yaml

 阶段:- 阶段:部署变量:天蓝色资源组:''kubernetesCluster: ''子域:''工作:${{/myfolder/*.yaml 中的每个文件名}}:值文件:$文件名- 模板:模板/deploy-helmchart.yaml@pipelinetemplates

deploy-helmchart.yaml

 工作:- 工作:部署水池:vmImage: 'ubuntu-最新'脚步:- 任务:HelmInstaller@1displayName: '安装 Helm'输入:helmVersionToInstall: '2.15.1'条件: and(succeeded(), startsWith(variables['build.sourceBranch'], 'refs/tags/v'))- 任务:HelmDeploy@0displayName: '初始化 Helm'输入:connectionType: 'Azure 资源管理器'azureSubscription: $(azureSubscription)天蓝色资源组:$(天蓝色资源组)kubernetesCluster: $(kubernetesCluster)命令:'初始化'条件: and(succeeded(), startsWith(variables['build.sourceBranch'], 'refs/tags/v'))- 任务:PowerShell@2displayName: '获取 GitTag'输入:目标类型:'内联'脚本: |# 在这里写下你的 PowerShell 命令.写主机获取最新的 GitTag"$gt = git 描述 --abbrev=0写主机##vso[task.setvariable variable=gittag]$gt"条件: and(succeeded(), startsWith(variables['build.sourceBranch'], 'refs/tags/v'))- 任务:Bash@3displayName: '获取回购标签'输入:目标类型:'内联'脚本: |回声 GitTag=$(gittag)echo BuildID=$(Build.BuildId)echo SourceBranchName=$(Build.SourceBranchName)echo ClusterName= $(kubernetesCluster)- 任务:HelmDeploy@0displayName: '升级 helmchart'输入:connectionType: 'Azure 资源管理器'azureSubscription: $(azureSubscription)天蓝色资源组:$(天蓝色资源组)kubernetesCluster: $(kubernetesCluster)命令:'升级'图表类型:'文件路径'图表路径:$(图表路径)安装:真的发布名称:$(发布名称)价值文件:$(价值文件)参数:'--set image.tag=$(gittag) --set subdomain=$(subdomain)'条件: and(succeeded(), startsWith(variables['build.sourceBranch'], 'refs/tags/v'))

另一件事是,如果作业可以默认访问 GitHub 存储库,还是我需要在作业级别做一些事情?

此外,对于这种情况,我如何在作业中使用 for 循环?

任何帮助将不胜感激.

收到@Leo 的评论后更新

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

 - 任务:PowerShell@2displayName: '获取文件'输入:目标类型:'内联'脚本: |写主机获取值文件"光盘我的文件夹$a=git ls-文件foreach ($i in $a) {写主机##vso[task.setvariable variable=filename]$i"写主机打印"$i}

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

解决方案

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

答案是肯定的.

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

git clone https://@github.com/XXXXX/TestProject.git

<块引用>

除了在这种情况下如何在作业中使用 for 循环?

您可以创建一个包含一组操作的模板,并在构建过程中传递参数,例如:

deploy-helmchart.yaml:

参数:参数:[]脚步:- ${{parameters.param 中的每个文件名}}:- 脚本:'echo ${{ 文件名}}'

pipeline.yaml:

步骤:- 模板:deploy-helmchart.yaml参数:参数:[filaname1"、filaname2"、filaname3"]

查看文档

所以,这个问题的答案是取决于你的文件名是否可以作为Helm命令使用,如果不是,你就不能运行任务HelmDeploy@0 为每个文件使用参数.如果是,你可以做到.

请查看官方文档模板 了解更多详情.

希望这会有所帮助.

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

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

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'))  

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?

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

Any help would be appreciated.

Updated after getting comments from @Leo

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
       }

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

解决方案

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

The answer is yes.

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

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:

parameters:
  param : []

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

pipeline.yaml:

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

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

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"

Update:

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

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

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"]

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

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

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.

Hope this helps.

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

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