Skip CleanupFileVariables stage if no file variables

In https://gitlab.com/gitlab-org/gitlab-runner/-/issues/27001 we
introduced a step that will clean up file based variables. However if we
are still running this stage even if there is no file based variables
defined. If we keep running the step no matter what we are wasting
compute resources and it wouldn't be consistent with our other steps
where they are skipped it it would be a no-op.

Update the test to create a brand new `build` and `info` so the
JobVariables aren't "cached" from the previous run.

closes https://gitlab.com/gitlab-org/gitlab-runner/-/issues/27057
parent 3f96fcfc
......@@ -735,14 +735,21 @@ func (b *AbstractShell) writeArchiveCacheOnFailureScript(w ShellWriter, info com
}
func (b *AbstractShell) writeCleanupFileVariablesScript(w ShellWriter, info common.ShellScriptInfo) error {
skipCleanupFileVariables := true
for _, variable := range info.Build.GetAllVariables() {
if !variable.File {
continue
}
skipCleanupFileVariables = false
w.RmFile(w.TmpFile(variable.Key))
}
if skipCleanupFileVariables {
return common.ErrSkipBuildStage
}
return nil
}
......
......@@ -970,6 +970,26 @@ func TestSkipBuildStage(t *testing.T) {
},
},
},
common.BuildStageCleanupFileVariables: {
"don't skip if file artifact defined": {
common.JobResponse{
Variables: common.JobVariables{
{
Key: "FILE_VARIABLE",
Value: "CONTENTS",
Public: true,
Internal: true,
File: true,
},
},
},
common.RunnerConfig{
RunnerCredentials: common.RunnerCredentials{
URL: "https://example.com",
},
},
},
},
}
shell := AbstractShell{}
......@@ -996,9 +1016,15 @@ func TestSkipBuildStage(t *testing.T) {
err,
)
// stages with bare minimum requirements should not be skipped
build.JobResponse = tc.JobResponse
build.Runner = &tc.Runner
// stages with bare minimum requirements should not be skipped.
build = &common.Build{
JobResponse: tc.JobResponse,
Runner: &tc.Runner,
}
info = common.ShellScriptInfo{
RunnerCommand: "gitlab-runner-helper",
Build: build,
}
err = shell.writeScript(&BashWriter{}, stage, info)
assert.NoError(t, err, "stage %v should not have been skipped", stage)
})
......
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