Skip to content

Commit

Permalink
Add server configuration option to add default set of labels for work…
Browse files Browse the repository at this point in the history
…flows that has no labels specified (#4326)
  • Loading branch information
lafriks authored Nov 14, 2024
1 parent 1c7728f commit 5699d22
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 23 deletions.
5 changes: 5 additions & 0 deletions cmd/server/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ var flags = append([]cli.Flag{
Usage: "The maximum time in minutes you can set in the repo settings before a pipeline gets killed",
Value: 120,
},
&cli.StringSliceFlag{
Sources: cli.EnvVars("WOODPECKER_DEFAULT_WORKFLOW_LABELS"),
Name: "default-workflow-labels",
Usage: "The default label filter to set for workflows that has no label filter set. By default workflows will be allowed to run on any agent, if not specified in the workflow.",
},
&cli.DurationFlag{
Sources: cli.EnvVars("WOODPECKER_SESSION_EXPIRES"),
Name: "session-expires",
Expand Down
11 changes: 11 additions & 0 deletions cmd/server/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,17 @@ func setupEvilGlobals(ctx context.Context, c *cli.Command, s store.Store) (err e
server.Config.Pipeline.DefaultTimeout = c.Int("default-pipeline-timeout")
server.Config.Pipeline.MaxTimeout = c.Int("max-pipeline-timeout")

_labels := c.StringSlice("default-workflow-labels")
labels := make(map[string]string, len(_labels))
for _, v := range _labels {
name, value, ok := strings.Cut(v, "=")
if !ok {
return fmt.Errorf("invalid label filter: %s", v)
}
labels[name] = value
}
server.Config.Pipeline.DefaultWorkflowLabels = labels

// backend options for pipeline compiler
server.Config.Pipeline.Proxy.No = c.String("backend-no-proxy")
server.Config.Pipeline.Proxy.HTTP = c.String("backend-http-proxy")
Expand Down
8 changes: 8 additions & 0 deletions docs/docs/30-administration/10-server-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,14 @@ The default docker image to be used when cloning the repo.

It is also added to the trusted clone plugin list.

### `WOODPECKER_DEFAULT_WORKFLOW_LABELS`

> By default run workflows on any agent if no label conditions are set in workflow definition.
You can specify default label/platform conditions that will be used for agent selection for workflows that does not have labels conditions set.

Example: `platform=linux/amd64,backend=docker`

### `WOODPECKER_DEFAULT_PIPELINE_TIMEOUT`

> 60 (minutes)
Expand Down
1 change: 1 addition & 0 deletions server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ var Config = struct {
Pipeline struct {
AuthenticatePublicRepos bool
DefaultCancelPreviousPipelineEvents []model.WebhookEvent
DefaultWorkflowLabels map[string]string
DefaultClonePlugin string
TrustedClonePlugins []string
Volumes []string
Expand Down
21 changes: 11 additions & 10 deletions server/pipeline/items.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,17 @@ func parsePipeline(forge forge.Forge, store store.Store, currentPipeline *model.
}

b := stepbuilder.StepBuilder{
Repo: repo,
Curr: currentPipeline,
Prev: prev,
Netrc: netrc,
Secs: secs,
Regs: regs,
Envs: envs,
Host: server.Config.Server.Host,
Yamls: yamls,
Forge: forge,
Repo: repo,
Curr: currentPipeline,
Prev: prev,
Netrc: netrc,
Secs: secs,
Regs: regs,
Envs: envs,
Host: server.Config.Server.Host,
Yamls: yamls,
Forge: forge,
DefaultLabels: server.Config.Pipeline.DefaultWorkflowLabels,
ProxyOpts: compiler.ProxyOptions{
NoProxy: server.Config.Pipeline.Proxy.No,
HTTPProxy: server.Config.Pipeline.Proxy.HTTP,
Expand Down
30 changes: 17 additions & 13 deletions server/pipeline/stepbuilder/stepBuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package stepbuilder

import (
"fmt"
"maps"
"path/filepath"
"strings"

Expand All @@ -40,17 +41,18 @@ import (

// StepBuilder Takes the hook data and the yaml and returns in internal data model.
type StepBuilder struct {
Repo *model.Repo
Curr *model.Pipeline
Prev *model.Pipeline
Netrc *model.Netrc
Secs []*model.Secret
Regs []*model.Registry
Host string
Yamls []*forge_types.FileMeta
Envs map[string]string
Forge metadata.ServerForge
ProxyOpts compiler.ProxyOptions
Repo *model.Repo
Curr *model.Pipeline
Prev *model.Pipeline
Netrc *model.Netrc
Secs []*model.Secret
Regs []*model.Registry
Host string
Yamls []*forge_types.FileMeta
Envs map[string]string
Forge metadata.ServerForge
DefaultLabels map[string]string
ProxyOpts compiler.ProxyOptions
}

type Item struct {
Expand Down Expand Up @@ -186,8 +188,10 @@ func (b *StepBuilder) genItemForWorkflow(workflow *model.Workflow, axis matrix.A
DependsOn: parsed.DependsOn,
RunsOn: parsed.RunsOn,
}
if item.Labels == nil {
item.Labels = map[string]string{}
if len(item.Labels) == 0 {
item.Labels = make(map[string]string, len(b.DefaultLabels))
// Set default labels if no labels are defined in the pipeline
maps.Copy(item.Labels, b.DefaultLabels)
}

return item, errorsAndWarnings
Expand Down

0 comments on commit 5699d22

Please sign in to comment.