Skip to content

Commit 92eec3a

Browse files
Torbjørn Vatnhackercat
Torbjørn Vatn
and
hackercat
authored
$GITHUB_PATH support (#566)
* Regression in the .golangci.yml file * This looks like an even better fix to #451 The previous solution only prevented the `starting container process caused "exec: \"bash\"` error when someone added an "extra" path in the workflow using `::add-path` * Add support for >> $GITHUB_PATH * The newRunCommand has too high cyclomatic complexity * Add "linux/arm64" to new test * The cyclop linter was complaining so I extracted some funcs * Close some readers * Fix typo * fix: add missing composite function * Fix regress from merging * Keep the error messages as is * consolidate with master * Close the tar reader on defer * New way to get ContainerWorkdir * Remove arch from runner test * Separate the UpdateFromEnv and UpdateFromPath Co-authored-by: hackercat <[email protected]>
1 parent 8153dc9 commit 92eec3a

File tree

8 files changed

+223
-124
lines changed

8 files changed

+223
-124
lines changed

.golangci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ run:
44
linters-settings:
55
gocyclo:
66
# minimal code complexity to report, 30 by default (but we recommend 10-20)
7-
mi-complexity: 15
7+
min-complexity: 15
88
gocritic:
99
disabled-checks:
1010
- ifElseChain

cmd/root.go

+1
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ func readEnvs(path string, envs map[string]string) bool {
145145
return false
146146
}
147147

148+
//nolint:gocyclo
148149
func newRunCommand(ctx context.Context, input *Input) func(*cobra.Command, []string) error {
149150
return func(cmd *cobra.Command, args []string) error {
150151
log.Debugf("Loading environment from %s", input.Envfile())

pkg/container/docker_run.go

+33
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ type Container interface {
7070
Start(attach bool) common.Executor
7171
Exec(command []string, env map[string]string) common.Executor
7272
UpdateFromEnv(srcPath string, env *map[string]string) common.Executor
73+
UpdateFromPath(env *map[string]string) common.Executor
7374
Remove() common.Executor
7475
}
7576

@@ -155,6 +156,10 @@ func (cr *containerReference) UpdateFromEnv(srcPath string, env *map[string]stri
155156
return cr.extractEnv(srcPath, env).IfNot(common.Dryrun)
156157
}
157158

159+
func (cr *containerReference) UpdateFromPath(env *map[string]string) common.Executor {
160+
return cr.extractPath(env).IfNot(common.Dryrun)
161+
}
162+
158163
func (cr *containerReference) Exec(command []string, env map[string]string) common.Executor {
159164
return common.NewPipelineExecutor(
160165
cr.connect(),
@@ -342,6 +347,7 @@ func (cr *containerReference) extractEnv(srcPath string, env *map[string]string)
342347
if err != nil {
343348
return nil
344349
}
350+
defer envTar.Close()
345351
reader := tar.NewReader(envTar)
346352
_, err = reader.Next()
347353
if err != nil && err != io.EOF {
@@ -376,6 +382,31 @@ func (cr *containerReference) extractEnv(srcPath string, env *map[string]string)
376382
}
377383
}
378384

385+
func (cr *containerReference) extractPath(env *map[string]string) common.Executor {
386+
localEnv := *env
387+
return func(ctx context.Context) error {
388+
pathTar, _, err := cr.cli.CopyFromContainer(ctx, cr.id, localEnv["GITHUB_PATH"])
389+
if err != nil {
390+
return errors.WithStack(err)
391+
}
392+
defer pathTar.Close()
393+
394+
reader := tar.NewReader(pathTar)
395+
_, err = reader.Next()
396+
if err != nil && err != io.EOF {
397+
return errors.WithStack(err)
398+
}
399+
s := bufio.NewScanner(reader)
400+
for s.Scan() {
401+
line := s.Text()
402+
localEnv["PATH"] = fmt.Sprintf("%s:%s", localEnv["PATH"], line)
403+
}
404+
405+
env = &localEnv
406+
return nil
407+
}
408+
}
409+
379410
func (cr *containerReference) exec(cmd []string, env map[string]string) common.Executor {
380411
return func(ctx context.Context) error {
381412
logger := common.Logger(ctx)
@@ -413,6 +444,8 @@ func (cr *containerReference) exec(cmd []string, env map[string]string) common.E
413444
if err != nil {
414445
return errors.WithStack(err)
415446
}
447+
defer resp.Close()
448+
416449
var outWriter io.Writer
417450
outWriter = cr.input.Stdout
418451
if outWriter == nil {

pkg/model/planner.go

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ type WorkflowFiles struct {
9090
}
9191

9292
// NewWorkflowPlanner will load a specific workflow, all workflows from a directory or all workflows from a directory and its subdirectories
93+
// nolint: gocyclo
9394
func NewWorkflowPlanner(path string, noWorkflowRecurse bool) (WorkflowPlanner, error) {
9495
path, err := filepath.Abs(path)
9596
if err != nil {

pkg/runner/run_context.go

+5
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ func (rc *RunContext) startJobContainer() common.Executor {
154154
Name: "workflow/envs.txt",
155155
Mode: 0644,
156156
Body: "",
157+
}, &container.FileEntry{
158+
Name: "workflow/paths.txt",
159+
Mode: 0644,
160+
Body: "",
157161
}),
158162
)(ctx)
159163
}
@@ -628,6 +632,7 @@ func (rc *RunContext) withGithubEnv(env map[string]string) map[string]string {
628632
github := rc.getGithubContext()
629633
env["CI"] = "true"
630634
env["GITHUB_ENV"] = "/tmp/workflow/envs.txt"
635+
env["GITHUB_PATH"] = "/tmp/workflow/paths.txt"
631636
env["GITHUB_WORKFLOW"] = github.Workflow
632637
env["GITHUB_RUN_ID"] = github.RunID
633638
env["GITHUB_RUN_NUMBER"] = github.RunNumber

pkg/runner/runner_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ func TestRunEvent(t *testing.T) {
107107
{"testdata", "uses-composite", "push", "", platforms, ""},
108108
{"testdata", "issue-597", "push", "", platforms, ""},
109109
{"testdata", "issue-598", "push", "", platforms, ""},
110+
{"testdata", "env-and-path", "push", "", platforms, ""},
110111
// {"testdata", "issue-228", "push", "", platforms, ""}, // TODO [igni]: Remove this once everything passes
111112

112113
// single test for different architecture: linux/arm64

0 commit comments

Comments
 (0)