func (b *BashShell) writeCloneCmd(w io.Writer, build *common.Build, projectDir string) { b.echoColoredFormat(w, "Cloning repository...") io.WriteString(w, fmt.Sprintf("rm -rf %s\n", projectDir)) io.WriteString(w, fmt.Sprintf("mkdir -p %s\n", projectDir)) io.WriteString(w, fmt.Sprintf("git clone %s %s\n", helpers.ShellEscape(build.RepoURL), projectDir)) io.WriteString(w, fmt.Sprintf("cd %s\n", projectDir)) }
func (s *Command) Run() error { if s.client == nil { return errors.New("Not connected") } session, err := s.client.NewSession() if err != nil { return err } var envVariables bytes.Buffer for _, keyValue := range s.Environment { envVariables.WriteString("export " + helpers.ShellEscape(keyValue) + "\n") } session.Stdin = io.MultiReader( &envVariables, bytes.NewBuffer(s.Stdin), ) session.Stdout = s.Stdout session.Stderr = s.Stderr err = session.Run(s.Command) session.Close() return err }
func (b *BashShell) finalize(script string) string { var buffer bytes.Buffer w := bufio.NewWriter(&buffer) io.WriteString(w, "#!/usr/bin/env bash\n\n") io.WriteString(w, "set -eo pipefail\n") io.WriteString(w, ": | eval "+helpers.ShellEscape(script)+"\n") w.Flush() return buffer.String() }
func (b *BashShell) executeCommand(w io.Writer, cmd string, arguments ...string) { list := []string{ helpers.ShellEscape(cmd), } for _, argument := range arguments { list = append(list, strconv.Quote(argument)) } io.WriteString(w, strings.Join(list, " ")+"\n") }
func (b *BashShell) writeFetchCmd(w io.Writer, build *common.Build, projectDir string, gitDir string) { io.WriteString(w, fmt.Sprintf("if [[ -d %s ]]; then\n", gitDir)) b.echoColoredFormat(w, "Fetching changes...") io.WriteString(w, fmt.Sprintf("cd %s\n", projectDir)) io.WriteString(w, fmt.Sprintf("git clean -fdx\n")) io.WriteString(w, fmt.Sprintf("git reset --hard > /dev/null\n")) io.WriteString(w, fmt.Sprintf("git remote set-url origin %s\n", helpers.ShellEscape(build.RepoURL))) io.WriteString(w, fmt.Sprintf("git fetch origin\n")) io.WriteString(w, fmt.Sprintf("else\n")) b.writeCloneCmd(w, build, projectDir) io.WriteString(w, fmt.Sprintf("fi\n")) }
func (b *PowerShell) GenerateScript(info common.ShellScriptInfo) (*common.ShellScript, error) { var buffer bytes.Buffer w := bufio.NewWriter(&buffer) build := info.Build projectDir := build.FullProjectDir() projectDir = helpers.ToBackslash(projectDir) b.writeCommand(w, "$ErrorActionPreference = \"Stop\"") if len(build.Hostname) != 0 { b.writeCommand(w, "echo \"Running on $env:computername via %s...\"", helpers.ShellEscape(build.Hostname)) } else { b.writeCommand(w, "echo \"Running on $env:computername...\"") } b.writeCommand(w, "") if build.AllowGitFetch { b.writeFetchCmd(w, build, projectDir) } else { b.writeCloneCmd(w, build, projectDir) } b.writeCheckoutCmd(w, build) b.writeCommand(w, "") for _, command := range strings.Split(build.Commands, "\n") { command = strings.TrimRight(command, " \t\r\n") if strings.TrimSpace(command) == "" { b.writeCommand(w, "echo \"\"") continue } if !helpers.BoolOrDefault(build.Runner.DisableVerbose, false) { b.writeCommand(w, "echo \"%s\"", strings.Replace(command, "\"", "`\"", -1)) } b.writeCommandChecked(w, "%s", command) } w.Flush() script := common.ShellScript{ Environment: b.GetVariables(build, projectDir, info.Environment), BuildScript: buffer.String(), Command: "powershell", Arguments: []string{"-noprofile", "-noninteractive", "-executionpolicy", "Bypass", "-command"}, PassFile: true, Extension: "ps1", } return &script, nil }
func (b *CmdShell) GenerateScript(info common.ShellScriptInfo) (*common.ShellScript, error) { var buffer bytes.Buffer w := bufio.NewWriter(&buffer) build := info.Build projectDir := build.FullProjectDir() projectDir = helpers.ToBackslash(projectDir) b.writeCommand(w, "@echo off") b.writeCommand(w, "echo.") b.writeCommand(w, "setlocal enableextensions") if len(build.Hostname) != 0 { b.writeCommand(w, "echo Running on %s via %s...", "%COMPUTERNAME%", helpers.ShellEscape(build.Hostname)) } else { b.writeCommand(w, "echo Running on %s...", "%COMPUTERNAME%") } if build.AllowGitFetch { b.writeFetchCmd(w, build, projectDir) } else { b.writeCloneCmd(w, build, projectDir) } b.writeCheckoutCmd(w, build) for _, command := range strings.Split(build.Commands, "\n") { command = strings.TrimRight(command, " \t\r\n") if strings.TrimSpace(command) == "" { b.writeCommand(w, "echo.") continue } if !helpers.BoolOrDefault(build.Runner.DisableVerbose, false) { b.writeCommand(w, "echo %s", command) } b.writeCommandChecked(w, "%s", command) } w.Flush() script := common.ShellScript{ Environment: b.GetVariables(build, projectDir, info.Environment), Script: buffer.String(), Command: "cmd", Arguments: []string{"/Q", "/C"}, PassFile: true, Extension: "cmd", } return &script, nil }
func (b *BashShell) echoWarning(w io.Writer, text string) { coloredText := helpers.ANSI_BOLD_YELLOW + text + helpers.ANSI_RESET b.executeCommandFormat(w, "echo %s", helpers.ShellEscape(coloredText)) }
func (b *BashShell) writeExport(w io.Writer, variable common.BuildVariable) { b.executeCommandFormat(w, "export %s=%s", helpers.ShellEscape(variable.Key), helpers.ShellEscape(variable.Value)) }
func (b *PowerShell) GenerateScript(build *common.Build, shellType common.ShellType) (*common.ShellScript, error) { var buffer bytes.Buffer w := bufio.NewWriter(&buffer) projectDir := build.FullProjectDir() projectDir = helpers.ToBackslash(projectDir) b.writeCommand(w, "$ErrorActionPreference = \"Stop\"") if len(build.Hostname) != 0 { b.writeCommand(w, "echo \"Running on $env:computername via %s...\"", helpers.ShellEscape(build.Hostname)) } else { b.writeCommand(w, "echo \"Running on $env:computername...\"") } b.writeCommand(w, "") if build.AllowGitFetch { b.writeFetchCmd(w, build, projectDir) } else { b.writeCloneCmd(w, build, projectDir) } b.writeCheckoutCmd(w, build) b.writeCommand(w, "") for _, command := range strings.Split(build.Commands, "\n") { command = strings.TrimRight(command, " \t\r\n") if strings.TrimSpace(command) == "" { b.writeCommand(w, "echo \"\"") continue } if !helpers.BoolOrDefault(build.Runner.DisableVerbose, false) { b.writeCommand(w, "echo \"%s\"", command) } b.writeCommandChecked(w, "%s", command) } w.Flush() env := []string{ fmt.Sprintf("CI_BUILD_REF=%s", build.Sha), fmt.Sprintf("CI_BUILD_BEFORE_SHA=%s", build.BeforeSha), fmt.Sprintf("CI_BUILD_REF_NAME=%s", build.RefName), fmt.Sprintf("CI_BUILD_ID=%d", build.ID), fmt.Sprintf("CI_BUILD_REPO=%s", build.RepoURL), fmt.Sprintf("CI_PROJECT_ID=%d", build.ProjectID), fmt.Sprintf("CI_PROJECT_DIR=%s", projectDir), "CI=true", "CI_SERVER=yes", "CI_SERVER_NAME=GitLab CI", "CI_SERVER_VERSION=", "CI_SERVER_REVISION=", "GITLAB_CI=true", } script := common.ShellScript{ Environment: env, Script: buffer.Bytes(), Command: "powershell", Arguments: []string{"-noprofile", "-noninteractive", "-executionpolicy", "Bypass", "-command"}, PassFile: true, Extension: "ps1", } return &script, nil }
func (b *BashShell) GenerateScript(build *common.Build, shellType common.ShellType) (*common.ShellScript, error) { var buffer bytes.Buffer w := bufio.NewWriter(&buffer) io.WriteString(w, "#!/usr/bin/env bash\n") io.WriteString(w, "\n") if len(build.Hostname) != 0 { io.WriteString(w, fmt.Sprintf("echo Running on $(hostname) via %s...\n", helpers.ShellEscape(build.Hostname))) } else { io.WriteString(w, "echo Running on $(hostname)...\n") } io.WriteString(w, "\n") io.WriteString(w, "set -eo pipefail\n") io.WriteString(w, "\n") if build.AllowGitFetch { b.writeFetchCmd(w, build) } else { b.writeCloneCmd(w, build) } b.writeCheckoutCmd(w, build) io.WriteString(w, "\n") if !helpers.BoolOrDefault(build.Runner.DisableVerbose, false) { io.WriteString(w, "set -v\n") io.WriteString(w, "\n") } commands := build.Commands commands = strings.Replace(commands, "\r\n", "\n", -1) io.WriteString(w, commands) w.Flush() env := []string{ fmt.Sprintf("CI_BUILD_REF=%s", build.Sha), fmt.Sprintf("CI_BUILD_BEFORE_SHA=%s", build.BeforeSha), fmt.Sprintf("CI_BUILD_REF_NAME=%s", build.RefName), fmt.Sprintf("CI_BUILD_ID=%d", build.ID), fmt.Sprintf("CI_BUILD_REPO=%s", build.RepoURL), fmt.Sprintf("CI_PROJECT_ID=%d", build.ProjectID), fmt.Sprintf("CI_PROJECT_DIR=%s", build.FullProjectDir()), "CI_SERVER=yes", "CI_SERVER_NAME=GitLab CI", "CI_SERVER_VERSION=", "CI_SERVER_REVISION=", } script := common.ShellScript{ Environment: env, Script: buffer.Bytes(), Command: "bash", } if shellType == common.LoginShell { script.Arguments = []string{"--login"} } return &script, nil }
func (b *CmdShell) GenerateScript(build *common.Build, shellType common.ShellType) (*common.ShellScript, error) { var buffer bytes.Buffer w := bufio.NewWriter(&buffer) projectDir := build.FullProjectDir() projectDir = helpers.ToBackslash(projectDir) b.writeCommand(w, "@echo off") b.writeCommand(w, "echo.") b.writeCommand(w, "setlocal enableextensions") if len(build.Hostname) != 0 { b.writeCommand(w, "echo Running on %s via %s...", "%COMPUTERNAME%", helpers.ShellEscape(build.Hostname)) } else { b.writeCommand(w, "echo Running on %s...", "%COMPUTERNAME%") } if build.AllowGitFetch { b.writeFetchCmd(w, build, projectDir) } else { b.writeCloneCmd(w, build, projectDir) } b.writeCheckoutCmd(w, build) for _, command := range strings.Split(build.Commands, "\n") { command = strings.TrimRight(command, " \t\r\n") if strings.TrimSpace(command) == "" { b.writeCommand(w, "echo.") continue } if !helpers.BoolOrDefault(build.Runner.DisableVerbose, false) { b.writeCommand(w, "echo %s", command) } b.writeCommandChecked(w, "%s", command) } w.Flush() env := []string{ fmt.Sprintf("CI_BUILD_REF=%s", build.Sha), fmt.Sprintf("CI_BUILD_BEFORE_SHA=%s", build.BeforeSha), fmt.Sprintf("CI_BUILD_REF_NAME=%s", build.RefName), fmt.Sprintf("CI_BUILD_ID=%d", build.ID), fmt.Sprintf("CI_BUILD_REPO=%s", build.RepoURL), fmt.Sprintf("CI_PROJECT_ID=%d", build.ProjectID), fmt.Sprintf("CI_PROJECT_DIR=%s", projectDir), "CI=true", "CI_SERVER=yes", "CI_SERVER_NAME=GitLab CI", "CI_SERVER_VERSION=", "CI_SERVER_REVISION=", "GITLAB_CI=true", } script := common.ShellScript{ Environment: env, Script: buffer.Bytes(), Command: "cmd", Arguments: []string{"/Q", "/C"}, PassFile: true, Extension: "cmd", } return &script, nil }
func (b *BashShell) GenerateScript(info common.ShellScriptInfo) (*common.ShellScript, error) { var buffer bytes.Buffer w := bufio.NewWriter(&buffer) build := info.Build projectDir := build.FullProjectDir() projectDir = helpers.ToSlash(projectDir) gitDir := filepath.Join(projectDir, ".git") projectScript := helpers.ShellEscape(build.FullProjectDir() + ".sh") io.WriteString(w, "#!/usr/bin/env bash\n") io.WriteString(w, "\n") 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, info.Environment) { io.WriteString(w, "export "+helpers.ShellEscape(keyValue)+"\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 build.AllowGitFetch { b.writeFetchCmd(w, build, helpers.ShellEscape(projectDir), helpers.ShellEscape(gitDir)) } else { b.writeCloneCmd(w, build, helpers.ShellEscape(projectDir)) } b.writeCheckoutCmd(w, build) io.WriteString(w, "\n") io.WriteString(w, "echo\n") io.WriteString(w, "\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 != "" { b.echoColored(w, "$ "+command) } else { io.WriteString(w, "echo\n") } } io.WriteString(w, command+"\n") } io.WriteString(w, "\n") w.Flush() script := common.ShellScript{ Script: buffer.String(), Environment: b.GetVariables(build, projectDir, info.Environment), } // su if info.User != nil { script.Command = "su" if info.Type == common.LoginShell { script.Arguments = []string{"--shell", "/bin/bash", "--login", *info.User} } else { script.Arguments = []string{"--shell", "/bin/bash", *info.User} } } else { script.Command = "bash" if info.Type == common.LoginShell { script.Arguments = []string{"--login"} } } return &script, nil }
func (b *BashShell) echoColored(w io.Writer, text string) { coloredText := helpers.ANSI_BOLD_GREEN + text + helpers.ANSI_RESET io.WriteString(w, "echo "+helpers.ShellEscape(coloredText)+"\n") }