Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to refrence files inside Azure DevOps pipeline templates when these templates reside in a standalone repo?

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
like image 894
Paulo Correia Avatar asked Sep 02 '25 14:09

Paulo Correia


2 Answers

Update 2021

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

Original Answer

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.

like image 123
DSpirit Avatar answered Sep 05 '25 06:09

DSpirit


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.

like image 22
Victorio Berra Avatar answered Sep 05 '25 07:09

Victorio Berra