Shallow fetch for repository
Asked Answered
B

3

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..

Brochu answered 17/1, 2019 at 8:13 Comment(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 😊 – Jehias
B
28

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

Brochu answered 17/1, 2019 at 9:25 Comment(2)
developercommunity.visualstudio.com/content/problem/294872/… – Ayeshaayin
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! – Smallclothes
I
12

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.

Intervocalic answered 19/8, 2022 at 23:35 Comment(0)
P
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.

Pallium answered 17/1, 2019 at 8:49 Comment(9)
thanks! any idea where this should be set? (in yaml hierarchy) – Pitchblende
pretty sure thats the root of the hierarchy, so at the first level – Pallium
nvm, found it, under steps – Pitchblende
not sure why it would be understeps, maybe if you need to checkout something directly in the phase – Pallium
it doesnt have to be repository, it can be just repo (works for me across many builds) – Pallium
just tested this under resources/repositories, no effect... trying under steps now – Pitchblende
Confirm this works when under steps... 2019-01-17T09:21:45.1133753Z ##[command]git -c http.extraheader="AUTHORIZATION: bearer ***" fetch --tags --prune --progress --no-recurse-submodules --depth=1 origin – Pitchblende
It's set at the step level as not every job requires the same or any source code - for example non-deployment jobs don't checkout code by default, other steps may want to build a different repo. – Cornall
links change and break, your answer is no longer relevant. – Reasonable

© 2022 - 2024 β€” McMap. All rights reserved.