Add step name as label to docker containers (#3137)

and add a test
This commit is contained in:
6543 2024-01-09 06:01:34 +01:00 committed by GitHub
parent c64340cf8f
commit 12df59d0ec
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 5 deletions

View file

@ -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)

View file

@ -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)
}