Skip to content

Commit 1ee94a3

Browse files
committed
refactor: pass environment to pipeline execution
1 parent 21af882 commit 1ee94a3

7 files changed

Lines changed: 45 additions & 35 deletions

File tree

cmd/flags/flags.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ type GlobalFlags struct {
1515
Namespace string
1616
KubeContext string
1717
Profiles []string
18-
ProfileRefresh bool
1918
DisableProfileActivation bool
2019
SwitchContext bool
2120
ConfigPath string
@@ -33,7 +32,6 @@ func (gf *GlobalFlags) ToConfigOptions() *loader.ConfigOptions {
3332
return &loader.ConfigOptions{
3433
OverrideName: gf.OverrideName,
3534
Profiles: profiles,
36-
ProfileRefresh: gf.ProfileRefresh,
3735
DisableProfileActivation: gf.DisableProfileActivation,
3836
Vars: gf.Vars,
3937
}
@@ -52,7 +50,6 @@ func SetGlobalFlags(flags *flag.FlagSet) *GlobalFlags {
5250
flags.BoolVar(&globalFlags.Silent, "silent", false, "Run in silent mode and prevents any devspace log output except panics & fatals")
5351

5452
flags.StringSliceVarP(&globalFlags.Profiles, "profile", "p", []string{}, "The DevSpace profiles to apply. Multiple profiles are applied in the order they are specified")
55-
flags.BoolVar(&globalFlags.ProfileRefresh, "profile-refresh", false, "If true will pull and re-download profile parent sources")
5653
flags.BoolVar(&globalFlags.DisableProfileActivation, "disable-profile-activation", false, "If true will ignore all profile activations")
5754
flags.BoolVarP(&globalFlags.SwitchContext, "switch-context", "s", false, "Switches and uses the last kube context and namespace that was used to deploy the DevSpace project")
5855
flags.StringVarP(&globalFlags.Namespace, "namespace", "n", "", "The kubernetes namespace to use")

pkg/devspace/config/versions/latest/schema.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,28 @@ type Pipeline struct {
143143
Run string `yaml:"run,omitempty" json:"run,omitempty"`
144144
}
145145

146+
func (p *Pipeline) UnmarshalYAML(unmarshal func(interface{}) error) error {
147+
pipelineString := ""
148+
err := unmarshal(&pipelineString)
149+
if err != nil {
150+
m := map[string]interface{}{}
151+
err := unmarshal(m)
152+
if err != nil {
153+
return err
154+
}
155+
156+
out, err := json.Marshal(m)
157+
if err != nil {
158+
return err
159+
}
160+
161+
return json.Unmarshal(out, p)
162+
}
163+
164+
p.Run = pipelineString
165+
return nil
166+
}
167+
146168
type RequireConfig struct {
147169
// DevSpace specifies the DevSpace version constraint that is needed to use this config
148170
DevSpace string `yaml:"devspace,omitempty" json:"devspace,omitempty"`

pkg/devspace/kubectl/util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ func EnsureNamespace(ctx context.Context, client Client, namespace string, log l
168168
return err
169169
}
170170

171-
log.Donef("Created namespace: %s", namespace)
171+
log.WithPrefixColor("info ", "cyan+b").Donef("Created namespace: %s", namespace)
172172
}
173173

174174
return nil

pkg/devspace/pipeline/engine/pipelinehandler/commands/run_pipelines.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ import (
77
devspacecontext "github.com/loft-sh/devspace/pkg/devspace/context"
88
"github.com/loft-sh/devspace/pkg/devspace/pipeline/types"
99
"github.com/pkg/errors"
10+
"mvdan.cc/sh/v3/expand"
1011
"strings"
1112
)
1213

1314
type RunPipelineOptions struct {
1415
types.PipelineOptions
1516
}
1617

17-
func RunPipelines(ctx devspacecontext.Context, pipeline types.Pipeline, args []string) error {
18+
func RunPipelines(ctx devspacecontext.Context, pipeline types.Pipeline, args []string, environ expand.Environ) error {
1819
ctx.Log().Debugf("run_pipelines %s", strings.Join(args, " "))
1920
options := &RunPipelineOptions{}
2021
args, err := flags.ParseArgs(options, args)
@@ -39,5 +40,6 @@ func RunPipelines(ctx devspacecontext.Context, pipeline types.Pipeline, args []s
3940
return fmt.Errorf("no pipeline to run specified")
4041
}
4142

43+
options.Environ = environ
4244
return pipeline.StartNewPipelines(ctx, pipelines, options.PipelineOptions)
4345
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ import (
1010
"github.com/loft-sh/devspace/pkg/util/scanner"
1111
"github.com/loft-sh/devspace/pkg/util/tomb"
1212
"io"
13+
"mvdan.cc/sh/v3/expand"
1314
"os"
1415
"sync"
1516
)
1617

1718
type Job struct {
1819
Pipeline types.Pipeline
1920
Config *latest.Pipeline
20-
ExtraEnv map[string]string
2121

2222
m sync.Mutex
2323
t *tomb.Tomb
@@ -47,7 +47,7 @@ func (j *Job) Stop() error {
4747
return t.Wait()
4848
}
4949

50-
func (j *Job) Run(ctx devspacecontext.Context) error {
50+
func (j *Job) Run(ctx devspacecontext.Context, environ expand.Environ) error {
5151
if ctx.IsDone() {
5252
return ctx.Context().Err()
5353
}
@@ -64,7 +64,7 @@ func (j *Job) Run(ctx devspacecontext.Context) error {
6464
t.Go(func() error {
6565
// start the actual job
6666
done := t.NotifyGo(func() error {
67-
return j.execute(ctx, t)
67+
return j.execute(ctx, t, environ)
6868
})
6969

7070
// wait until job is dying
@@ -85,7 +85,7 @@ func (j *Job) Run(ctx devspacecontext.Context) error {
8585
return t.Wait()
8686
}
8787

88-
func (j *Job) execute(ctx devspacecontext.Context, parent *tomb.Tomb) error {
88+
func (j *Job) execute(ctx devspacecontext.Context, parent *tomb.Tomb, environ expand.Environ) error {
8989
ctx = ctx.WithLogger(ctx.Log())
9090
stdoutReader, stdoutWriter := io.Pipe()
9191
defer stdoutWriter.Close()
@@ -110,6 +110,6 @@ func (j *Job) execute(ctx devspacecontext.Context, parent *tomb.Tomb) error {
110110
})
111111

112112
handler := pipelinehandler.NewPipelineExecHandler(ctx, stdoutWriter, stderrWriter, j.Pipeline)
113-
_, err := engine.ExecutePipelineShellCommand(ctx.Context(), j.Config.Run, os.Args[1:], ctx.WorkingDir(), j.Config.ContinueOnError, stdoutWriter, stderrWriter, os.Stdin, ctx.Environ(), handler)
113+
_, err := engine.ExecutePipelineShellCommand(ctx.Context(), j.Config.Run, os.Args[1:], ctx.WorkingDir(), j.Config.ContinueOnError, stdoutWriter, stderrWriter, os.Stdin, environ, handler)
114114
return err
115115
}

pkg/devspace/pipeline/pipeline.go

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/loft-sh/devspace/pkg/devspace/context/values"
77
"github.com/loft-sh/devspace/pkg/devspace/kubectl"
88
utilerrors "k8s.io/apimachinery/pkg/util/errors"
9+
"mvdan.cc/sh/v3/expand"
910
"os"
1011
"strings"
1112
"sync"
@@ -167,7 +168,7 @@ func (p *pipeline) Dependencies() map[string]types.Pipeline {
167168
}
168169

169170
func (p *pipeline) Run(ctx devspacecontext.Context) error {
170-
return p.executeJob(ctx, p.main)
171+
return p.executeJob(ctx, p.main, ctx.Environ())
171172
}
172173

173174
func (p *pipeline) StartNewDependencies(ctx devspacecontext.Context, dependencies []types2.Dependency, options types.DependencyOptions) error {
@@ -193,12 +194,14 @@ func (p *pipeline) StartNewDependencies(ctx devspacecontext.Context, dependencie
193194
continue
194195
} else if !deployableDependencies[dependency.Name()] {
195196
ctx.Log().Infof("Skipping dependency %s as it was either already deployed or is currently in use by another DevSpace instance in the same namespace", dependency.Name())
197+
196198
continue
197199
}
198200

199201
deployDependencies = append(deployDependencies, dependency)
200202
}
201203

204+
// Start sequentially
202205
if options.Sequential {
203206
for _, dependency := range deployDependencies {
204207
err := p.startNewDependency(ctx, dependency, options)
@@ -307,37 +310,22 @@ func (p *pipeline) StartNewPipelines(ctx devspacecontext.Context, pipelines []*l
307310
}
308311

309312
func (p *pipeline) startNewPipeline(ctx devspacecontext.Context, configPipeline *latest.Pipeline, id string, options types.PipelineOptions) error {
310-
// parse env
311-
envMap := map[string]string{}
312-
for _, s := range options.Env {
313-
if s == "" {
314-
continue
315-
}
316-
317-
splitted := strings.Split(s, "=")
318-
if len(splitted) <= 1 {
319-
return fmt.Errorf("invalid environment variable format. Has to be KEY=VALUE")
320-
}
321-
322-
envMap[splitted[0]] = strings.Join(splitted[1:], "=")
323-
}
324-
325313
// exchange job if it's not alive anymore
326-
j, err := p.createJob(configPipeline, envMap, id)
314+
j, err := p.createJob(configPipeline, id)
327315
if err != nil {
328316
return err
329317
}
330318
defer p.removeJob(j, id)
331319

332-
err = p.executeJob(ctx, j)
320+
err = p.executeJob(ctx, j, options.Environ)
333321
if err != nil {
334322
return err
335323
}
336324

337325
return nil
338326
}
339327

340-
func (p *pipeline) createJob(configPipeline *latest.Pipeline, envMap map[string]string, id string) (job *Job, err error) {
328+
func (p *pipeline) createJob(configPipeline *latest.Pipeline, id string) (job *Job, err error) {
341329
p.m.Lock()
342330
defer p.m.Unlock()
343331

@@ -349,7 +337,6 @@ func (p *pipeline) createJob(configPipeline *latest.Pipeline, envMap map[string]
349337
j = &Job{
350338
Pipeline: p,
351339
Config: configPipeline,
352-
ExtraEnv: envMap,
353340
}
354341
p.jobs[id] = j
355342
return j, nil
@@ -367,13 +354,13 @@ func (p *pipeline) removeJob(j *Job, id string) {
367354
}
368355
}
369356

370-
func (p *pipeline) executeJob(ctx devspacecontext.Context, j *Job) error {
357+
func (p *pipeline) executeJob(ctx devspacecontext.Context, j *Job, environ expand.Environ) error {
371358
// don't start jobs on a cancelled context
372359
if ctx.IsDone() {
373360
return nil
374361
}
375362

376-
err := j.Run(ctx)
363+
err := j.Run(ctx, environ)
377364
if err != nil {
378365
return err
379366
}

pkg/devspace/pipeline/types/pipeline.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
types2 "github.com/loft-sh/devspace/pkg/devspace/dependency/types"
99
"github.com/loft-sh/devspace/pkg/devspace/deploy"
1010
"github.com/loft-sh/devspace/pkg/devspace/devpod"
11+
"mvdan.cc/sh/v3/expand"
1112
)
1213

1314
type Options struct {
@@ -27,9 +28,10 @@ type DependencyOptions struct {
2728

2829
// PipelineOptions describe how pipelines should be run
2930
type PipelineOptions struct {
30-
Env []string `long:"env" description:"Pass the following environment variable to the pipelines"`
31-
Background bool `long:"background" description:"Run the pipeline in the background"`
32-
Sequential bool `long:"sequential" description:"Run pipelines one after another"`
31+
Background bool `long:"background" description:"Run the pipeline in the background"`
32+
Sequential bool `long:"sequential" description:"Run pipelines one after another"`
33+
34+
Environ expand.Environ
3335
}
3436

3537
type Pipeline interface {

0 commit comments

Comments
 (0)