Problem: step.checkout with lfs reports existing hook error
We run Azure Pipelines using step.checkout with lfs: true to download Git-LFS files when checking out the source code. The step worked fine with Microsoft-hosted agents, and it’s pretty simple, containing only a few properties:
steps:
- checkout: self
lfs: true
persistCredentials: trueHowever, when we switched the pipeline to a self-hosted agent (for better security), the step threw the error below:
git lfs install --local
Hook already exists: pre-push
...
To resolve this, either:
1: run `git lfs update --manual` for instructions on how to merge hooks.
2: run `git lfs update --force` to overwrite your hook.If you Google the error message, you will see a couple of posts telling you to remove .git/hooks, remove git lfs command from your script, or update Git Lfs version. Unfortunately, those solutions do not fit our use case. We do not state git lfs install explicitly in the yaml (as it is invoked indirectly by step.checkout). Moreover, customized commands to delete .git/hooks or run git lfs update –force may be a workaround, but it is not perfect because everything worked fine when using Microsoft-hosted agents.
Solution: clean the workspace before runs
After investigating the options for the runtime environment of self-hosted agents, we identified the root cause: the workspace directory needs to be cleaned before every run. When using MS-hosted agents, jobs are always run on a new agent. However, when using self-hosted agents, according to Azure Pipelines Workspace | Microsoft Learn:
When you run a pipeline on a self-hosted agent, by default, none of the subdirectories other than
$(Build.ArtifactStagingDirectory)and$(Common.TestResultsDirectory)are cleaned in between two consecutive runs. As a result, you can do incremental builds and deployments, provided that tasks are implemented to make use of that. You can override this behavior using theworkspacesetting on the job.
We added workspace: clean: all in the job definition as below to delete the entire Pipeline.Workspace directory before running a new job. Issue resolved!
- job: myJob
workspace:
clean: all