Skip to content

Commit 0b831b9

Browse files
committed
Improve token and workspace resolution in gh-action node
1 parent 70894d7 commit 0b831b9

File tree

1 file changed

+31
-17
lines changed

1 file changed

+31
-17
lines changed

nodes/gh-action@v1.go

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ func (n *GhActionNode) ExecuteDocker(c *core.ExecutionState, workingDirectory st
360360
}
361361

362362
func init() {
363-
err := core.RegisterNodeFactory(ghActionNodeDefinition, func(ctx any, parent core.NodeBaseInterface, parentId string, nodeDef map[string]any, validate bool) (core.NodeBaseInterface, []error) {
363+
err := core.RegisterNodeFactory(ghActionNodeDefinition, func(ctx any, parent core.NodeBaseInterface, parentId string, nodeDef map[string]any, validate bool, opts core.RunOpts) (core.NodeBaseInterface, []error) {
364364

365365
nodeType := ctx.(string)
366366

@@ -374,36 +374,47 @@ func init() {
374374
return nil, []error{core.CreateErr(nil, err, "unable to get user home directory")}
375375
}
376376

377-
// repoRoot is where the git repository is stored locall
377+
// actionRepoRoot is where the git repository is stored locall
378378
// ~/work/_actions/{owner}/{repo}/{ref}
379-
repoRoot := filepath.Join(home, "work", "_actions", owner, repo, ref)
379+
actionRepoRoot := filepath.Join(home, "work", "_actions", owner, repo, ref)
380380

381381
// actionDir is where the action.yml lives of the action which is not always the repo root it seems
382382
// If the action is in the root, path is empty
383383
// If the action is in a subdir like "github.com/owner/repo/sub/path", path is just "sub/path"
384-
actionDir := filepath.Join(repoRoot, path)
384+
actionDir := filepath.Join(actionRepoRoot, path)
385385

386-
_, ok := os.LookupEnv("GITHUB_ACTIONS")
387-
if !ok {
388-
return nil, []error{core.CreateErr(nil, nil, "environment not configured yet to run GitHub Actions.").SetHint(
389-
"In order to run GitHub Actions, please follow the instructions at https://docs.actionforge.dev/reference/github-actions/#configure"),
390-
}
386+
isGitHubAction := opts.OverrideEnv["GITHUB_ACTIONS"] == "true" || os.Getenv("GITHUB_ACTIONS") == "true"
387+
if !isGitHubAction {
388+
return nil, []error{core.CreateErr(nil, nil, "node representing GitHub Action '%v' can only be used in a GitHub Actions workflow.", nodeType)}
391389
}
392390

393-
// Reminder that INPUT_* env vars are only prefixed for the graph execution, not here
394-
ghToken := os.Getenv("INPUT_TOKEN")
391+
// Reminder:
392+
// `INPUT_TOKEN` comes from the GitHub Action actionforge/action.
393+
// `GITHUB_TOKEN` is manually provided, eg through the web app and has a higher precedence.
394+
// GITHUB_TOKEN should always be set via secrets, but just in case the user provides it via env, check also there
395+
ghToken := opts.OverrideSecrets["GITHUB_TOKEN"]
396+
if ghToken == "" {
397+
ghToken = opts.OverrideEnv["GITHUB_TOKEN"]
398+
if ghToken == "" {
399+
ghToken = os.Getenv("GITHUB_TOKEN")
400+
if ghToken == "" {
401+
// Note that `INPUT_*` env vars are only prefixed for the graph execution, not here
402+
ghToken = os.Getenv("INPUT_TOKEN")
403+
}
404+
}
405+
}
395406

396407
// TODO: (Seb) for the validation process we only need the action.yml, not the entire repo
397408
// so check if we are in validate mode and only download the action.yml file
398-
_, err = os.Stat(repoRoot)
409+
_, err = os.Stat(actionRepoRoot)
399410
if errors.Is(err, os.ErrNotExist) {
400411
if ghToken == "" {
401-
return nil, []error{core.CreateErr(nil, nil, "INPUT_TOKEN not set")}
412+
return nil, []error{core.CreateErr(nil, nil, "neither GITHUB_TOKEN nor INPUT_TOKEN are set")}
402413
}
403414

404415
cloneUrl := fmt.Sprintf("https://github.com/%s/%s", owner, repo)
405416

406-
if err := os.MkdirAll(filepath.Dir(repoRoot), 0755); err != nil {
417+
if err := os.MkdirAll(filepath.Dir(actionRepoRoot), 0755); err != nil {
407418
return nil, []error{core.CreateErr(nil, err, "unable to create action directory")}
408419
}
409420

@@ -419,7 +430,7 @@ func init() {
419430
Password: ghToken,
420431
}
421432

422-
clonedRepo, err := git.PlainClone(repoRoot, false, cloneOpts)
433+
clonedRepo, err := git.PlainClone(actionRepoRoot, false, cloneOpts)
423434
if err != nil {
424435
return nil, []error{core.CreateErr(nil, err, "failed to clone repository")}
425436
}
@@ -448,7 +459,7 @@ func init() {
448459
}
449460
} else {
450461
// reset in just case something tampered with the cached gh actions
451-
existingRepo, err := git.PlainOpen(repoRoot)
462+
existingRepo, err := git.PlainOpen(actionRepoRoot)
452463
if err != nil {
453464
return nil, []error{core.CreateErr(nil, err, "failed to open cached repository")}
454465
}
@@ -501,7 +512,10 @@ func init() {
501512

502513
switch action.Runs.Using {
503514
case "docker":
504-
sysWorkspaceDir := os.Getenv("GITHUB_WORKSPACE")
515+
sysWorkspaceDir := opts.OverrideEnv["GITHUB_WORKSPACE"]
516+
if sysWorkspaceDir == "" {
517+
sysWorkspaceDir = os.Getenv("GITHUB_WORKSPACE")
518+
}
505519
if sysWorkspaceDir == "" {
506520
return nil, []error{core.CreateErr(nil, nil, "GITHUB_WORKSPACE not set")}
507521
}

0 commit comments

Comments
 (0)