Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions engine/compiler/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/drone-runners/drone-runner-docker/engine"
"github.com/drone-runners/drone-runner-docker/engine/compiler/shell"
"github.com/drone-runners/drone-runner-docker/engine/compiler/shell/powershell"
"github.com/drone-runners/drone-runner-docker/engine/compiler/shell/wincmd"
"github.com/drone-runners/drone-runner-docker/engine/resource"
)

Expand All @@ -17,7 +18,7 @@ func setupScript(src *resource.Step, dst *engine.Step, os string) {
if len(src.Commands) > 0 {
switch os {
case "windows":
setupScriptWindows(src, dst)
setupScriptWindowsCmd(src, dst)
default:
setupScriptPosix(src, dst)
}
Expand All @@ -26,13 +27,32 @@ func setupScript(src *resource.Step, dst *engine.Step, os string) {

// helper function configures the pipeline script for the
// windows operating system.
func setupScriptWindows(src *resource.Step, dst *engine.Step) {
// WIP, BROKEN
func setupScriptWindowsCmd(src *resource.Step, dst *engine.Step) {
dst.Entrypoint = []string{"cmd", "/S", "/c"}
dst.Command = []string{`\drone\drone-build-script.cmd`}
dst.Envs["DRONE_SCRIPT"] = wincmd.Script(src.Commands)
dst.Envs["SHELL"] = "cmd.exe"
}

// helper function configures the pipeline script for the
// windows operating system.
func setupScriptWindowsPowershell(src *resource.Step, dst *engine.Step) {
dst.Entrypoint = []string{"powershell", "-noprofile", "-noninteractive", "-command"}
dst.Command = []string{"echo $Env:DRONE_SCRIPT | iex"}
dst.Envs["DRONE_SCRIPT"] = powershell.Script(src.Commands)
dst.Envs["SHELL"] = "powershell.exe"
}

// helper function configures the pipeline script for the
// windows operating system.
func setupScriptWindowsPwsh(src *resource.Step, dst *engine.Step) {
dst.Entrypoint = []string{"pwsh", "-noprofile", "-noninteractive", "-command"}
dst.Command = []string{"echo $Env:DRONE_SCRIPT | iex"}
dst.Envs["DRONE_SCRIPT"] = powershell.Script(src.Commands)
dst.Envs["SHELL"] = "pwsh.exe"
}

// helper function configures the pipeline script for the
// linux operating system.
func setupScriptPosix(src *resource.Step, dst *engine.Step) {
Expand Down
2 changes: 1 addition & 1 deletion engine/compiler/shell/powershell/powershell.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ if ($Env:DRONE_NETRC_MACHINE) {
machine $Env:DRONE_NETRC_MACHINE
login $Env:DRONE_NETRC_USERNAME
password $Env:DRONE_NETRC_PASSWORD
"@ > (Join-Path $Env:USERPROFILE '_netrc');
"@ > (Join-Path $Env:USERPROFILE '.netrc');
}
[Environment]::SetEnvironmentVariable("DRONE_NETRC_USERNAME", $null);
[Environment]::SetEnvironmentVariable("DRONE_NETRC_PASSWORD", $null);
Expand Down
48 changes: 48 additions & 0 deletions engine/compiler/shell/wincmd/wincmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Package wincmd provides functions for converting shell
// commands to cmd scripts.
package wincmd

import (
"bytes"
)

// Script converts a slice of individual shell commands to
// a cmd script.
func Script(commands []string) string {
buf := new(bytes.Buffer)

// ignore first linebreak
buf.WriteString(optionScript)

for _, command := range commands {
buf.WriteString("echo +--------------------------------\n")
buf.WriteString("echo + " + command + "\n")
buf.WriteString(command)
buf.WriteString(`
set LastErrorLevel=%ErrorLevel%
if %LastErrorLevel% gtr 0 (
echo ERROR: %LastErrorLevel%
exit /b %LastErrorLevel%
)` + "\n")
buf.WriteString("echo.\n")
}

return buf.String()
}

// optionScript is a helper script this is added to the build
// to set shell options, in this case, to exit on error.
const optionScript = `
@echo off
if .%DRONE_NETRC_MACHINE%. neq .. (
type nul > %USERPROFILE%\.netrc
echo machine %DRONE_NETRC_MACHINE% >> %USERPROFILE%\.netrc
echo login %DRONE_NETRC_USERNAME% >> %USERPROFILE%\.netrc
echo password %DRONE_NETRC_PASSWORD% >> %USERPROFILE%\.netrc
)
set DRONE_NETRC_USERNAME=
set DRONE_NETRC_PASSWORD=
set DRONE_NETRC_FILE=
set DRONE_SCRIPT=

`
30 changes: 30 additions & 0 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
package engine

import (
"archive/tar"
"bufio"
"bytes"
"context"
"io"
"io/ioutil"
Expand All @@ -18,6 +21,7 @@ import (
"github.com/drone/runner-go/logger"
"github.com/drone/runner-go/pipeline/runtime"
"github.com/drone/runner-go/registry/auths"
"github.com/sirupsen/logrus"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
Expand Down Expand Up @@ -198,6 +202,32 @@ func (e *Docker) Run(ctx context.Context, specv runtime.Spec, stepv runtime.Step
if err != nil {
return nil, errors.TrimExtraInfo(err)
}

// Copy init script to container only on windows
if spec.Platform.OS == "windows" && step.Envs["SHELL"] == "cmd.exe" {
if step.Name != "clone" {
fileContent := []byte(step.Envs["DRONE_SCRIPT"])

hdr := &tar.Header{
Name: "/drone/drone-build-script.cmd",
Size: int64(len(fileContent)),
}

tarBuf := new(bytes.Buffer)
tw := tar.NewWriter(tarBuf)

tw.WriteHeader(hdr)
io.Copy(tw, bytes.NewReader(fileContent))
tw.Close()

err = e.client.CopyToContainer(ctx, step.ID, "/", bufio.NewReader(tarBuf), types.CopyToContainerOptions{})
if err != nil {
logrus.Errorln(hdr.Name + ": " + err.Error())
} else {
logrus.Debugln("Copied build script: " + hdr.Name)
}
}
}
// start the container
err = e.start(ctx, step.ID)
if err != nil {
Expand Down