Skip to content

Commit 5a8fae5

Browse files
authored
allow for extra environment variables to be passed to updater image (#492)
1 parent 503d98b commit 5a8fae5

File tree

5 files changed

+53
-44
lines changed

5 files changed

+53
-44
lines changed

cmd/dependabot/internal/cmd/root.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,19 @@ import (
1212
)
1313

1414
type SharedFlags struct {
15-
file string
16-
cache string
17-
debugging bool
18-
flamegraph bool
19-
proxyCertPath string
20-
collectorConfigPath string
21-
extraHosts []string
22-
output string
23-
pullImages bool
24-
volumes []string
25-
timeout time.Duration
26-
local string
15+
file string
16+
cache string
17+
debugging bool
18+
flamegraph bool
19+
proxyCertPath string
20+
collectorConfigPath string
21+
extraHosts []string
22+
output string
23+
pullImages bool
24+
volumes []string
25+
timeout time.Duration
26+
local string
27+
updaterEnvironmentVariables []string
2728
}
2829

2930
// root flags

cmd/dependabot/internal/cmd/test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ func NewTestCommand() *cobra.Command {
7474
cmd.Flags().StringArrayVarP(&flags.volumes, "volume", "v", nil, "mount volumes in Docker")
7575
cmd.Flags().StringArrayVar(&flags.extraHosts, "extra-hosts", nil, "Docker extra hosts setting on the proxy")
7676
cmd.Flags().DurationVarP(&flags.timeout, "timeout", "t", 0, "max time to run an update")
77+
cmd.Flags().StringArrayVarP(&flags.updaterEnvironmentVariables, "updater-env", "e", nil, "additional environment variables to set in the update container")
7778

7879
return cmd
7980
}

cmd/dependabot/internal/cmd/update.go

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,20 @@ import (
66
"encoding/json"
77
"errors"
88
"fmt"
9-
"github.com/MakeNowJust/heredoc"
10-
"github.com/dependabot/cli/internal/infra"
11-
"github.com/dependabot/cli/internal/model"
12-
"github.com/dependabot/cli/internal/server"
13-
"github.com/spf13/cobra"
14-
"gopkg.in/yaml.v3"
159
"io"
1610
"log"
1711
"net"
1812
"net/url"
1913
"os"
2014
"regexp"
2115
"strings"
16+
17+
"github.com/MakeNowJust/heredoc"
18+
"github.com/dependabot/cli/internal/infra"
19+
"github.com/dependabot/cli/internal/model"
20+
"github.com/dependabot/cli/internal/server"
21+
"github.com/spf13/cobra"
22+
"gopkg.in/yaml.v3"
2223
)
2324

2425
var updateCmd = NewUpdateCommand()
@@ -84,27 +85,28 @@ func NewUpdateCommand() *cobra.Command {
8485
}
8586

8687
if err := infra.Run(infra.RunParams{
87-
CacheDir: flags.cache,
88-
CollectorConfigPath: flags.collectorConfigPath,
89-
CollectorImage: collectorImage,
90-
Creds: input.Credentials,
91-
Debug: flags.debugging,
92-
Flamegraph: flags.flamegraph,
93-
Expected: nil, // update subcommand doesn't use expectations
94-
ExtraHosts: flags.extraHosts,
95-
InputName: flags.file,
96-
Job: &input.Job,
97-
LocalDir: flags.local,
98-
Output: flags.output,
99-
ProxyCertPath: flags.proxyCertPath,
100-
ProxyImage: proxyImage,
101-
PullImages: flags.pullImages,
102-
StorageImage: storageImage,
103-
Timeout: flags.timeout,
104-
UpdaterImage: updaterImage,
105-
Volumes: flags.volumes,
106-
Writer: writer,
107-
ApiUrl: flags.apiUrl,
88+
CacheDir: flags.cache,
89+
CollectorConfigPath: flags.collectorConfigPath,
90+
CollectorImage: collectorImage,
91+
Creds: input.Credentials,
92+
Debug: flags.debugging,
93+
Flamegraph: flags.flamegraph,
94+
Expected: nil, // update subcommand doesn't use expectations
95+
ExtraHosts: flags.extraHosts,
96+
InputName: flags.file,
97+
Job: &input.Job,
98+
LocalDir: flags.local,
99+
Output: flags.output,
100+
ProxyCertPath: flags.proxyCertPath,
101+
ProxyImage: proxyImage,
102+
PullImages: flags.pullImages,
103+
StorageImage: storageImage,
104+
Timeout: flags.timeout,
105+
UpdaterImage: updaterImage,
106+
Volumes: flags.volumes,
107+
Writer: writer,
108+
ApiUrl: flags.apiUrl,
109+
UpdaterEnvironmentVariables: flags.updaterEnvironmentVariables,
108110
}); err != nil {
109111
if errors.Is(err, context.DeadlineExceeded) {
110112
log.Fatalf("update timed out after %s", flags.timeout)
@@ -137,6 +139,7 @@ func NewUpdateCommand() *cobra.Command {
137139
cmd.Flags().DurationVarP(&flags.timeout, "timeout", "t", 0, "max time to run an update")
138140
cmd.Flags().IntVar(&flags.inputServerPort, "input-port", 0, "port to use for securely passing input to the updater")
139141
cmd.Flags().StringVarP(&flags.apiUrl, "api-url", "a", "", "the api dependabot should connect to.")
142+
cmd.Flags().StringArrayVarP(&flags.updaterEnvironmentVariables, "updater-env", "e", nil, "additional environment variables to set in the update container")
140143

141144
return cmd
142145
}

internal/infra/run.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ type RunParams struct {
7575
InputName string
7676
InputRaw []byte
7777
ApiUrl string
78+
// UpdaterEnvironmentVariables are additional environment variables to set in the update container
79+
UpdaterEnvironmentVariables []string
7880
}
7981

8082
var gitShaRegex = regexp.MustCompile(`^[0-9a-f]{40}$`)
@@ -436,7 +438,7 @@ func runContainers(ctx context.Context, params RunParams) (err error) {
436438
}
437439

438440
if params.Debug {
439-
if err := updater.RunShell(ctx, prox.url, params.ApiUrl, params.Job); err != nil {
441+
if err := updater.RunShell(ctx, prox.url, params.ApiUrl, params.Job, params.UpdaterEnvironmentVariables); err != nil {
440442
return err
441443
}
442444
} else {
@@ -446,7 +448,7 @@ func runContainers(ctx context.Context, params RunParams) (err error) {
446448
}
447449

448450
// Then run the dependabot commands as the dependabot user
449-
env := userEnv(prox.url, params.ApiUrl, params.Job)
451+
env := userEnv(prox.url, params.ApiUrl, params.Job, params.UpdaterEnvironmentVariables)
450452
if params.Flamegraph {
451453
env = append(env, "FLAMEGRAPH=1")
452454
}

internal/infra/updater.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ func mountOptions(v string) (local, remote string, readOnly bool, err error) {
305305
return local, remote, readOnly, nil
306306
}
307307

308-
func userEnv(proxyURL string, apiUrl string, job *model.Job) []string {
308+
func userEnv(proxyURL string, apiUrl string, job *model.Job, additionalEnvVars []string) []string {
309309
envVars := []string{
310310
"GITHUB_ACTIONS=true", // sets exit code when fetch fails
311311
fmt.Sprintf("http_proxy=%s", proxyURL),
@@ -329,18 +329,20 @@ func userEnv(proxyURL string, apiUrl string, job *model.Job) []string {
329329
envVars = append(envVars, fmt.Sprintf("DEPENDABOT_REPO_CONTENTS_PATH=%s", guestRepoDir))
330330
}
331331

332+
envVars = append(envVars, additionalEnvVars...)
333+
332334
return envVars
333335
}
334336

335337
// RunShell executes an interactive shell, blocks until complete.
336-
func (u *Updater) RunShell(ctx context.Context, proxyURL string, apiUrl string, job *model.Job) error {
338+
func (u *Updater) RunShell(ctx context.Context, proxyURL string, apiUrl string, job *model.Job, additionalEnvVars []string) error {
337339
execCreate, err := u.cli.ContainerExecCreate(ctx, u.containerID, container.ExecOptions{
338340
AttachStdin: true,
339341
AttachStdout: true,
340342
AttachStderr: true,
341343
Tty: true,
342344
User: dependabot,
343-
Env: append(userEnv(proxyURL, apiUrl, job), "DEBUG=1"),
345+
Env: append(userEnv(proxyURL, apiUrl, job, additionalEnvVars), "DEBUG=1"),
344346
Cmd: []string{"/bin/bash", "-c", "update-ca-certificates && /bin/bash"},
345347
})
346348
if err != nil {

0 commit comments

Comments
 (0)