Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom steps entrypoint #2985

Merged
merged 18 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from 12 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
4 changes: 4 additions & 0 deletions docs/docs/20-usage/20-workflow-syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ docker run --entrypoint=build.sh golang

> Please note that only build steps can define commands. You cannot use commands with plugins or services.

### `entrypoint`

Allows you to specify the entrypoint for containers. Note that this must be a list of the command and its arguments (e.g. `["/bin/sh", "-c"]`).

### `environment`

Woodpecker provides the ability to pass environment variables to individual steps.
Expand Down
6 changes: 3 additions & 3 deletions pipeline/backend/common/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ import (
"encoding/base64"
)

func GenerateContainerConf(commands []string, goos string) (env map[string]string, entry, cmd []string) {
func GenerateContainerConf(commands []string, goos string) (env map[string]string, entry []string, cmd string) {
env = make(map[string]string)
if goos == "windows" {
env["CI_SCRIPT"] = base64.StdEncoding.EncodeToString([]byte(generateScriptWindows(commands)))
env["HOME"] = "c:\\root"
env["SHELL"] = "powershell.exe"
entry = []string{"powershell", "-noprofile", "-noninteractive", "-command"}
cmd = []string{"[System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($Env:CI_SCRIPT)) | iex"}
cmd = "[System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($Env:CI_SCRIPT)) | iex"
} else {
env["CI_SCRIPT"] = base64.StdEncoding.EncodeToString([]byte(generateScriptPosix(commands)))
env["HOME"] = "/root"
env["SHELL"] = "/bin/sh"
entry = []string{"/bin/sh", "-c"}
cmd = []string{"echo $CI_SCRIPT | base64 -d | /bin/sh -e"}
cmd = "echo $CI_SCRIPT | base64 -d | /bin/sh -e"
}

return env, entry, cmd
Expand Down
5 changes: 4 additions & 1 deletion pipeline/backend/docker/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@ func (e *docker) toConfig(step *types.Step) *container.Config {
for k, v := range env {
step.Environment[k] = v
}
if len(step.Entrypoint) > 0 {
entry = step.Entrypoint
}
config.Entrypoint = entry
config.Cmd = cmd
config.Cmd = []string{cmd}
}

if len(step.Environment) != 0 {
Expand Down
17 changes: 10 additions & 7 deletions pipeline/backend/kubernetes/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const (

func mkPod(namespace, name, image, workDir, goos, serviceAccountName string,
pool, privileged bool,
commands, vols, pullSecretNames []string,
commands, vols, pullSecretNames, entrypoint []string,
labels, annotations, env, nodeSelector map[string]string,
extraHosts []types.HostAlias, tolerations []types.Toleration, resources types.Resources,
securityContext *types.SecurityContext, securityContextConfig SecurityContextConfig,
Expand All @@ -51,7 +51,7 @@ func mkPod(namespace, name, image, workDir, goos, serviceAccountName string,
return nil, err
}

container, err := podContainer(name, image, workDir, goos, pool, privileged, commands, vols, env,
container, err := podContainer(name, image, workDir, goos, pool, privileged, commands, vols, entrypoint, env,
resources, securityContext)
if err != nil {
return nil, err
Expand Down Expand Up @@ -109,7 +109,7 @@ func podSpec(serviceAccountName string, vols, pullSecretNames []string, env, bac
return spec, nil
}

func podContainer(name, image, workDir, goos string, pull, privileged bool, commands, volumes []string, env map[string]string, resources types.Resources,
func podContainer(name, image, workDir, goos string, pull, privileged bool, commands, volumes, entrypoint []string, env map[string]string, resources types.Resources,
securityContext *types.SecurityContext,
) (v1.Container, error) {
var err error
Expand All @@ -124,9 +124,12 @@ func podContainer(name, image, workDir, goos string, pull, privileged bool, comm
}

if len(commands) != 0 {
scriptEnv, command, args := common.GenerateContainerConf(commands, goos)
container.Command = command
container.Args = args
scriptEnv, entry, cmd := common.GenerateContainerConf(commands, goos)
if len(entrypoint) > 0 {
entry = entrypoint
}
container.Command = entry
container.Args = []string{cmd}
maps.Copy(env, scriptEnv)
}

Expand Down Expand Up @@ -374,7 +377,7 @@ func startPod(ctx context.Context, engine *kube, step *types.Step) (*v1.Pod, err

pod, err := mkPod(engine.config.Namespace, podName, step.Image, step.WorkingDir, engine.goos, step.BackendOptions.Kubernetes.ServiceAccountName,
step.Pull, step.Privileged,
step.Commands, step.Volumes, engine.config.ImagePullSecretNames,
step.Commands, step.Volumes, engine.config.ImagePullSecretNames, step.Entrypoint,
engine.config.PodLabels, engine.config.PodAnnotations, step.Environment, step.BackendOptions.Kubernetes.NodeSelector,
step.ExtraHosts, step.BackendOptions.Kubernetes.Tolerations, step.BackendOptions.Kubernetes.Resources, step.BackendOptions.Kubernetes.SecurityContext, engine.config.SecurityContext)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion pipeline/backend/kubernetes/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"github.com/kinbiko/jsonassert"
"github.com/stretchr/testify/assert"

"go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/types"
)

Expand Down Expand Up @@ -244,7 +245,7 @@ func TestFullPod(t *testing.T) {
}
pod, err := mkPod("woodpecker", "wp-01he8bebctabr3kgk0qj36d2me-0", "meltwater/drone-cache", "/woodpecker/src", "linux/amd64", "wp-svc-acc",
true, true,
[]string{"go get", "go test"}, []string{"woodpecker-cache:/woodpecker/src/cache"}, []string{"regcred", "another-pull-secret"},
[]string{"go get", "go test"}, []string{"woodpecker-cache:/woodpecker/src/cache"}, []string{"regcred", "another-pull-secret"}, []string{"/bin/sh", "-c"},
map[string]string{"app": "test"}, map[string]string{"apparmor.security": "runtime/default"}, map[string]string{"CGO": "0"}, map[string]string{"storage": "ssd"},
hostAliases, []types.Toleration{{Key: "net-port", Value: "100Mbit", Effect: types.TaintEffectNoSchedule}},
types.Resources{Requests: map[string]string{"memory": "128Mi", "cpu": "1000m"}, Limits: map[string]string{"memory": "256Mi", "cpu": "2"}},
Expand Down
1 change: 1 addition & 0 deletions pipeline/backend/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ func (e *local) StartStep(ctx context.Context, step *types.Step, taskUUID string
// execCommands use step.Image as shell and run the commands in it
func (e *local) execCommands(ctx context.Context, step *types.Step, state *workflowState, env []string) error {
// Prepare commands
// TODO support `entrypoint` from pipeline config
args, err := e.genCmdByShell(step.Image, step.Commands)
if err != nil {
return fmt.Errorf("could not convert commands into args: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion pipeline/frontend/metadata/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

package metadata

// Event types corresponding to scm hooks.
// Event types corresponding to forge hooks.
const (
EventPush = "push"
EventPull = "pull_request"
Expand Down
1 change: 1 addition & 0 deletions pipeline/frontend/yaml/compiler/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ func (c *Compiler) createProcess(name string, container *yaml_types.Container, s
WorkingDir: workingdir,
Environment: environment,
Commands: container.Commands,
Entrypoint: container.Entrypoint,
ExtraHosts: extraHosts,
Volumes: volumes,
Tmpfs: container.Tmpfs,
Expand Down
5 changes: 3 additions & 2 deletions pipeline/frontend/yaml/types/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type (
Container struct {
BackendOptions BackendOptions `yaml:"backend_options,omitempty"`
Commands base.StringOrSlice `yaml:"commands,omitempty"`
Entrypoint base.StringOrSlice `yaml:"entrypoint,omitempty"`
Detached bool `yaml:"detach,omitempty"`
Directory string `yaml:"directory,omitempty"`
Environment base.SliceOrMap `yaml:"environment,omitempty"`
Expand All @@ -50,7 +51,7 @@ type (
Ports []base.StringOrInt `yaml:"ports,omitempty"`
DependsOn base.StringOrSlice `yaml:"depends_on,omitempty"`

// Docker Specific
// Docker and Kubernetes Specific
Privileged bool `yaml:"privileged,omitempty"`

// Undocumented
Expand Down Expand Up @@ -121,7 +122,7 @@ func (c *ContainerList) UnmarshalYAML(value *yaml.Node) error {
}

func (c *Container) IsPlugin() bool {
return len(c.Commands) == 0
return len(c.Commands) == 0 && len(c.Entrypoint) == 0
qwerty287 marked this conversation as resolved.
Show resolved Hide resolved
}

func (c *Container) IsTrustedCloneImage() bool {
Expand Down
2 changes: 2 additions & 0 deletions pipeline/frontend/yaml/types/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ devices:
directory: example/
dns: 8.8.8.8
dns_search: example.com
entrypoint: [/bin/sh, -c]
environment:
- RACK_ENV=development
- SHOW=true
Expand Down Expand Up @@ -84,6 +85,7 @@ func TestUnmarshalContainer(t *testing.T) {
Directory: "example/",
DNS: base.StringOrSlice{"8.8.8.8"},
DNSSearch: base.StringOrSlice{"example.com"},
Entrypoint: []string{"/bin/sh", "-c"},
Environment: base.SliceOrMap{"RACK_ENV": "development", "SHOW": "true"},
ExtraHosts: []string{"somehost:162.242.195.82", "otherhost:50.31.209.229", "ipv6:2001:db8::10"},
Image: "golang:latest",
Expand Down