Search code examples
azurepowershellyamlazure-pipelinesazure-rm-template

Using a powershell script in a template


I have a powershell script that I want to use in a template (edits a file). I want to store the script in the repo that houses the yaml template. I then want to call that template from another repo. Everything seems to work fine but it seems like the powershell script is not being pulled in when the build is triggered. I know there is probably some additional setup I need to do on my end.

This script alters a vdproj file. I want it to remain in the repo that contains the template as I want to be able to reuse that script from other repos that call in that template.

Error that I'm getting: enter image description here

yaml template:

parameters:
  ArtifactPath: ''
  ArtifactName: ''
  ArtifactPublish: false
  Artifacts: []
  PublishClient: false
  Solution: '**/*.sln'
  
jobs:
- job: Build
  displayName: 'Build, Pack, and Publish'
  pool:
    name: 'Windows Self Hosted'
  variables:
    solution: ${{ parameters.Solution }}
    buildPlatform: 'Any CPU'
    buildConfiguration: 'Release'

  steps:
  - task: NuGetToolInstaller@1
    displayName: "Install Nuget Tool"

  - task: NuGetCommand@2
    displayName: 'Restore Nuget Packages'
    inputs:
      command: 'restore'
      restoreSolution: '$(solution)'
      feedsToUse: 'select'
      vstsFeed: '507de2ce-fb6f-41a2-a787-fbfb891bec38'

   - task: PowerShell@2
    inputs:
      targetType: 'filePath'
      filePath: $(System.DefaultWorkingDirectory)\RW.Scripts\PowerShell\Client\UpdateProductVersion.ps1
      arguments: > # Use this to avoid newline characters in multiline string
        -ver $(Build.BuildId)
        -filename "$(System.DefaultWorkingDirectory)\Installers\InstallerExample\InstallerExample.vdproj"
    displayName: 'Change Project Version'

  - script: |
      cd C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE
      devenv "$(Build.Repository.LocalPath)\Example.sln"  /Project "$(Build.Repository.LocalPath)\Installers\InstallerExample\InstallerExample.vdproj" /Build "$(buildConfiguration)"
    displayName: 'Build MSI with DevEnv.'
    condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/development'))

  - task: VSTest@2
    displayName: 'Run Unit Tests'
    inputs:
      platform: '$(buildPlatform)'
      configuration: '$(buildConfiguration)'

  - task: PublishSymbols@2
    displayName: 'Publish Symbols to Symbol Server'
    inputs:
      SearchPattern: '**/bin/**/*.pdb'
      SymbolServerType: 'TeamServices'
            
    - ${{ each artifact in parameters.Artifacts }}:
      - ${{ if eq(artifact.PublishClient, true) }}:
        - task: PublishPipelineArtifact@1
          displayName: 'Publish Artifact: ${{ artifact.ArtifactName }}'
          inputs:
            targetPath: '$(Build.SourcesDirectory)/${{ artifact.ArtifactPath }}'
            artifactName: '${{ artifact.ArtifactName }}'

yaml that calls template:

trigger:
  - release
  - development
  - master
  - feature/*
  - task/*

resources:
  repositories:
    - repository: templates
      name: Project/RepoWithTemplate
      type: git
      ref: refs/heads/development

# This sets the BuildNumber variable
name: $(Date:yyyyMMdd).$(Rev:r)-$(SourceBranchName)
  
jobs:
- template: RW.Yaml.Templates/example.yml@templates
  parameters:
    Solution: '**/Example.sln'
    ArtifactPublish: true
    Artifacts:
    - ArtifactPath: 'Installers/ExampleInstaller/$(buildConfiguration)'
      ArtifactName: 'example'
      ${{ if eq(variables['Build.SourceBranchName'], 'development') }}:
        PublishClient: true

Solution

  • I figured this one out and now feel dumb. I just needed to check both repos out like below and it worked.

      - checkout: self
      - checkout: templates