0
votes

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?

1
Don't think template should be pipelines. Templates are meant to be inserted into existing pipelines. Know it's not what you are asking but feel this is a pretty big diversion from what would be considered best practices. Consider making changes to the template repo in a new branch and select Resources->YAML Templatel-> banch version for testing out new changes.DreadedFrost
@DreadedFrost Hi, thanks for reaching out. I get what you're saying, i do seriously want to have the tooling when editing the yaml however that's the only reason why i've added them as a pipeline. Could you clarify what you mean with Resources->YAML Templatel-> banch version cause i don't quite follow.sommmen
Templates should be a repository resource never a pipeline docs.microsoft.com/en-us/azure/devops/pipelines/process/… and don't get hung up using the web UI if you want to create serious pipelines I suggest vscode + azure pipelines extension (with intellisense). I have a bunch of templates in case you need inspiration for best practice github.com/f2calv/CasCap.YAMLTemplates/tree/master/templates which are consumed here github.com/f2calv/CasCap.Apis.GooglePhotos/blob/master/…alv
@alv hi alv, thanks for your time. I understand this is not normal practice, however i want to use the tooling azure pipelines provide, where i can just press settings of a task and i get all the information about said task. I did not yet know about the vscode extension, thanks for that - although that unfortunately also does not provide the aforementioned feature. Also thanks for your example repo's - ill take a peek monday.sommmen
@sommen if you want to do that just hit the YAML button in the UI and can copy the YAML and insert in the pipeline. May need to massage it a bit but is a good start.DreadedFrost

1 Answers

0
votes

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.