'Stop azure pipelines from running my template files

I am trying to use Azure pipeline templates to organize my build. I am using multiple repositories for this:

  • BuildTemplates
  • MySoftwareProject1
  • MySoftwareProject2
  • MySoftwareProjectN

The BuildTemplates repo contains all templates and I want to use these from my other repos. I am trying to make sure that when the BuildTemplates repo is changed - not a single pipeline gets triggered to run (these are templates and should not run) - more on this later. The folder structure is like so:

enter image description here

I've also added these templates as pipeline to Azure (which is a bit unconventional):

enter image description here

This is great because now I can use the editor for azure pipelines. If I edit the .yml directly from the repo, or do that locally, I don't get the nice tooling:

  1. Editing yaml file in azure pipelines: enter image description here

  2. Editing yaml file in azure repos: enter image description here

This works but now every time the BuildTemplates repository is updated, it will run these template pipelines - which will fail.

I've tried to set trigger: none which works when importing jobs, but not when importing steps. Here are all the yaml files that make up this build:

trigger:
- master

pool:
  vmImage: 'windows-latest'

resources:
  repositories:
    - repository: BuildTemplates
      type: git
      name: HMI/BuildTemplates

extends:
  template: NuGet/Jobs/NuGet.Build.yml@BuildTemplates
# Don't run templates NOTE: this works!
trigger: none

parameters:
 - name: packagesToPack
   type: string
   default: '**/*.nuspec'

jobs:
- job: package
  steps:
  - template: ../Steps/NuGet.Build.yml
    parameters:
      packagesToPack: ${{ parameters.packagesToPack }}
# Don't run templates NOTE: this does not work!
# trigger: none

steps:
  - task: NuGetToolInstaller@1
  - task: NuGetCommand@2
    inputs:
      command: 'pack'
      packagesToPack: ${{ parameters.packagesToPack }}
      versioningScheme: 'byPrereleaseNumber'
      majorVersion: '7'
      minorVersion: '1'
      patchVersion: '0'
  - task: PublishBuildArtifacts@1
    inputs:
      PathtoPublish: '$(Build.ArtifactStagingDirectory)'
      ArtifactName: 'Files'
      publishLocation: 'Container'

When I uncomment #trigger: none from the /steps/NuGet.Build.yml file the following happens when trying to run a build (with the first mentioned yaml file)

enter image description here

/NuGet/Steps/NuGet.Build.yml@BuildTemplates (Line: 3, Col: 1): Unexpected value 'trigger'

My question therefore is: How can I stop templates from running automatically when I have them defined as a pipeline so that I can use the pipeline editor?

Can I disable the pipeline from running somewhere? Can I conditionally add 'trigger: none'?

I was also thinking of create a trigger-none.yml file with the contents being only trigger: none and use extend to optionally include it or something - but I don't believe that would work.

Is there perhaps another way I can edit the templates with tooling support - is there for example a vscode extension or something?



Solution 1:[1]

Unfortunately there is no real way to add a yaml file to a pipeline and stop it from running if your file contains only steps. It will result in yaml compile errors.

So it seems like there is no way to get the nice online editor for yaml templates.

The next best thing is what DreadedFrost proposed, The VSCode extension.

I've added a powershell script to auto update the schema definition:

# This powershell updates the Yaml schema file.

Set-StrictMode -Version Latest

# Configure these options before running
#

# The personal access token to use. 
# Grant the Token Administration(Read & manage) or Tokens(Read & manage) permission to the PAT.
# Also see: https://stackoverflow.com/a/64868268/4122889
$pat = 'TODO'

# Name of the organization 
$organizationName = "TODO"

$yamlSchemaPartUrl = "https://dev.azure.com/{0}/_apis/distributedtask/yamlschema?api-version=6.0" # &accessToken={1}
$yamlSchemaUrl = $yamlSchemaPartUrl -f $organizationName #, $pat
$outputFile = ".\yamlschema.json"

Write-Host "Going to fetch file at " $yamlSchemaUrl " to: " $(Resolve-Path -Path $outputFile)

# See the resources below for how authentication with the PAT works:
# https://tenbulls.co.uk/2020/03/11/querying-azure-devops-rest-api-with-powershell/
# https://docs.microsoft.com/en-us/rest/api/azure/devops/?view=azure-devops-rest-6.1#assemble-the-request
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($pat)"))

$props = @{
    Uri = $yamlSchemaUrl
    Method = 'GET'
    ContentType = 'application/json'    
    Headers = @{authorization = "Basic $token"}
}

# Write-Host $props.Headers.accessToken

Invoke-RestMethod @props -OutFile $outputFile

# Invoke-WebRequest $yamlSchemaUrl -OutFile $outputFile

Write-Host "Done, please restart VsCode to apply the changes."
Write-Warning "Make sure the output file is correct - if it is a bunch of html, something likely went wrong with the PAT"
Write-Host "if thats the case download the yaml schema manually via the browser, or fix the PAT"

I still find myself copy and pasting items into the online editor however.

Solution 2:[2]

There is an easier way without using YAML.

  1. I will go to the pipeline
  2. Click 3 dots, and click Settings

enter image description here

  1. Set it to Disabled

enter image description here

The pipeline will never trigger, but you can call it from another YAML as a template and it will run within that YAML.

This is the way I have solved it in my case and it works just fine.

My best advice is to document this part internally for later use.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 sommmen
Solution 2 Maytham Fahmi