Commit 6482233b authored by Kamil Trzciński's avatar Kamil Trzciński 🔴

Environment variables are set from bash script instead of passing them to command

parent a0764a4d
......@@ -44,8 +44,9 @@ type GetBuildRequest struct {
}
type BuildVariable struct {
Key string `json:"key"`
Value string `json:"value"`
Key string `json:"key"`
Value string `json:"value"`
Public bool `json:"public"`
}
type BuildOptions map[string]interface{}
......
......@@ -52,7 +52,7 @@ type ShellScriptInfo struct {
Build *Build
Type ShellType
User *string
Environment []string
Environment []BuildVariable
}
type Shell interface {
......
......@@ -11,6 +11,7 @@ import (
"gitlab.com/gitlab-org/gitlab-ci-multi-runner/helpers"
"io"
"path/filepath"
"strings"
)
type ExecutorOptions struct {
......@@ -166,9 +167,28 @@ func (e *AbstractExecutor) Errorln(args ...interface{}) {
}
func (e *AbstractExecutor) generateShellScript() error {
e.Shell.Build = e.Build
e.Shell.Shell = helpers.StringOrDefault(e.Config.Shell, e.Shell.Shell)
shellScript, err := common.GenerateShellScript(e.Shell)
script := &e.Shell
script.Build = e.Build
script.Shell = helpers.StringOrDefault(e.Config.Shell, script.Shell)
// Add config variables
for _, environment := range e.Config.Environment {
keyValue := strings.SplitN(environment, "=", 2)
if len(keyValue) != 2 {
continue
}
variable := common.BuildVariable{
Key: keyValue[0],
Value: keyValue[1],
}
script.Environment = append(script.Environment, variable)
}
// Add secure variables
script.Environment = append(script.Environment, e.Build.Variables...)
// Generate shell script
shellScript, err := common.GenerateShellScript(*script)
if err != nil {
return err
}
......
......@@ -25,18 +25,3 @@ func (s *AbstractShell) GetDefaultVariables(build *Build, projectDir string) []s
"GITLAB_CI=true",
}
}
func (s *AbstractShell) GetBuildVariables(build *Build) []string {
var variables []string
for _, variable := range build.Variables {
variables = append(variables,
fmt.Sprintf("%s=%s", variable.Key, variable.Value))
}
return variables
}
func (s *AbstractShell) GetVariables(build *Build, projectDir string) []string {
variables := s.GetDefaultVariables(build, projectDir)
variables = append(variables, s.GetBuildVariables(build)...)
return variables
}
......@@ -71,26 +71,29 @@ func (b *BashShell) GenerateScript(info common.ShellScriptInfo) (*common.ShellSc
io.WriteString(w, "set -eo pipefail\n")
io.WriteString(w, "\n")
if len(build.Hostname) != 0 {
io.WriteString(w, fmt.Sprintf("echo Running on $(hostname) via %s...", helpers.ShellEscape(build.Hostname)))
} else {
io.WriteString(w, "echo Running on $(hostname)...\n")
}
io.WriteString(w, "\n")
io.WriteString(w, "echo\n")
// Set env variables from build script
for _, keyValue := range b.GetVariables(build, projectDir) {
for _, keyValue := range b.GetDefaultVariables(build, projectDir) {
io.WriteString(w, "export " + helpers.ShellEscape(keyValue) + "\n")
}
for _, keyValue := range info.Environment {
io.WriteString(w, "export " + helpers.ShellEscape(keyValue) + "\n")
command := helpers.ShellEscape(keyValue.Key + "=" + keyValue.Value)
io.WriteString(w, "export " + command + "\n")
}
io.WriteString(w, "\n")
io.WriteString(w, "# save script that is read from to file and execute script file on remote server\n")
io.WriteString(w, fmt.Sprintf("mkdir -p %s\n", helpers.ShellEscape(projectDir)))
io.WriteString(w, fmt.Sprintf("cat > %s; source %s\n", projectScript, projectScript))
io.WriteString(w, "\n")
if len(build.Hostname) != 0 {
io.WriteString(w, fmt.Sprintf("echo Running on $(hostname) via %s...", helpers.ShellEscape(build.Hostname)))
} else {
io.WriteString(w, "echo Running on $(hostname)...\n")
}
io.WriteString(w, "\n")
io.WriteString(w, "echo\n")
if build.AllowGitFetch {
b.writeFetchCmd(w, build, helpers.ShellEscape(projectDir), helpers.ShellEscape(gitDir))
} else {
......@@ -102,7 +105,9 @@ func (b *BashShell) GenerateScript(info common.ShellScriptInfo) (*common.ShellSc
io.WriteString(w, "echo\n")
io.WriteString(w, "\n")
for _, command := range strings.Split(build.Commands, "\n") {
commands := build.Commands
commands = strings.TrimSpace(commands)
for _, command := range strings.Split(commands, "\n") {
command = strings.TrimSpace(command)
if !helpers.BoolOrDefault(build.Runner.DisableVerbose, false) {
if command != "" {
......
......@@ -96,13 +96,17 @@ func (b *CmdShell) GenerateScript(info common.ShellScriptInfo) (*common.ShellScr
w.Flush()
script := common.ShellScript{
Environment: b.GetVariables(build, projectDir),
Environment: b.GetDefaultVariables(build, projectDir),
Script: buffer.String(),
Command: "cmd",
Arguments: []string{"/Q", "/C"},
PassFile: true,
Extension: "cmd",
}
for _, keyValue := range info.Environment {
script.Environment = append(script.Environment, keyValue.Key + "=" + keyValue.Value)
}
return &script, nil
}
......
......@@ -95,13 +95,17 @@ func (b *PowerShell) GenerateScript(info common.ShellScriptInfo) (*common.ShellS
w.Flush()
script := common.ShellScript{
Environment: b.GetVariables(build, projectDir),
Environment: b.GetDefaultVariables(build, projectDir),
Script: buffer.String(),
Command: "powershell",
Arguments: []string{"-noprofile", "-noninteractive", "-executionpolicy", "Bypass", "-command"},
PassFile: true,
Extension: "ps1",
}
for _, keyValue := range info.Environment {
script.Environment = append(script.Environment, keyValue.Key + "=" + keyValue.Value)
}
return &script, nil
}
......
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