19

I have an Azure Pipeline (yaml) which uses templates and I'm trying to figure out how to setup the fetch depth of the actual repository being cloned.

resources:
  repositories:
    - repository: templates
      type: git
      name: 'DevOps/CICD'
      ref: refs/heads/develop
    - repository: self # sic!
      fetchDepth: 1
      clean: true`

Fetch depth is supported (vscode extension) but I can't seem to find any extensive documentation on it..

Anthon
  • 69,918
  • 32
  • 186
  • 246
grmbl
  • 2,514
  • 4
  • 29
  • 54
  • 1
    Just a comment for in case it's been missed so far, when you use templates it doesn't do a checkout of repo you reference, it just references those yaml files. The accepted answer mentions doing an actual checkout which would give you access to the other non yaml files from where you are referencing the template but are 2 different things – Gordon Beeming Jan 06 '22 at 07:52

3 Answers3

26

Placing this under steps works for me:

steps:
  - checkout: self
    fetchDepth: 1
    clean: true
  - task: NuGetCommand@2
  ...

Results in:

2019-01-17T09:21:45.1133753Z ##[command]git -c http.extraheader="AUTHORIZATION: bearer ***" fetch --tags --prune --progress --no-recurse-submodules --depth=1 origin

grmbl
  • 2,514
  • 4
  • 29
  • 54
  • 1
    https://developercommunity.visualstudio.com/content/problem/294872/yaml-build-ignores-fetchdepth.html – mloskot Jan 20 '19 at 17:49
  • 1
    NOTE: If you are using tags you may still be downloading more than you expect. You can disable tags by using "fetchTags: false" alongside "fetchDepth: 1" as seen in the above. I had the above step and could see "--depth=1" in my pipeline output, but it was still downloading too much, disabling tags fixed it for me! – Peter Rasmussen May 16 '23 at 14:06
11

Another option is to add the shallow fetch setting into the Variables section of your YAML pipeline:

variables:
  Agent.Source.Git.ShallowFetchDepth: 1

Azure Pipelines will automatically recognize this setting and use it as a --depth=1 argument when it does git fetch.

Note that this only applies to pipelines created before September 2022 – pipelines created after that date will automatically fetch with a depth of 1, so it doesn't need to be explicitly configured.

Craig Brown
  • 1,891
  • 1
  • 24
  • 25
5

here's the link you are looking for: https://learn.microsoft.com/en-us/azure/devops/pipelines/yaml-schema?view=vsts&tabs=schema#checkout

the property is fetchDepth indeed.

4c74356b41
  • 69,186
  • 6
  • 100
  • 141