From 12df59d0ecbea49628ec70578d511009f23627a4 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Tue, 9 Jan 2024 06:01:34 +0100 Subject: [PATCH] Add step name as label to docker containers (#3137) and add a test --- pipeline/backend/docker/convert.go | 16 +++++++++----- pipeline/backend/docker/convert_test.go | 28 +++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/pipeline/backend/docker/convert.go b/pipeline/backend/docker/convert.go index cfe5d55e5..b7eb3df25 100644 --- a/pipeline/backend/docker/convert.go +++ b/pipeline/backend/docker/convert.go @@ -17,6 +17,7 @@ package docker import ( "encoding/base64" "encoding/json" + "maps" "regexp" "strings" @@ -29,24 +30,29 @@ import ( // returns a container configuration. func (e *docker) toConfig(step *types.Step) *container.Config { config := &container.Config{ - Image: step.Image, - Labels: map[string]string{"wp_uuid": step.UUID}, + Image: step.Image, + Labels: map[string]string{ + "wp_uuid": step.UUID, + "wp_step": step.Name, + }, WorkingDir: step.WorkingDir, AttachStdout: true, AttachStderr: true, } + env := make(map[string]string) + maps.Copy(env, step.Environment) if len(step.Commands) != 0 { env, entry, cmd := common.GenerateContainerConf(step.Commands, e.info.OSType) for k, v := range env { - step.Environment[k] = v + env[k] = v } config.Entrypoint = entry config.Cmd = cmd } - if len(step.Environment) != 0 { - config.Env = toEnv(step.Environment) + if len(env) != 0 { + config.Env = toEnv(env) } if len(step.Volumes) != 0 { config.Volumes = toVol(step.Volumes) diff --git a/pipeline/backend/docker/convert_test.go b/pipeline/backend/docker/convert_test.go index 1da403b95..a9a6c24a3 100644 --- a/pipeline/backend/docker/convert_test.go +++ b/pipeline/backend/docker/convert_test.go @@ -17,6 +17,12 @@ package docker import ( "reflect" "testing" + + "github.com/docker/docker/api/types" + "github.com/docker/docker/api/types/container" + "github.com/stretchr/testify/assert" + + backend "go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/types" ) func TestSplitVolumeParts(t *testing.T) { @@ -85,3 +91,25 @@ func TestSplitVolumeParts(t *testing.T) { } } } + +func TestToConfigSmall(t *testing.T) { + engine := docker{info: types.Info{OSType: "linux/riscv64"}} + + conf := engine.toConfig(&backend.Step{ + Name: "test", + UUID: "09238932", + Commands: []string{"go test"}, + }) + + assert.NotNil(t, conf) + assert.EqualValues(t, &container.Config{ + AttachStdout: true, + AttachStderr: true, + Cmd: []string{"echo $CI_SCRIPT | base64 -d | /bin/sh -e"}, + Entrypoint: []string{"/bin/sh", "-c"}, + Labels: map[string]string{ + "wp_step": "test", + "wp_uuid": "09238932", + }, + }, conf) +}