From 602bf8ca44023dbbdd15a15d91a30d45473145a4 Mon Sep 17 00:00:00 2001 From: Tetsuya KIKUCHI <97105818+t-kikuc@users.noreply.github.com> Date: Mon, 6 Jan 2025 09:29:33 +0900 Subject: [PATCH] Add default envs in ScriptRunStage: SR_TRIGGERED_COMMANDER, SR_IS_ROLLBACK (#5464) * Add TriggeredCommandar Signed-off-by: t-kikuc * add isRollback Signed-off-by: t-kikuc * update docs Signed-off-by: t-kikuc * fix name->id Signed-off-by: t-kikuc * fix env name Signed-off-by: t-kikuc * fix: specify isRollback since deployment.Status is always Planned Signed-off-by: t-kikuc * fix typo Signed-off-by: t-kikuc * Add case of `pipectl sync` Signed-off-by: t-kikuc --------- Signed-off-by: t-kikuc --- .../customizing-deployment/script-run.md | 2 ++ pkg/app/piped/executor/kubernetes/rollback.go | 2 +- pkg/app/piped/executor/scriptrun/script_run_test.go | 6 +++++- pkg/app/piped/executor/scriptrun/scriptrun.go | 10 ++++++++-- 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/docs/content/en/docs-dev/user-guide/managing-application/customizing-deployment/script-run.md b/docs/content/en/docs-dev/user-guide/managing-application/customizing-deployment/script-run.md index dd4ba0544f..69f0e20485 100644 --- a/docs/content/en/docs-dev/user-guide/managing-application/customizing-deployment/script-run.md +++ b/docs/content/en/docs-dev/user-guide/managing-application/customizing-deployment/script-run.md @@ -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 diff --git a/pkg/app/piped/executor/kubernetes/rollback.go b/pkg/app/piped/executor/kubernetes/rollback.go index 8a4164dd2e..5317a4058a 100644 --- a/pkg/app/piped/executor/kubernetes/rollback.go +++ b/pkg/app/piped/executor/kubernetes/rollback.go @@ -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) diff --git a/pkg/app/piped/executor/scriptrun/script_run_test.go b/pkg/app/piped/executor/scriptrun/script_run_test.go index 842b252a1d..187a5cd32b 100644 --- a/pkg/app/piped/executor/scriptrun/script_run_test.go +++ b/pkg/app/piped/executor/scriptrun/script_run_test.go @@ -36,12 +36,14 @@ 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", @@ -49,8 +51,10 @@ func Test_ContextInfo_BuildEnv(t *testing.T) { "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", }, diff --git a/pkg/app/piped/executor/scriptrun/scriptrun.go b/pkg/app/piped/executor/scriptrun/scriptrun.go index 34cb3a46ea..75742368d7 100644 --- a/pkg/app/piped/executor/scriptrun/scriptrun.go +++ b/pkg/app/piped/executor/scriptrun/scriptrun.go @@ -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) @@ -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, } } @@ -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. }