Skip to content

Commit

Permalink
Add default envs in ScriptRunStage: SR_TRIGGERED_COMMANDER, SR_IS_ROL…
Browse files Browse the repository at this point in the history
…LBACK (#5464)

* Add TriggeredCommandar

Signed-off-by: t-kikuc <[email protected]>

* add isRollback

Signed-off-by: t-kikuc <[email protected]>

* update docs

Signed-off-by: t-kikuc <[email protected]>

* fix name->id

Signed-off-by: t-kikuc <[email protected]>

* fix env name

Signed-off-by: t-kikuc <[email protected]>

* fix: specify isRollback since deployment.Status is always Planned

Signed-off-by: t-kikuc <[email protected]>

* fix typo

Signed-off-by: t-kikuc <[email protected]>

* Add case of `pipectl sync`

Signed-off-by: t-kikuc <[email protected]>

---------

Signed-off-by: t-kikuc <[email protected]>
  • Loading branch information
t-kikuc authored Jan 6, 2025
1 parent 392a62a commit 602bf8c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
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 | [email protected]: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":"[email protected]: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 @@ func (e *rollbackExecutor) ensureScriptRunRollback(ctx context.Context) model.St
}
}

ci := scriptrun.NewContextInfo(e.Deployment)
ci := scriptrun.NewContextInfo(e.Deployment, true)
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 @@ -36,21 +36,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 @@ -102,7 +102,7 @@ func (e *Executor) executeCommand() model.StageStatus {
}
}

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 @@ -137,22 +137,26 @@ type ContextInfo struct {
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,
RepositoryURL: d.GitPath.Repo.Remote,
Summary: d.Summary,
Labels: d.Labels,
IsRollback: isRollback,
}
}

Expand All @@ -169,8 +173,10 @@ func (src *ContextInfo) BuildEnv() (map[string]string, error) {
"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

0 comments on commit 602bf8c

Please sign in to comment.