Skip to content

Commit fb7ac29

Browse files
ks-ci-botstark
andauthored
Fix shell validate failed (#1003)
Co-authored-by: stark <stark@yunify.com>
1 parent 34d7f86 commit fb7ac29

3 files changed

Lines changed: 22 additions & 5 deletions

File tree

controllers/jenkins/pipeline/json_converter.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ func (r *JenkinsfileReconciler) reconcileJenkinsfileEditMode(pip *v1alpha3.Pipel
112112
// Users are able to clean jenkinsfile
113113
if jenkinsfile != "" {
114114
var toJSONResult core.GenericResult
115+
jenkinsfile = strings.ReplaceAll(jenkinsfile, "\\", "\\\\") // escape backslash
115116
if toJSONResult, err = coreClient.ToJSON(jenkinsfile); err != nil || toJSONResult.GetStatus() != "success" {
116117
r.log.Error(err, "failed to convert jenkinsfile to json format")
117118
if err != nil {
@@ -168,9 +169,13 @@ func (r *JenkinsfileReconciler) reconcileJSONEditMode(pip *v1alpha3.Pipeline, pi
168169
err = r.updateAnnotations(pip.Annotations, pipelineKey)
169170
return
170171
}
172+
jenkinsfile := toResult.GetResult()
173+
jenkinsfile = strings.ReplaceAll(jenkinsfile, "\\\\", "\\") // unescape backslash
174+
jenkinsfile = strings.ReplaceAll(jenkinsfile, `\'`, `'`) // unescape single quote
175+
171176
pip.Annotations[v1alpha3.PipelineJenkinsfileEditModeAnnoKey] = ""
172177
pip.Annotations[v1alpha3.PipelineJenkinsfileValidateAnnoKey] = v1alpha3.PipelineJenkinsfileValidateSuccess
173-
err = r.updateAnnotationsAndJenkinsfile(pip.Annotations, toResult.GetResult(), pipelineKey)
178+
err = r.updateAnnotationsAndJenkinsfile(pip.Annotations, jenkinsfile, pipelineKey)
174179
}
175180
return
176181
}

pkg/api/devops/v1alpha3/steptemplate_render.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ func dslRender(dslTpl string, param map[string]interface{}, secret *v1.Secret) (
137137

138138
func shellRender(shellTpl string, param map[string]interface{}, secret *v1.Secret) (output string, err error) {
139139
if output, err = dslRender(shellTpl, param, secret); err == nil {
140+
escapedOutput := strings.ReplaceAll(output, `\`, `\\`)
141+
escapedOutput = strings.ReplaceAll(escapedOutput, "\n", "\\n")
142+
escapedOutput = strings.ReplaceAll(escapedOutput, `"`, `\"`)
143+
140144
output = fmt.Sprintf(`{
141145
"arguments": [
142146
{
@@ -148,7 +152,7 @@ func shellRender(shellTpl string, param map[string]interface{}, secret *v1.Secre
148152
}
149153
],
150154
"name": "sh"
151-
}`, strings.ReplaceAll(output, "\n", "\\n"))
155+
}`, escapedOutput)
152156
}
153157
return
154158
}

pkg/api/devops/v1alpha3/steptemplate_render_test.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,16 @@ import (
2828

2929
func Test_handler_stepTemplateRender(t *testing.T) {
3030
stepTemplate := &StepTemplateSpec{
31-
Template: `echo 1`,
32-
Runtime: "shell",
31+
Template: `cat > log.sh << EOF
32+
for ((i=1; i<=1000000; i++))
33+
do
34+
echo "Log message number \\$i: This is a sample log message."
35+
done
36+
EOF
37+
38+
cat log.sh
39+
bash log.sh`,
40+
Runtime: "shell",
3341
}
3442
stepTemplateWithParameters := &StepTemplateSpec{
3543
Template: `docker login -u $USERNAMEVARIABLE -p $PASSWORDVARIABLE
@@ -74,7 +82,7 @@ docker build {{.param.context}} -t {{.param.tag}} -f {{.param.dockerfile.path}}`
7482
"key": "script",
7583
"value": {
7684
"isLiteral": true,
77-
"value": "echo 1"
85+
"value": "cat > log.sh << EOF\n for ((i=1; i<=1000000; i++))\n do\n echo \"Log message number \\\\$i: This is a sample log message.\"\n done\nEOF\n\n cat log.sh\n bash log.sh"
7886
}
7987
}
8088
],

0 commit comments

Comments
 (0)