Skip to content

Commit

Permalink
Fix shell validate failed (#1002)
Browse files Browse the repository at this point in the history
Co-authored-by: stark <[email protected]>
  • Loading branch information
ks-ci-bot and chilianyi authored Oct 11, 2023
1 parent 105ee83 commit e33901e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
7 changes: 6 additions & 1 deletion controllers/jenkins/pipeline/json_converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ func (r *JenkinsfileReconciler) reconcileJenkinsfileEditMode(pip *v1alpha3.Pipel
// Users are able to clean jenkinsfile
if jenkinsfile != "" {
var toJSONResult core.GenericResult
jenkinsfile = strings.ReplaceAll(jenkinsfile, "\\", "\\\\") // escape backslash
if toJSONResult, err = coreClient.ToJSON(jenkinsfile); err != nil || toJSONResult.GetStatus() != "success" {
r.log.Error(err, "failed to convert jenkinsfile to json format")
if err != nil {
Expand Down Expand Up @@ -168,9 +169,13 @@ func (r *JenkinsfileReconciler) reconcileJSONEditMode(pip *v1alpha3.Pipeline, pi
err = r.updateAnnotations(pip.Annotations, pipelineKey)
return
}
jenkinsfile := toResult.GetResult()
jenkinsfile = strings.ReplaceAll(jenkinsfile, "\\\\", "\\") // unescape backslash
jenkinsfile = strings.ReplaceAll(jenkinsfile, `\'`, `'`) // unescape single quote

pip.Annotations[v1alpha3.PipelineJenkinsfileEditModeAnnoKey] = ""
pip.Annotations[v1alpha3.PipelineJenkinsfileValidateAnnoKey] = v1alpha3.PipelineJenkinsfileValidateSuccess
err = r.updateAnnotationsAndJenkinsfile(pip.Annotations, toResult.GetResult(), pipelineKey)
err = r.updateAnnotationsAndJenkinsfile(pip.Annotations, jenkinsfile, pipelineKey)
}
return
}
Expand Down
6 changes: 5 additions & 1 deletion pkg/api/devops/v1alpha3/steptemplate_render.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ func dslRender(dslTpl string, param map[string]interface{}, secret *v1.Secret) (

func shellRender(shellTpl string, param map[string]interface{}, secret *v1.Secret) (output string, err error) {
if output, err = dslRender(shellTpl, param, secret); err == nil {
escapedOutput := strings.ReplaceAll(output, `\`, `\\`)
escapedOutput = strings.ReplaceAll(escapedOutput, "\n", "\\n")
escapedOutput = strings.ReplaceAll(escapedOutput, `"`, `\"`)

output = fmt.Sprintf(`{
"arguments": [
{
Expand All @@ -148,7 +152,7 @@ func shellRender(shellTpl string, param map[string]interface{}, secret *v1.Secre
}
],
"name": "sh"
}`, strings.ReplaceAll(output, "\n", "\\n"))
}`, escapedOutput)
}
return
}
14 changes: 11 additions & 3 deletions pkg/api/devops/v1alpha3/steptemplate_render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,16 @@ import (

func Test_handler_stepTemplateRender(t *testing.T) {
stepTemplate := &StepTemplateSpec{
Template: `echo 1`,
Runtime: "shell",
Template: `cat > log.sh << EOF
for ((i=1; i<=1000000; i++))
do
echo "Log message number \\$i: This is a sample log message."
done
EOF
cat log.sh
bash log.sh`,
Runtime: "shell",
}
stepTemplateWithParameters := &StepTemplateSpec{
Template: `docker login -u $USERNAMEVARIABLE -p $PASSWORDVARIABLE
Expand Down Expand Up @@ -74,7 +82,7 @@ docker build {{.param.context}} -t {{.param.tag}} -f {{.param.dockerfile.path}}`
"key": "script",
"value": {
"isLiteral": true,
"value": "echo 1"
"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"
}
}
],
Expand Down

0 comments on commit e33901e

Please sign in to comment.