1
0
mirror of https://github.com/woodpecker-ci/woodpecker.git synced 2025-11-23 21:44:44 +02:00

GenerateScript should not return encoded script (#1397)

followup to #1395
This commit is contained in:
6543
2022-11-06 13:36:34 +01:00
committed by GitHub
parent 18311d4360
commit e8490a757f
8 changed files with 64 additions and 43 deletions

View File

@@ -16,18 +16,31 @@ package local
import (
"context"
"encoding/base64"
"fmt"
"io"
"os"
"os/exec"
"strings"
"github.com/woodpecker-ci/woodpecker/pipeline/backend/common"
"github.com/alessio/shellescape"
"golang.org/x/exp/slices"
"github.com/woodpecker-ci/woodpecker/pipeline/backend/types"
"github.com/woodpecker-ci/woodpecker/shared/constant"
)
// notAllowedEnvVarOverwrites are all env vars that can not be overwritten by step config
var notAllowedEnvVarOverwrites = []string{
"CI_NETRC_MACHINE",
"CI_NETRC_USERNAME",
"CI_NETRC_PASSWORD",
"CI_SCRIPT",
"HOME",
"SHELL",
}
type local struct {
// TODO: make cmd a cmd list to itterate over, the hard part is to have a common ReadCloser
cmd *exec.Cmd
output io.ReadCloser
workingdir string
@@ -65,7 +78,8 @@ func (e *local) Exec(ctx context.Context, step *types.Step) error {
// Get environment variables
env := os.Environ()
for a, b := range step.Environment {
if a != "HOME" && a != "SHELL" { // Don't override $HOME and $SHELL
// append allowed env vars to command env
if !slices.Contains(notAllowedEnvVarOverwrites, a) {
env = append(env, a+"="+b)
}
}
@@ -73,6 +87,8 @@ func (e *local) Exec(ctx context.Context, step *types.Step) error {
var command []string
if step.Image == constant.DefaultCloneImage {
// Default clone step
// TODO: creat tmp HOME and insert netrc
// TODO: download plugin-git binary if not exist
env = append(env, "CI_WORKSPACE="+e.workingdir+"/"+step.Environment["CI_REPO"])
command = append(command, "plugin-git")
} else {
@@ -81,10 +97,14 @@ func (e *local) Exec(ctx context.Context, step *types.Step) error {
command = append(command, "-c")
// TODO: use commands directly
script, _ := base64.StdEncoding.DecodeString(common.GenerateScript(step.Commands))
scriptStr := string(script)
script := ""
for _, cmd := range step.Commands {
script += fmt.Sprintf("echo + %s\n%s\n\n", shellescape.Quote(cmd), cmd)
}
script = strings.TrimSpace(script)
// Deleting the initial lines removes netrc support but adds compatibility for more shells like fish
command = append(command, scriptStr[strings.Index(scriptStr, "\n\n")+2:])
command = append(command, script)
}
// Prepare command