-
Notifications
You must be signed in to change notification settings - Fork 208
Script run stage #4720
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
Script run stage #4720
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e6118ce
Add option script run stage
ffjlabo 9351080
Implement Executor for script run
ffjlabo 2594710
Use StageStatus_STAGE_FAILURE
ffjlabo 57c694e
Add error log
ffjlabo 5525832
Add Copyright
ffjlabo 92c6dd9
Re-implement script run stage like custom sync
ffjlabo 2dab4a3
Delete comment
ffjlabo b198ecc
Merge branch 'master' into script-run-stage
ffjlabo 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,68 @@ | ||
| package scriptrun | ||
|
|
||
| import ( | ||
| "os" | ||
| "os/exec" | ||
|
|
||
| "github.com/pipe-cd/pipecd/pkg/app/piped/executor" | ||
| "github.com/pipe-cd/pipecd/pkg/model" | ||
| ) | ||
|
|
||
| type registerer interface { | ||
| Register(stage model.Stage, f executor.Factory) error | ||
| RegisterRollback(kind model.RollbackKind, f executor.Factory) error | ||
| } | ||
|
|
||
| type Executor struct { | ||
| executor.Input | ||
| } | ||
|
|
||
| func (e *Executor) Execute(sig executor.StopSignal) model.StageStatus { | ||
| e.LogPersister.Infof("Start executing the script run stage") | ||
|
|
||
| opts := e.Input.StageConfig.ScriptRunStageOptions | ||
| if opts == nil { | ||
| e.LogPersister.Infof("option for script run stage not found") | ||
| return model.StageStatus_STAGE_FAILURE | ||
| } | ||
|
|
||
| if opts.Run == "" { | ||
| return model.StageStatus_STAGE_SUCCESS | ||
| } | ||
|
|
||
| envs := make([]string, 0, len(opts.Env)) | ||
| for key, value := range opts.Env { | ||
| envs = append(envs, key+"="+value) | ||
| } | ||
|
|
||
| cmd := exec.Command("/bin/sh", "-l", "-c", opts.Run) | ||
| cmd.Env = append(os.Environ(), envs...) | ||
| cmd.Stdout = e.LogPersister | ||
| cmd.Stderr = e.LogPersister | ||
|
|
||
| e.LogPersister.Infof("executing script:") | ||
| e.LogPersister.Infof(opts.Run) | ||
|
|
||
| if err := cmd.Run(); err != nil { | ||
khanhtc1202 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return model.StageStatus_STAGE_FAILURE | ||
| } | ||
| return model.StageStatus_STAGE_SUCCESS | ||
| } | ||
|
|
||
| type RollbackExecutor struct { | ||
| executor.Input | ||
| } | ||
|
|
||
| func (e *RollbackExecutor) Execute(sig executor.StopSignal) model.StageStatus { | ||
| e.LogPersister.Infof("Unimplement: rollbacking the script run stage") | ||
| return model.StageStatus_STAGE_FAILURE | ||
| } | ||
|
|
||
| // Register registers this executor factory into a given registerer. | ||
| func Register(r registerer) { | ||
| r.Register(model.StageScriptRun, func(in executor.Input) executor.Executor { | ||
| return &Executor{ | ||
| Input: in, | ||
| } | ||
| }) | ||
| } | ||
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 |
|---|---|---|
|
|
@@ -702,3 +702,33 @@ func TestCustomSyncConfig(t *testing.T) { | |
| }) | ||
| } | ||
| } | ||
|
|
||
| // TODO: Add testcases for other kinds of applications. | ||
|
||
| func TestScriptSycConfiguration(t *testing.T) { | ||
| testcases := []struct { | ||
| name string | ||
| opts ScriptRunStageOptions | ||
| wantErr bool | ||
| }{ | ||
| { | ||
| name: "valid", | ||
| opts: ScriptRunStageOptions{ | ||
| Run: "echo 'hello world'", | ||
| }, | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "invalid", | ||
| opts: ScriptRunStageOptions{ | ||
| Run: "", | ||
| }, | ||
| wantErr: true, | ||
| }, | ||
| } | ||
| for _, tc := range testcases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| err := tc.opts.Validate() | ||
| assert.Equal(t, tc.wantErr, err != 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
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.