Handle LFS only if it was not disabled by the job

parent 692ae235
......@@ -774,3 +774,12 @@ func (b *Build) IsFeatureFlagOn(name string) bool {
return on
}
func (b *Build) IsLFSSmudgeDisabled() bool {
disabled, err := strconv.ParseBool(b.GetAllVariables().Get("GIT_LFS_SKIP_SMUDGE"))
if err != nil {
return false
}
return disabled
}
......@@ -1020,3 +1020,52 @@ func TestWaitForTerminal(t *testing.T) {
})
}
}
func TestBuild_IsLFSSmudgeDisabled(t *testing.T) {
testCases := map[string]struct {
isVariableUnset bool
variableValue string
expectedResult bool
}{
"variable not set": {
isVariableUnset: true,
expectedResult: false,
},
"variable empty": {
variableValue: "",
expectedResult: false,
},
"variable set to true": {
variableValue: "true",
expectedResult: true,
},
"variable set to false": {
variableValue: "false",
expectedResult: false,
},
"variable set to 1": {
variableValue: "1",
expectedResult: true,
},
"variable set to 0": {
variableValue: "0",
expectedResult: false,
},
}
for testName, testCase := range testCases {
t.Run(testName, func(t *testing.T) {
b := &Build{
JobResponse: JobResponse{
Variables: JobVariables{},
},
}
if !testCase.isVariableUnset {
b.Variables = append(b.Variables, JobVariable{Key: "GIT_LFS_SKIP_SMUDGE", Value: testCase.variableValue, Public: true})
}
assert.Equal(t, testCase.expectedResult, b.IsLFSSmudgeDisabled())
})
}
}
......@@ -360,11 +360,17 @@ func (b *AbstractShell) writeCloneFetchCmds(w ShellWriter, info common.ShellScri
w.Warning("DEPRECATION: this GitLab server doesn't support refspecs, gitlab-runner 12.0 will no longer work with this version of GitLab")
}
// We're disabling smudging to prevent us from memory allocation failures.
// If LFS smudging was disabled by the user (by setting the GIT_LFS_SKIP_SMUDGE variable
// when defining the job) we're skipping this step.
//
// In other case we're disabling smudging here to prevent us from memory
// allocation failures.
//
// Please read https://gitlab.com/gitlab-org/gitlab-runner/issues/3366 and
// https://github.com/git-lfs/git-lfs/issues/3524 for context.
w.Variable(common.JobVariable{Key: "GIT_LFS_SKIP_SMUDGE", Value: "1"})
if !build.IsLFSSmudgeDisabled() {
w.Variable(common.JobVariable{Key: "GIT_LFS_SKIP_SMUDGE", Value: "1"})
}
switch info.Build.GetGitStrategy() {
case common.GitFetch:
......@@ -390,15 +396,24 @@ func (b *AbstractShell) writeCloneFetchCmds(w ShellWriter, info common.ShellScri
if info.Build.GetGitCheckout() {
b.writeCheckoutCmd(w, build)
// Because we've disabled LFS smudging above, we need now manually call `git lfs pull`
// to fetch and checkout all LFS objects that may be present in the repository.
// If LFS smudging was disabled by the user (by setting the GIT_LFS_SKIP_SMUDGE variable
// when defining the job) we're skipping this step.
//
// In other case, because we've disabled LFS smudging above, we need now manually call
// `git lfs pull` to fetch and checkout all LFS objects that may be present in
// the repository.
//
// Repositories without LFS objects (and without any LFS metadata) will be not
// affected by this command.
w.IfCmd("git-lfs", "version")
w.Command("git", "lfs", "pull")
w.EmptyLine()
w.EndIf()
//
// Please read https://gitlab.com/gitlab-org/gitlab-runner/issues/3366 and
// https://github.com/git-lfs/git-lfs/issues/3524 for context.
if !build.IsLFSSmudgeDisabled() {
w.IfCmd("git-lfs", "version")
w.Command("git", "lfs", "pull")
w.EmptyLine()
w.EndIf()
}
} else {
w.Notice("Skipping Git checkout")
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment