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 env for script run #5002

Merged
merged 8 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -55,7 +55,22 @@ The commands run in the directory where this application configuration file exis

![](/images/script-run.png)

# When to rollback
## Default environment values

You can use the envrionment values related to the deployment.

| Name | Description | Example |
|-|-|-|
|SR_DEPLOYMENT_ID| The deployment id | 877625fc-196a-40f9-b6a9-99decd5494a0 |
|SR_APPLICATION_ID| The application id | 8d7609e0-9ff6-4dc7-a5ac-39660768606a |
|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_REPOSITORY_URL| The repository url configured in the piped config | git@github.com:org/repo.git, https://github.com/org/repo |
|SR_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_LABEL_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_LABEL_ENV` and `SR_LABEL_TEAM` are registered. | prd, server |

## Rollback

You can define the command as `onRollback` to execute when to rollback similar to `run`.
Execute the command to rollback SCRIPT_RUN to the point where the deployment was canceled or failed.
Expand Down Expand Up @@ -114,7 +129,7 @@ Then
- If 4 is canceled or fails while running, only SCRIPT_RUN of 3 will be rollbacked.
- If 6 is canceled or fails while running, only SCRIPT_RUNs 3 and 5 will be rollbacked. The order of executing is 3 -> 5.

# Note
## Note
1. You can use `SCRIPT_RUN` stage with only the application kind of `KubernetesApp`. Soon we will implement it. for other application kinds.

2. The public piped image available in PipeCD main repo (ref: [Dockerfile](https://github.com/pipe-cd/pipecd/blob/master/cmd/piped/Dockerfile)) is based on [alpine](https://hub.docker.com/_/alpine/) and only has a few UNIX command available (ref: [piped-base Dockerfile](https://github.com/pipe-cd/pipecd/blob/master/tool/piped-base/Dockerfile)). If you want to use your commands, you can:
Expand Down
14 changes: 13 additions & 1 deletion pkg/app/piped/executor/kubernetes/rollback.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"go.uber.org/zap"

"github.com/pipe-cd/pipecd/pkg/app/piped/executor"
"github.com/pipe-cd/pipecd/pkg/app/piped/executor/scriptrun"
provider "github.com/pipe-cd/pipecd/pkg/app/piped/platformprovider/kubernetes"
"github.com/pipe-cd/pipecd/pkg/model"
)
Expand Down Expand Up @@ -206,7 +207,18 @@
}
}

envs := make([]string, 0, len(env))
ci := scriptrun.NewContextInfo(e.Deployment)
ciEnv, err := ci.BuildEnv()
if err != nil {
e.LogPersister.Errorf("failed to build srcipt run context info: %w", err)
return model.StageStatus_STAGE_FAILURE
}

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

View check run for this annotation

Codecov / codecov/patch

pkg/app/piped/executor/kubernetes/rollback.go#L210-L215

Added lines #L210 - L215 were not covered by tests

envs := make([]string, 0, len(ciEnv)+len(env))
for key, value := range ciEnv {
envs = append(envs, key+"="+value)
}

Check warning on line 220 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-L220

Added lines #L217 - L220 were not covered by tests

for key, value := range env {
envs = append(envs, key+"="+value)
}
Expand Down
64 changes: 63 additions & 1 deletion pkg/app/piped/executor/scriptrun/scriptrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
package scriptrun

import (
"encoding/json"
"os"
"os/exec"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -99,7 +101,18 @@
}
}

envs := make([]string, 0, len(opts.Env))
ci := NewContextInfo(e.Deployment)
ciEnv, err := ci.BuildEnv()
if err != nil {
e.LogPersister.Errorf("failed to build srcipt run context info: %w", err)
return model.StageStatus_STAGE_FAILURE
}

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

View check run for this annotation

Codecov / codecov/patch

pkg/app/piped/executor/scriptrun/scriptrun.go#L104-L109

Added lines #L104 - L109 were not covered by tests

envs := make([]string, 0, len(ciEnv)+len(opts.Env))
for key, value := range ciEnv {
envs = append(envs, key+"="+value)
}

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

View check run for this annotation

Codecov / codecov/patch

pkg/app/piped/executor/scriptrun/scriptrun.go#L111-L114

Added lines #L111 - L114 were not covered by tests

for key, value := range opts.Env {
envs = append(envs, key+"="+value)
}
Expand All @@ -116,6 +129,55 @@
return model.StageStatus_STAGE_SUCCESS
}

// ContextInfo is the information that will be passed to the script run stage.
type ContextInfo struct {
DeploymentID string `json:"deploymentID"`
ApplicationID string `json:"applicationID"`
ApplicationName string `json:"applicationName,omitempty"`
TriggeredAt int64 `json:"triggeredAt"`
TriggeredCommitHash string `json:"triggeredCommitHash"`
RepositoryURL string `json:"repositoryURL"`
Labels map[string]string `json:"labels,omitempty"`
}

// NewContextInfo creates a new ContextInfo from the given deployment.
func NewContextInfo(d *model.Deployment) *ContextInfo {
return &ContextInfo{
DeploymentID: d.Id,
ApplicationID: d.ApplicationId,
ApplicationName: d.ApplicationName,
TriggeredAt: d.Trigger.Timestamp,
TriggeredCommitHash: d.Trigger.Commit.Hash,
RepositoryURL: d.GitPath.Repo.Remote,
Labels: d.Labels,
}

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

View check run for this annotation

Codecov / codecov/patch

pkg/app/piped/executor/scriptrun/scriptrun.go#L144-L153

Added lines #L144 - L153 were not covered by tests
}

// BuildEnv builds the environment variables from the context info.
func (src *ContextInfo) BuildEnv() (map[string]string, error) {
b, err := json.Marshal(src)
if err != nil {
return nil, err
}

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

View check run for this annotation

Codecov / codecov/patch

pkg/app/piped/executor/scriptrun/scriptrun.go#L157-L161

Added lines #L157 - L161 were not covered by tests

envs := map[string]string{
"SR_DEPLOYMENT_ID": src.DeploymentID,
"SR_APPLICATION_ID": src.ApplicationID,
"SR_APPLICATION_NAME": src.ApplicationName,
"SR_TRIGGERED_AT": strconv.FormatInt(src.TriggeredAt, 10),
"SR_TRIGGERED_COMMIT_HASH": src.TriggeredCommitHash,
"SR_REPOSITORY_URL": src.RepositoryURL,
"SR_RAW": string(b), // Add the raw json string as an environment variable.
Copy link
Member

Choose a reason for hiding this comment

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

how about renaming it to SR_CONTEXT_RAW

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, cheged at a9aac93

}

for k, v := range src.Labels {
eName := "SR_LABELS_" + strings.ToUpper(k)
envs[eName] = v
}

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

View check run for this annotation

Codecov / codecov/patch

pkg/app/piped/executor/scriptrun/scriptrun.go#L163-L176

Added lines #L163 - L176 were not covered by tests

return envs, nil

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L178 was not covered by tests
}

type RollbackExecutor struct {
executor.Input
}
Expand Down
Loading