Use runtime variable for repository name in checkout in Azure Pipelines
Asked Answered
E

2

7

I have a matrix in a multi-stage pipeline that generates jobs to go and checkout to different git repositories and make commits.

The matrix has the repo name as a value, and I want to reference that during a checkout task. However I keep getting errors due to the variable being evaluated at runtime.

i.e. matrix

matrix:
  repo_foo:
    repo: foo
  repo_bar:
    repo: bar

and I want to do something like

steps:
- checkout: $[ variables['repo'] ]

but those isn't evaluated and throws an error before the pipeline starts. Similarly, using ${{ variables['repo'] }} also fails because it gets evaluated at compile time and thus becomes an empty string.

I've defined the potential repos as resources in the pipeline yaml, as well as attempting to pass the full git path as the variable value.

Is there another way I can do this?

Experimental answered 9/7, 2020 at 14:24 Comment(2)
Not get your response for several days, would you please share your latest information about this issue? If you have any concern, feel free to share it here.Allotment
It is not possible for variables but for parameters in a template... Does this help?Kopje
A
2

According to the test, variable syntax should not be supported in the checkout step.

If you use variables to refer to the repo in the checkout step, then when you start running the pipeline you will get the following error:

enter image description here

steps:
- checkout: self | none | repository name # self represents the repo where the initial Pipelines YAML file was found

You can refer to this official document .

Allotment answered 10/7, 2020 at 3:32 Comment(3)
Is there a design/technical reason why we can't? We have a GitOps based deployment strategy where we check in to multiple different git repos to update image tags and we generate a release manifest that we pass as a matrix. Because matrix variables are defined at runtime we can't dynamically create jobs based off of the repos in that manifest. Ideally I could checkout a variable that resolves to a repo name at runtime OR the job definition could use a matrix variable to deifne a condition.Experimental
I was hoping to do the same thing here, and it seems like the limitation may be related to how DevOps tracks the specific repo+version associated with a build. If that information isn't known until the pipeline is running it probably gets more complicated to do that tracking. It would be very useful to have this support, though.Momentarily
Variables can be used with the checkout step. Have a look here https://mcmap.net/q/771249/-is-it-possible-to-use-a-variable-in-the-ref-property-of-resources-repository-for-azure-devops-yamlAbsurdity
P
1

You could get related resource by calling git clone command per to matrix variable value.

Pelota answered 13/7, 2020 at 13:49 Comment(2)
I tried doing the git clone in a bash task but got credential errors in doing so.Experimental
@Experimental You could specify the credential in URL: for example: git clone test:$(system.accesstoken)@dev.azure.com/starain09/…. Note: you need to grant build service account read permission for target repository. You may refer to this article: Run Git commands in a script.Pelota

© 2022 - 2024 — McMap. All rights reserved.