Azure pipeline git checkout with variable depth and default value

Viewed 904

I have the following code in a Azure Pipeline YML.

In checkout.yml (which is used as template)

- checkout: self
  clean: true
  fetchDepth: 100
  lfs: true
  submodules: recursive
  persistCredentials: true

I want to make the fetchDepth dynamic. I could use a variable. But I also want to set a default value for that variable. The aim is, that a caller can include this file as template.

some_file.yml:

steps:
  template: checkout.yml

I want to make possible that some_file.yml can set fetchDepth to some other value but does not need to do this. I tried with $(something) but how to I set a default? Or would $[something] the way to use?

I am stuck here, but I assume the answer could be very simple :-(

Thank you,

1 Answers

You can accomplish this via template parameters and using a default value for fetchDepth within the parameter itself.

Here's an example using your checkout task:

checkout-task-template-example.yml

parameters:
    fetchDepth: 100
steps:
  - checkout: self
    clean: true
    fetchDepth: ${{ parameters.fetchDepth }}
    lfs: true
    submodules: recursive
    persistCredentials: true

checkout-pipeline-example.yml

name: Stackoverflow-Example-Checkout
trigger:
    - none
stages:
- stage: Deploy_Production
  pool:
    vmImage: ubuntu-latest
  jobs:
    - deployment: DeployWeb
      displayName: Deploy Web App
      environment: YourApp-QA
      pool:
        vmImage: 'ubuntu-latest'
      strategy:
        runOnce:
          deploy:
            steps:
              # Example: Use Default
              - template: checkout-task-template-example.yml
              # Example: Override Default
              - template: checkout-task-template-example.yml
                parameters:
                  fetchDepth: 10

Here's Microsoft's documentation on YAML templates, and parameters specifically:

Related