0

We use shared YAML templates extensively in our YAML builds. Something like this:

trigger: none

resources:
  repositories:
    - repository: templates
      type: git
      name: DFDevOps\cicd-templates
      ref: refs/tags/stable-1
      # http://tdc1tfsapp01:8080/tfs/DefaultCollection/DFDevOps/_git/cicd-templates

name: $(BuildVersionPrefix).$(DayOfYear)$(Date:HH)

jobs:
  - job: Build
    pool:
      demands: DotNetFramework
    workspace:
      clean: outputs
    timeoutInMinutes: 180
    steps:
      - checkout: self # self represents the repo where the initial Pipelines YAML file was found
        clean: true
        lfs: true

      - template: ci/ci-build.yml@templates
        parameters:
          releaseBranch: $(ReleaseBranch)

However, the shared YAML templates often need to execute Powershell code. So far we either embed it or put it in a PS module, which the templates load.

I hate both approaches. I hate embedding because:

  1. No intellisens
  2. No unit testing
  3. No proper error reporting on pipeline failures

I hate putting in a module, because it is detached from the origin and requires a lot of overhead.

My question - is it possible to have the PS code in dedicated ps1 files in the same repo as the YAML templates and yet have them available at run time?

mark
  • 59,016
  • 79
  • 296
  • 580
  • How about the issue? Does the answer below resolved your question, If not, would you please let me know the latest information about this issue? – Leo Liu Feb 16 '21 at 07:38
  • I have not checked it yet. But I will and be back. – mark Feb 16 '21 at 13:25

1 Answers1

0

So, checkout: self will refer to the repository in which that particular pipeline YAML lives - not the templates repository (unless they are one and the same).

I would add a second checkout step - checkout: templates - in the pipeline and then in the inputs for your powershell script task (in the template), specify the path to the script you just checked out:

        scriptType: filePath
        scriptPath: $(Build.SourcesDirectory)/templates/pathToscript.ps1

The template is just expanded in place into the pipeline at compile time, so it should have access to that folder. You can verify this now in the YAML pipeline editor by downloading the expanded pipeline: Download full YAML

WaitingForGuacamole
  • 3,744
  • 1
  • 8
  • 22
  • Thank you. I will test it. Can you think of a way to do it without touching the top most YAML? Because we will have to modify a lot of YAMLs. Maybe the templates repository is already available somewhere? After all, its YAMLs are already in use... – mark Feb 11 '21 at 18:43
  • I would imagine that the template could include its own checkout step, might be worth trying. – WaitingForGuacamole Feb 11 '21 at 21:17
  • Very interesting idea. I will have to check it. – mark Feb 11 '21 at 22:54