Skip to content
Snippets Groups Projects
Commit f72899fd authored by Tomasz Maczukin's avatar Tomasz Maczukin
Browse files

Merge branch 'execute-steps-as-is' into 'master'

Execute steps for build as-is, without joining and splitting them

See merge request !626
parents 23bb5278 195890fb
No related branches found
No related tags found
No related merge requests found
...@@ -37,7 +37,16 @@ func GetRemoteLongRunningBuild() (JobResponse, error) { ...@@ -37,7 +37,16 @@ func GetRemoteLongRunningBuild() (JobResponse, error) {
return getRemoteBuildResponse("sleep 3600") return getRemoteBuildResponse("sleep 3600")
} }
func getRemoteBuildResponse(commands string) (response JobResponse, err error) { func GetMultilineBashBuild() (JobResponse, error) {
return getRemoteBuildResponse(`if true; then
bash \
--login \
-c 'echo Hello World'
fi
`)
}
func getRemoteBuildResponse(commands ...string) (response JobResponse, err error) {
response = JobResponse{ response = JobResponse{
GitInfo: GitInfo{ GitInfo: GitInfo{
RepoURL: repoRemoteURL, RepoURL: repoRemoteURL,
...@@ -49,7 +58,7 @@ func getRemoteBuildResponse(commands string) (response JobResponse, err error) { ...@@ -49,7 +58,7 @@ func getRemoteBuildResponse(commands string) (response JobResponse, err error) {
Steps: Steps{ Steps: Steps{
Step{ Step{
Name: StepNameScript, Name: StepNameScript,
Script: StepScript{commands}, Script: commands,
When: StepWhenAlways, When: StepWhenAlways,
AllowFailure: false, AllowFailure: false,
}, },
...@@ -59,7 +68,7 @@ func getRemoteBuildResponse(commands string) (response JobResponse, err error) { ...@@ -59,7 +68,7 @@ func getRemoteBuildResponse(commands string) (response JobResponse, err error) {
return return
} }
func getLocalBuildResponse(commands string) (response JobResponse, err error) { func getLocalBuildResponse(commands ...string) (response JobResponse, err error) {
localRepoURL, err := getLocalRepoURL() localRepoURL, err := getLocalRepoURL()
if err != nil { if err != nil {
return return
...@@ -76,7 +85,7 @@ func getLocalBuildResponse(commands string) (response JobResponse, err error) { ...@@ -76,7 +85,7 @@ func getLocalBuildResponse(commands string) (response JobResponse, err error) {
Steps: Steps{ Steps: Steps{
Step{ Step{
Name: StepNameScript, Name: StepNameScript,
Script: StepScript{commands}, Script: commands,
When: StepWhenAlways, When: StepWhenAlways,
AllowFailure: false, AllowFailure: false,
}, },
......
...@@ -442,3 +442,16 @@ func TestBuildWithDebugTrace(t *testing.T) { ...@@ -442,3 +442,16 @@ func TestBuildWithDebugTrace(t *testing.T) {
assert.Regexp(t, `[^$] echo Hello World`, out) assert.Regexp(t, `[^$] echo Hello World`, out)
}) })
} }
func TestBuildMultilineCommand(t *testing.T) {
multilineBuild, err := common.GetMultilineBashBuild()
assert.NoError(t, err)
build, cleanup := newBuild(t, multilineBuild, "bash")
defer cleanup()
// The default build shouldn't have debug tracing enabled
out, err := runBuildReturningOutput(t, build)
assert.NoError(t, err)
assert.NotContains(t, out, "bash")
assert.Contains(t, out, "Hello World")
assert.Contains(t, out, "collapsed multi-line command")
}
...@@ -68,15 +68,15 @@ func (c *GitLabCiYamlParser) prepareJobInfo(job *common.JobResponse) (err error) ...@@ -68,15 +68,15 @@ func (c *GitLabCiYamlParser) prepareJobInfo(job *common.JobResponse) (err error)
func (c *GitLabCiYamlParser) getCommands(commands interface{}) (common.StepScript, error) { func (c *GitLabCiYamlParser) getCommands(commands interface{}) (common.StepScript, error) {
if lines, ok := commands.([]interface{}); ok { if lines, ok := commands.([]interface{}); ok {
text := "" var steps common.StepScript
for _, line := range lines { for _, line := range lines {
if lineText, ok := line.(string); ok { if lineText, ok := line.(string); ok {
text += lineText + "\n" steps = append(steps, lineText)
} else { } else {
return common.StepScript{}, errors.New("unsupported script") return common.StepScript{}, errors.New("unsupported script")
} }
} }
return common.StepScript(strings.Split(text, "\n")), nil return steps, nil
} else if text, ok := commands.(string); ok { } else if text, ok := commands.(string); ok {
return common.StepScript(strings.Split(text, "\n")), nil return common.StepScript(strings.Split(text, "\n")), nil
} else if commands != nil { } else if commands != nil {
...@@ -117,9 +117,7 @@ func (c *GitLabCiYamlParser) prepareSteps(job *common.JobResponse) (err error) { ...@@ -117,9 +117,7 @@ func (c *GitLabCiYamlParser) prepareSteps(job *common.JobResponse) (err error) {
if err != nil { if err != nil {
return return
} }
for _, scriptLine := range script { scriptCommands = append(scriptCommands, script...)
scriptCommands = append(scriptCommands, scriptLine)
}
afterScriptCommands, err = c.getCommands(c.jobConfig["after_script"]) afterScriptCommands, err = c.getCommands(c.jobConfig["after_script"])
if err != nil { if err != nil {
......
...@@ -339,12 +339,17 @@ func (b *AbstractShell) writeDownloadArtifactsScript(w ShellWriter, info common. ...@@ -339,12 +339,17 @@ func (b *AbstractShell) writeDownloadArtifactsScript(w ShellWriter, info common.
} }
// Write the given string of commands using the provided ShellWriter object. // Write the given string of commands using the provided ShellWriter object.
func (b *AbstractShell) writeCommands(w ShellWriter, commands string) { func (b *AbstractShell) writeCommands(w ShellWriter, commands ...string) {
commands = strings.TrimSpace(commands) for _, command := range commands {
for _, command := range strings.Split(commands, "\n") {
command = strings.TrimSpace(command) command = strings.TrimSpace(command)
if command != "" { if command != "" {
w.Notice("$ %s", command) lines := strings.SplitN(command, "\n", 2)
if len(lines) > 1 {
// TODO: this should be collapsable once we introduce that in GitLab
w.Notice("$ %s # collapsed multi-line command", lines[0])
} else {
w.Notice("$ %s", lines[0])
}
} else { } else {
w.EmptyLine() w.EmptyLine()
} }
...@@ -373,8 +378,7 @@ func (b *AbstractShell) writeUserScript(w ShellWriter, info common.ShellScriptIn ...@@ -373,8 +378,7 @@ func (b *AbstractShell) writeUserScript(w ShellWriter, info common.ShellScriptIn
b.writeCommands(w, info.PreBuildScript) b.writeCommands(w, info.PreBuildScript)
} }
commands := strings.Join(scriptStep.Script, "\n") b.writeCommands(w, scriptStep.Script...)
b.writeCommands(w, commands)
if info.PostBuildScript != "" { if info.PostBuildScript != "" {
b.writeCommands(w, info.PostBuildScript) b.writeCommands(w, info.PostBuildScript)
...@@ -503,18 +507,7 @@ func (b *AbstractShell) writeAfterScript(w ShellWriter, info common.ShellScriptI ...@@ -503,18 +507,7 @@ func (b *AbstractShell) writeAfterScript(w ShellWriter, info common.ShellScriptI
b.writeCdBuildDir(w, info) b.writeCdBuildDir(w, info)
w.Notice("Running after script...") w.Notice("Running after script...")
b.writeCommands(w, afterScriptStep.Script...)
for _, command := range afterScriptStep.Script {
command = strings.TrimSpace(command)
if command != "" {
w.Notice("$ %s", command)
} else {
w.EmptyLine()
}
w.Line(command)
w.CheckForErrors()
}
return nil return nil
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment