using for-loop in azure pipeline jobs

前端 未结 1 1836
悲哀的现实
悲哀的现实 2021-01-13 20:16

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

相关标签:
1条回答
  • 2021-01-13 21:04

    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.

    0 讨论(0)
提交回复
热议问题