Azure-devops-docs: Defining Resources inside a Template

Created on 2 Dec 2019  Â·  8Comments  Â·  Source: MicrosoftDocs/azure-devops-docs

Today I discovered it's not possible to ~use~ _define_ Resources inside a Template.
It would probably be valuable to include this caveat here, and if reasonable, some expected guidance for how this would be handled.


_A similar Suggestion exists (Resource Templates). Some improvements around this experience would be welcomed from a composition + abstraction standpoint. I'll describe my scenario + workaround(s) below._

Issue

I have a Pipeline _(pipeline.yml)_ that references a Template _(local-template.yml)_, which itself references a Template _(templates/remote_template.yml)_ in another Repository _(github/my/repo_2)_. Because only local-template.yml knows or cares about the Remote Repository, it defines a resources section with the relevant dependent Repository.

  • Repository1:

    • local-template.yml:

      resources:
        repositories:
      
          - repository: repo_2
            type: github
            name: my/repo_2
            endpoint: myCreds
      
      stages:
      - stage: stage_1
        displayName: stage 1 
      
        jobs: 
        - job: job_1
          steps:
          - template: templates/remote_template.yml@repo_2
      
    • pipeline.yml:

      stages: 
      - template: ../local-template.yml
      
      

However, this is not supported, resulting in the following:

_Pipeline configuration error:_
_/local-template.yml (Line: xx, Col: xx): Unexpected value 'resources'_

Possible Options

Assuming the Resource is added to the top-level Pipeline + removed from local-template:

  1. Add a Template Parameter for the dependent Repository name.
  2. Do not add a Parameter; assume the caller knows it must add the Resource and it must be named specifically.

Of these choices, the first is better, and ultimately gives the most control to the author. but it adds some hidden dependency + cruft the consumer would otherwise not need. OTOH, I can appreciate the level of complexity such support would introduce. _(ie: Templates which define the same resource multiple times for different repos, vice versa, etc.)_


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Pri1 devops-cictech devopprod doc-enhancement

Most helpful comment

@FlorisDevreese Well, moving to YAML is still worth it to us, as we'll get much better version control, plus we're taking advantage of the YAML-specific template syntax to create advanced templates, but it's definitely not an ideal situation.

What's weird is that if the template file is in the same repository, the local template file can access a remote repository template using the "@template" syntax, with the name of the remote repository defined in the root YAML pipeline file. I don't know why they just don't enable that feature for remote repository templates as well.

All 8 comments

I'm currently in the process of transitioning all classic builds to yaml builds.
We currently have a multi-layer hierarchy of Task Groups.

The fact that it is not possible (for now) to define resources inside a template is blocking for us.
Will this be possible in the future?

We're dealing with the same issue. We're forging ahead with a temporary workaround - consolidating the YAML files into just two repositories - we have one repo for templates, and another for pipelines - but it's definitely not ideal, as the pipelines are not being stored with the source code associated with them. It would definitely make our lives easier if we could have a template stored in a remote repo access another template stored in another remote repo.

@NateB2 That's means that a lot of the advantages of yaml pipelines are nullified...

@FlorisDevreese Well, moving to YAML is still worth it to us, as we'll get much better version control, plus we're taking advantage of the YAML-specific template syntax to create advanced templates, but it's definitely not an ideal situation.

What's weird is that if the template file is in the same repository, the local template file can access a remote repository template using the "@template" syntax, with the name of the remote repository defined in the root YAML pipeline file. I don't know why they just don't enable that feature for remote repository templates as well.

In general the documentation is absolutely abysmal and requires people to work out the issues themselves.

e.g. on this page it has an example with a template which starts with stages:
However the pipeline will fail :

/templates/cluster-config.yml@devopsPipelineTemplates (Line: 1, Col: 1): Unexpected value 'stages'

Agree with author. I also need ability to define resources in template. I have repo with commonly used templates and repo with my projects, that stores only azure-pipelines.yml in which project team decide what templates to use in particular project. Some of the build steps depends on third project (like Utilities) which has to be built in advance. I dont want put this step in every azure-pipelines.yml file but want it to be built in template.

I'd rather have the ability to create resources templates (whatever the type: pipeline, repository...) than being allowed to define resources in templates. Why?

  • I have some repo with multiple pipelines that require the use the exact same resources.
  • At the same time, those repo may interact with each other inside pipelines (i.e. the pipeline of a repo may need artifacts created from another repo).
  • Then, my templates requires a couple of pipeline resources (or '' for the current pipeline) and with that I can easily get metadata or artifacts from those pipelines whatever the context.

For those who is looking for way to use another repo in template file in case like I provided before I found solution:

Inside my commonly used template I just checked out repo directly without defining repo:

- checkout: git://Global/ThirdParty
      displayName: 'Checkout ThirdParty'
      name: CheckoutThirdParty
Was this page helpful?
0 / 5 - 0 ratings