I'm setting up several pipelines in Azure DevOps. To make my teams life easier, I'm using job templates.
These job templates are in a a proper repository, just for them.
For every pipeline I define the repository to get the templates from.
Some tasks in these templates run powershell code, and I want this code to be in a script file, to be reusable and stored in the same repo as the template.
When the pipelines runs, the template is embeded, it tries to locate the powershell script inside project repo actually being built/deployed.
How can i achieve this?
The workaround is to have inline code which I really don't want to have.
Any constructive answer will be very appreciated.
Thanks
After some digging I couldn't find any way to specify a script file as source to powershell task in a template.
Inside pipeline definition:
resources:
  repositories:
    - repository: templates
      type: git
      name: deploy-templates
variables:
  artifactName: 'Trade Data ETL - $(Build.SourceBranchName)'
stages:
- stage: Build
  displayName: Build
  variables:
  - group: DEV-Credential-Group
  - group: COMMON-Settings-Group
  jobs:
  - template: ssis/pipelines/stage-build.yml@templates  # Template reference
    parameters:
      artifactName: '$(artifactName)'
Inside template file:
- task: PowerShell@2
    inputs:
      filePath: ssis/pipelines/scripts/build-ssis-project.ps1
      arguments: '-ProjectToBuild "tradedata-ldz-ssis/tradedata-ldz-ssis.dtproj'
      pwsh: true
According to learn.microsoft.com, you can now also check out multiple repositories without custom scripting. If you check out more than one repository, a separate folder containing the repository is created below $(Build.SourcesDirectory).
You can define multiple repositories like this:
resources:
  repositories:
    - repository: devops
      type: git
      name: DevOps
      ref: main
    - repository: infrastructure
      type: git
      name: Infrastructure
      ref: main
And in the steps simply check them out as follows:
steps:
 - checkout: self
 - checkout: devops
 - checkout: infrastructure
 # List all available repositories
 - script: ls
Currently the resources command only supports yml files in other repositories. However, you could simply checkout the repository in a task and then run the desired powershell script.
steps:
- task: PowerShell@2
  inputs:
    targetType: inline
    script: |
      git clone -b <your-desired-branch> https://azuredevops:$($env:token)@dev.azure.com/<your-organization>/<your-project>/_git/<your-repository> <target-folder-name>      
      ./<target-folder-name>/foo.ps1
  env:
    token: $(System.AccessToken)
This script would checkout an arbitrary branch and execute a script foo.ps1 in the root of the target repository.
Call - checkout: templates inside the template file. This might only work when you insert a template but it successfully sees the repository resource and pulls it down.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With