Skip to content
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

Add default envs in ScriptRunStage: SR_TRIGGERED_COMMANDER, SR_IS_ROLLBACK #5464

Merged
merged 8 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,12 @@ You can use the envrionment values related to the deployment.
|SR_APPLICATION_NAME| The application name | example |
|SR_TRIGGERED_AT| The timestamp when the deployment is triggered | 1719571113 |
|SR_TRIGGERED_COMMIT_HASH| The commit hash that triggered the deployment | 2bf969a3dad043aaf8ae6419943255e49377da0d |
|SR_TRIGGERED_COMMANDER| The ID of user who triggered the deployment via UI. This is APIKey's ID if it was triggered via `pipectl sync`. This is empty if it was triggered by your piped. | userid |
|SR_REPOSITORY_URL| The repository url configured in the piped config | git@github.com:org/repo.git, https://github.com/org/repo |
|SR_SUMMARY| The summary of the deployment | Sync with the specified pipeline because piped received a command from user via web console or pipectl|
|SR_CONTEXT_RAW| The json encoded string of above values | {"deploymentID":"877625fc-196a-40f9-b6a9-99decd5494a0","applicationID":"8d7609e0-9ff6-4dc7-a5ac-39660768606a","applicationName":"example","triggeredAt":1719571113,"triggeredCommitHash":"2bf969a3dad043aaf8ae6419943255e49377da0d","repositoryURL":"git@github.com:org/repo.git","labels":{"env":"example","team":"product"}} |
|SR_LABELS_XXX| The label attached to the deployment. The env name depends on the label name. For example, if a deployment has the labels `env:prd` and `team:server`, `SR_LABELS_ENV` and `SR_LABELS_TEAM` are registered. | prd, server |
|SR_IS_ROLLBACK| This is `true` if the deployment is rollbacking. Otherwise, this is `false`. | false |

### Use `SR_CONTEXT_RAW` with jq

Expand Down
2 changes: 1 addition & 1 deletion pkg/app/piped/executor/kubernetes/rollback.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@
}
}

ci := scriptrun.NewContextInfo(e.Deployment)
ci := scriptrun.NewContextInfo(e.Deployment, true)

Check warning on line 217 in pkg/app/piped/executor/kubernetes/rollback.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/piped/executor/kubernetes/rollback.go#L217

Added line #L217 was not covered by tests
ciEnv, err := ci.BuildEnv()
if err != nil {
e.LogPersister.Errorf("failed to build srcipt run context info: %w", err)
Expand Down
6 changes: 5 additions & 1 deletion pkg/app/piped/executor/scriptrun/script_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,25 @@ func Test_ContextInfo_BuildEnv(t *testing.T) {
ApplicationName: "application-name",
TriggeredAt: 1234567890,
TriggeredCommitHash: "commit-hash",
TriggeredCommander: "commander",
RepositoryURL: "repo-url",
Labels: map[string]string{
"key1": "value1",
"key2": "value2",
},
Summary: "summary",
IsRollback: false,
Summary: "summary",
},
want: map[string]string{
"SR_DEPLOYMENT_ID": "deployment-id",
"SR_APPLICATION_ID": "application-id",
"SR_APPLICATION_NAME": "application-name",
"SR_TRIGGERED_AT": "1234567890",
"SR_TRIGGERED_COMMIT_HASH": "commit-hash",
"SR_TRIGGERED_COMMANDER": "commander",
"SR_REPOSITORY_URL": "repo-url",
"SR_SUMMARY": "summary",
"SR_IS_ROLLBACK": "false",
"SR_LABELS_KEY1": "value1",
"SR_LABELS_KEY2": "value2",
},
Expand Down
10 changes: 8 additions & 2 deletions pkg/app/piped/executor/scriptrun/scriptrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
}
}

ci := NewContextInfo(e.Deployment)
ci := NewContextInfo(e.Deployment, false)
ciEnv, err := ci.BuildEnv()
if err != nil {
e.LogPersister.Errorf("failed to build srcipt run context info: %w", err)
Expand Down Expand Up @@ -136,22 +136,26 @@
ApplicationName string `json:"applicationName,omitempty"`
TriggeredAt int64 `json:"triggeredAt,omitempty"`
TriggeredCommitHash string `json:"triggeredCommitHash,omitempty"`
TriggeredCommander string `json:"triggeredCommander,omitempty"`
RepositoryURL string `json:"repositoryURL,omitempty"`
Summary string `json:"summary,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
IsRollback bool `json:"isRollback,omitempty"`
}

// NewContextInfo creates a new ContextInfo from the given deployment.
func NewContextInfo(d *model.Deployment) *ContextInfo {
func NewContextInfo(d *model.Deployment, isRollback bool) *ContextInfo {
return &ContextInfo{
DeploymentID: d.Id,
ApplicationID: d.ApplicationId,
ApplicationName: d.ApplicationName,
TriggeredAt: d.Trigger.Timestamp,
TriggeredCommitHash: d.Trigger.Commit.Hash,
TriggeredCommander: d.Trigger.Commander,

Check warning on line 154 in pkg/app/piped/executor/scriptrun/scriptrun.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/piped/executor/scriptrun/scriptrun.go#L154

Added line #L154 was not covered by tests
RepositoryURL: d.GitPath.Repo.Remote,
Summary: d.Summary,
Labels: d.Labels,
IsRollback: isRollback,

Check warning on line 158 in pkg/app/piped/executor/scriptrun/scriptrun.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/piped/executor/scriptrun/scriptrun.go#L158

Added line #L158 was not covered by tests
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed to use isRollback flag instead of d.Status.

That's because d.Status is ALWAYS PLANNED since scheduler does not update it...

}
}

Expand All @@ -168,8 +172,10 @@
"SR_APPLICATION_NAME": src.ApplicationName,
"SR_TRIGGERED_AT": strconv.FormatInt(src.TriggeredAt, 10),
"SR_TRIGGERED_COMMIT_HASH": src.TriggeredCommitHash,
"SR_TRIGGERED_COMMANDER": src.TriggeredCommander,
"SR_REPOSITORY_URL": src.RepositoryURL,
"SR_SUMMARY": src.Summary,
"SR_IS_ROLLBACK": strconv.FormatBool(src.IsRollback),
"SR_CONTEXT_RAW": string(b), // Add the raw json string as an environment variable.
}

Expand Down
Loading