-
Notifications
You must be signed in to change notification settings - Fork 1
Add post lifecycle hook #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2bc65a1
Use correct job conclusion values for expression evluator
sebastianrath 88656f7
Implement post-step exec for gh action nodes
sebastianrath fdbc711
Fix CodeQL warning
sebastianrath e1d25d6
Missing job conclusion
sebastianrath eb48bbb
Bump tests
sebastianrath 879272a
Bump test-action-node
sebastianrath ba01f08
Fix CodeQL warning
sebastianrath File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,197 @@ | ||
| package core | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "strings" | ||
| "sync" | ||
|
|
||
| "github.com/actionforge/actrun-cli/utils" | ||
| ) | ||
|
|
||
| // PostStepRunner is implemented by node types that support post-step execution. | ||
| type PostStepRunner interface { | ||
| RunPost(c *ExecutionState, env map[string]string) error | ||
| } | ||
|
|
||
| // PostStep holds the information needed to execute a post step. | ||
| type PostStep struct { | ||
| ActionName string | ||
| NodeID string | ||
| PostIf string | ||
| Runner PostStepRunner | ||
| StateFilePath string | ||
| EnvSnapshot map[string]string | ||
| } | ||
|
|
||
| // PostStepQueue is a thread-safe queue for post steps. | ||
| type PostStepQueue struct { | ||
| mu sync.Mutex | ||
| steps []PostStep | ||
| } | ||
|
|
||
| // NewPostStepQueue creates a new empty PostStepQueue. | ||
| func NewPostStepQueue() *PostStepQueue { | ||
| return &PostStepQueue{} | ||
| } | ||
|
|
||
| // Register adds a post step to the queue. | ||
| func (q *PostStepQueue) Register(step PostStep) { | ||
| q.mu.Lock() | ||
| defer q.mu.Unlock() | ||
| q.steps = append(q.steps, step) | ||
| } | ||
|
|
||
| // Len returns the number of registered post steps. | ||
| func (q *PostStepQueue) Len() int { | ||
| q.mu.Lock() | ||
| defer q.mu.Unlock() | ||
| return len(q.steps) | ||
| } | ||
|
|
||
| // DrainLIFO returns all post steps in LIFO order and clears the queue. | ||
| func (q *PostStepQueue) DrainLIFO() []PostStep { | ||
| q.mu.Lock() | ||
| defer q.mu.Unlock() | ||
|
|
||
| result := make([]PostStep, len(q.steps)) | ||
| for i, step := range q.steps { | ||
| result[len(q.steps)-1-i] = step | ||
| } | ||
| q.steps = nil | ||
| return result | ||
| } | ||
|
|
||
| // executePostSteps runs post steps in order, logging errors but continuing. | ||
| func executePostSteps(c *ExecutionState, steps []PostStep) { | ||
| for _, step := range steps { | ||
| utils.LogOut.Infof("Running post step: %s (%s)\n", step.ActionName, step.NodeID) | ||
|
|
||
| if !evaluatePostIf(c, step) { | ||
| utils.LogOut.Infof("Post step skipped (post-if condition not met): %s\n", step.ActionName) | ||
| continue | ||
| } | ||
|
|
||
| env := step.EnvSnapshot | ||
| if step.StateFilePath != "" { | ||
| injectStateVars(env, step.StateFilePath) | ||
| } | ||
|
|
||
| if err := step.Runner.RunPost(c, env); err != nil { | ||
| utils.LogErr.Errorf("Post step failed: %s: %v\n", step.ActionName, err) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // evaluatePostIf evaluates the post-if condition for a post step. | ||
| // Returns true if the step should run. | ||
| func evaluatePostIf(c *ExecutionState, step PostStep) bool { | ||
| condition := step.PostIf | ||
| if condition == "" { | ||
| // Default: always() | ||
| return true | ||
| } | ||
|
|
||
| // Wrap in ${{ }} if not already wrapped | ||
| if !strings.Contains(condition, "${{") { | ||
| condition = "${{ " + condition + " }}" | ||
| } | ||
|
|
||
| evaluator := NewEvaluator(c) | ||
| result, err := evaluator.Evaluate(condition) | ||
| if err != nil { | ||
| utils.LogErr.Errorf("Failed to evaluate post-if condition for %s: %v\n", step.ActionName, err) | ||
| return false | ||
| } | ||
|
|
||
| return isTruthy(result) | ||
| } | ||
|
|
||
| // injectStateVars reads the GITHUB_STATE file and injects STATE_* env vars. | ||
| func injectStateVars(env map[string]string, stateFilePath string) { | ||
| if stateFilePath == "" { | ||
| return | ||
| } | ||
|
|
||
| stateVars, err := ParseKeyValueFile(stateFilePath) | ||
| if err != nil { | ||
| utils.LogErr.Errorf("Failed to read state file %s: %v\n", stateFilePath, err) | ||
| return | ||
| } | ||
|
|
||
| for key, value := range stateVars { | ||
| env[fmt.Sprintf("STATE_%s", key)] = value | ||
| } | ||
| } | ||
|
|
||
| // ParseKeyValueFile parses a GitHub Actions file command output file. | ||
| // Supports both NAME=VALUE and NAME<<DELIMITER heredoc styles. | ||
| func ParseKeyValueFile(filePath string) (map[string]string, error) { | ||
| cleanPath, err := utils.ValidatePath(filePath) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| b, err := os.ReadFile(cleanPath) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return ParseKeyValueString(string(b)) | ||
| } | ||
|
|
||
| // ParseKeyValueString parses a GitHub Actions key-value string. | ||
| // Supports both NAME=VALUE and NAME<<DELIMITER heredoc styles. | ||
| func ParseKeyValueString(input string) (map[string]string, error) { | ||
| results := make(map[string]string) | ||
| lines := strings.Split(input, "\n") | ||
|
|
||
| for i := 0; i < len(lines); i++ { | ||
| line := lines[i] | ||
| if line == "" { | ||
| continue | ||
| } | ||
|
|
||
| var key, value string | ||
| equalsIndex := strings.Index(line, "=") | ||
| heredocIndex := strings.Index(line, "<<") | ||
|
|
||
| // Normal style: NAME=VALUE | ||
| if equalsIndex >= 0 && (heredocIndex < 0 || equalsIndex < heredocIndex) { | ||
| parts := strings.SplitN(line, "=", 2) | ||
| if len(parts) != 2 || parts[0] == "" { | ||
| return nil, CreateErr(nil, nil, "invalid format '%s'. Name must not be empty", line) | ||
| } | ||
| key, value = parts[0], parts[1] | ||
| } else if heredocIndex >= 0 && (equalsIndex < 0 || heredocIndex < equalsIndex) { | ||
| // Heredoc style: NAME<<EOF | ||
| parts := strings.SplitN(line, "<<", 2) | ||
| if len(parts) != 2 || parts[0] == "" || parts[1] == "" { | ||
| return nil, CreateErr(nil, nil, "invalid format '%s'. Name must not be empty", line) | ||
| } | ||
| key = parts[0] | ||
| delimiter := strings.TrimRight(parts[1], " \t\n\r") | ||
|
|
||
| var heredocValue strings.Builder | ||
| for i++; i < len(lines); i++ { | ||
| if strings.TrimRight(lines[i], " \t\n\r") == delimiter { | ||
| break | ||
| } | ||
| heredocValue.WriteString(lines[i]) | ||
| if i < len(lines)-1 { | ||
| heredocValue.WriteString("\n") | ||
| } | ||
| } | ||
| if i >= len(lines) { | ||
| return nil, CreateErr(nil, nil, "invalid value. Matching delimiter not found '%s'", delimiter) | ||
| } | ||
| value = heredocValue.String() | ||
| } else { | ||
| return nil, CreateErr(nil, nil, "invalid format '%s'", line) | ||
| } | ||
|
|
||
| results[key] = value | ||
| } | ||
|
|
||
| return results, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.