microsoft / azure-pipelines-yaml

Azure Pipelines YAML examples, templates, and community interaction
MIT License
1.19k stars 926 forks source link

Confused about using YAML templates in other repositories #538

Closed josh-endries closed 3 years ago

josh-endries commented 3 years ago

Hello, I'm new to using YAML pipelines, always used the UI before. Hopefully this is the right place for this question.

I am trying to set up a YAML pipeline that uses another pipeline template defined in a different repository, which in turn uses a third pipeline template in that same repository. Is this possible? E.g.:

repoA\
  app-build\
    pipeline.yaml (includes template: ../validation/pipeline.yaml)
  validation\
    pipeline.yaml (includes PowerShell step with filePath: "validate.ps1")

repoB\
  MyApp\
    pipeline.yaml (includes template: app-build/pipeline.yaml@repoA)

The MyApp pipeline seems to find and run the app-build pipeline and validation pipeline. The issue I'm running into is that it can't find the ps1 file. Is there a way to locate this file relative to its "parent" pipeline (it's in the same folder as that yaml file)?

After reading stuff online my assumption is that it's looking for validate.ps1 in the MyApp folder, as relative paths seem to be relative to the "root" MyApp YAML file, though I'm not sure then how it finds the validation pipeline. FWIW, using ./ or an absolute path to the ps1 file didn't work, either. Anyway, since I can't define resources in the templates, I don't think I could qualify the ps1 file within it's pipeline as existing in repoA.

I'm not really sure how to make this work in a portable way for other apps that should use the same build/validate steps, without duplicating lots of stuff. Is this pattern possible or supposed to work?

josh-endries commented 3 years ago

I was able to work around the problem by having the template check itself out and absolutely reference the file path within my PowerShell step via:

filePath: $(Agent.BuildDirectory)/s/repo/path/to/script.ps1

I am guessing that this isn't the expected/correct way to do this, though...

anatolybolshakov commented 3 years ago

Hi @josh-endries yes, you can use it from other repository - please see the docs.

anatolybolshakov commented 3 years ago

@josh-endries I'm closing it at the moment since it looks resolved - please feel free to ask any further questions.

sigpop commented 3 months ago

In case anybody else stumbles in here, I think @josh-endries has the best approach so far. The docs that @anatolybolshakov linked has this: Repositories are resolved only once, when the pipeline starts up. After that, the same resource is used for the duration of the pipeline. Only the template files are used. Once the templates are fully expanded, the final pipeline runs as if it were defined entirely in the source repo. This means that you can't use scripts from the template repo in your pipeline. I take that to mean you can use templates in other repos, but if those templates call script files using a normal relative path, it won't work.