8000 feat(worker): add static-key to keep the same public url for serve st… · ovh/cds@0154f0f · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 0154f0f

Browse files
bnjjjyesnault
authored andcommitted
feat(worker): add static-key to keep the same public url for serve static files (#3670)
close #3657
1 parent b34e71a commit 0154f0f

File tree

9 files changed

+66
-9
lines changed

9 files changed

+66
-9
lines changed

docs/content/workflows/pipelines/actions/builtin/serve-static-files.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ Pay attention this action is only available if your objectstore is configured to
1212
## Parameters
1313
* name: Name to display in CDS UI and identify your static files
1414
* path: Path where static files will be uploaded (example: mywebsite/*). If it's a file, the entrypoint would be set to this filename by default.
15-
* tag: Filename (and not path) for the entrypoint when serving static files (default: if empty it would be index.html).
15+
* entrypoint: Filename (and not path) for the entrypoint when serving static files (default: if empty it would be index.html).
16+
* static-key: Indicate a static-key which will be a reference to keep the same generated URL. Example: {{.git.branch}}.
1617

1718
### Example
1819

19-
* In this example I created a website with script in bash and use action `Serve Static Files`.
20+
* In this example I created a website with script in bash and use action `Serve Static Files`. If you want to keep the same URL by .git.branch for example you can indicate in the advanced parameters a `static-key` equals to `{{.git.branch}}`.
2021

2122
![img]( 10000 /images/workflows.pipelines.actions.builtin.serve-static-files-job.png)
2223

engine/api/action/builtin.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,12 @@ Semver used if fully compatible with https://semver.org/
272272
Type: sdk.StringParameter,
273273
Value: "",
274274
Advanced: true})
275+
serveStaticAct.Parameter(sdk.Parameter{
276+
Name: "static-key",
277+
Description: "Indicate a static-key which will be a reference to keep the same generated URL. Example: {{.git.branch}}",
278+
Type: sdk.StringParameter,
279+
Value: "",
280+
Advanced: true})
275281

276282
return checkBuiltinAction(db, serveStaticAct)
277283
}

engine/api/workflow_queue_staticfiles.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ func (api *API) postWorkflowJobStaticFilesHandler() service.Handler {
5555
entrypoint = m.Value["entrypoint"][0]
5656
}
5757

58+
var staticKey string
59+
if len(m.Value["static_key"]) > 0 {
60+
staticKey = m.Value["static_key"][0]
61+
}
62+
5863
if fileName == "" {
5964
return sdk.WrapError(sdk.ErrWrongRequest, "Content-Disposition header is not set")
6065
}
@@ -64,13 +69,26 @@ func (api *API) postWorkflowJobStaticFilesHandler() service.Handler {
6469
return sdk.WrapError(errJ, "Cannot load node job run")
6570
}
6671

72+
nodeRun, errNr := workflow.LoadNodeRunByID(db, nodeJobRun.WorkflowNodeRunID, workflow.LoadRunOptions{DisableDetailledNodeRun: true})
73+
if errNr != nil {
74+
return sdk.WrapError(errNr, "Cannot load node run")
75+
}
76+
6777
staticFile := sdk.StaticFiles{
6878
Name: name,
6979
EntryPoint: entrypoint,
80+
StaticKey: staticKey,
81+
WorkflowID: nodeRun.WorkflowID,
7082
NodeRunID: nodeJobRun.WorkflowNodeRunID,
7183
NodeJobRunID: nodeJobRunID,
7284
}
7385

86+
if staticFile.StaticKey != "" {
87+
if err := objectstore.Delete(&staticFile); err != nil {
88+
return sdk.WrapError(err, "Cannot delete existing static files")
89+
}
90+
}
91+
7492
files := m.File[fileName]
7593
if len(files) == 1 {
7694
file, err := files[0].Open()
@@ -119,13 +137,25 @@ func (api *API) postWorkflowJobStaticFilesWithTempURLHandler() service.Handler {
119137
return sdk.WithStack(err)
120138
}
121139

140+
if staticfile.StaticKey != "" {
141+
if err := objectstore.Delete(&staticfile); err != nil {
142+
return sdk.WrapError(err, "Cannot delete existing static files")
143+
}
144+
}
145+
122146
nodeJobRun, errJ := workflow.LoadNodeJobRun(api.mustDB(), api.Cache, id)
123147
if errJ != nil {
124148
return sdk.WrapError(errJ, "Cannot load node job run")
125149
}
126150
staticfile.NodeRunID = nodeJobRun.WorkflowNodeRunID
127151
staticfile.Name = name
128152

153+
nodeRun, errNr := workflow.LoadNodeRunByID(api.mustDB(), nodeJobRun.WorkflowNodeRunID, workflow.LoadRunOptions{DisableDetailledNodeRun: true})
154+
if errNr != nil {
155+
return sdk.WrapError(errNr, "Cannot load node run")
156+
}
157+
staticfile.WorkflowID = nodeRun.WorkflowID
158+
129159
var retryURL = 10
130160
var url, key string
131161
var errorStoreURL error

engine/sql/141_static_files_key.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- +migrate Up
2+
3+
INSERT into action_parameter (action_id, name, description, type, value, advanced) values((select id from action where name = 'Serve Static Files' and type = 'Builtin'), 'static-key', 'Indicate a static-key which will be a reference to keep the same generated URL. Example: {{.git.branch}}', 'string', '', true);
4+
ALTER TABLE workflow_node_run_static_files ADD COLUMN static_key TEXT DEFAULT '';
5+
6+
-- +migrate Down
7+
8+
DELETE from action_parameter where name = 'static-key' and action_id = (select id from action where name = 'Serve Static Files' and type = 'Builtin');
9+
ALTER TABLE workflow_node_run_static_files DROP COLUMN static_key;

engine/worker/builtin_serve_static_files.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func runServeStaticFiles(w *currentWorker) BuiltInAction {
3333
sendLog(res.Reason)
3434
return res
3535
}
36+
staticKey := sdk.ParameterValue(a.Parameters, "static-key")
3637

3738
// Global all files matching filePath
3839
filesPath, err := filepath.Glob(path)
@@ -65,7 +66,7 @@ func runServeStaticFiles(w *currentWorker) BuiltInAction {
6566
}
6667

6768
sendLog(fmt.Sprintf(`Upload and serving files in progress... with entrypoint "%s"`, entrypoint.Value))
68-
publicURL, _, _, err := w.client.QueueStaticFilesUpload(ctx, buildID, name.Value, entrypoint.Value, file)
69+
publicURL, _, _, err := w.client.QueueStaticFilesUpload(ctx, buildID, name.Value, entrypoint.Value, staticKey, file)
6970
if err != nil {
7071
res.Status = sdk.StatusFail.String()
7172
res.Reason = fmt.Sprintf("Cannot upload static files: %v", err)

sdk/cdsclient/client_queue.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,10 +564,11 @@ func (c *client) QueueServiceLogs(ctx context.Context, logs []sdk.ServiceLog) er
564564

565565
// STATIC FILES -----
566566

567-
func (c *client) QueueStaticFilesUpload(ctx context.Context, nodeJobRunID int64, name, entrypoint string, tarContent< 10000 /span> io.Reader) (string, bool, time.Duration, error) {
567+
func (c *client) QueueStaticFilesUpload(ctx context.Context, nodeJobRunID int64, name, entrypoint, staticKey string, tarContent io.Reader) (string, bool, time.Duration, error) {
568568
t0 := time.Now()
569569
staticFile := sdk.StaticFiles{
570570
EntryPoint: entrypoint,
571+
StaticKey: staticKey,
571572
Name: name,
572573
NodeJobRunID: nodeJobRunID,
573574
}
@@ -699,6 +700,7 @@ func (c *client) queueDirectStaticFilesUpload(staticFile *sdk.StaticFiles, tarCo
699700

700701
_ = writer.WriteField("name", staticFile.Name)
701702
_ = writer.WriteField("entrypoint", staticFile.EntryPoint)
703+
_ = writer.WriteField("static_key", staticFile.StaticKey)
702704

703705
if errclose := writer.Close(); errclose != nil {
704706
return "", errclose

sdk/cdsclient/interface.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ type QueueClient interface {
223223
QueueJobSendSpawnInfo(ctx context.Context, isWorkflowJob bool, id int64, in []sdk.SpawnInfo) error
224224
QueueSendResult(ctx context.Context, id int64, res sdk.Result) error
225225
QueueArtifactUpload(ctx context.Context, id int64, tag, filePath string) (bool, time.Duration, error)
226-
QueueStaticFilesUpload(ctx context.Context, nodeJobRunID int64, name, entrypoint string, tarContent io.Reader) (string, bool, time.Duration, error)
226+
QueueStaticFilesUpload(ctx context.Context, nodeJobRunID int64, name, entrypoint, staticKey string, tarContent io.Reader) (string, bool, time.Duration, error)
227227
QueueJobTag(ctx context.Context, jobID int64, tags []sdk.WorkflowRunTag) error
228228
QueueJobIncAttempts(ctx context.Context, jobID int64) ([]int64, error)
229229
QueueServiceLogs(ctx context.Context, logs []sdk.ServiceLog) error

sdk/static_files.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ import (
1111
type StaticFiles struct {
1212
ID int64 `json:"id" db:"id" cli:"id"`
1313
Name string `json:"name" db:"name" cli:"name"`
14+
WorkflowID int64 `json:"workflow_id" db:"-"`
1415
NodeRunID int64 `json:"workflow_node_run_id" db:"workflow_node_run_id"`
1516
NodeJobRunID int64 `json:"workflow_node_run_job_id,omitempty" db:"-"`
1617
EntryPoint string `json:"entrypoint" db:"entrypoint"`
18+
StaticKey string `json:"static_key" db:"static_key"`
1719
PublicURL string `json:"public_url" db:"public_url" cli:"public_url"`
1820
Created time.Time `json:"created" db:"created" cli:"created"`
1921

@@ -28,13 +30,19 @@ func (staticfile *StaticFiles) GetName() string {
2830

2931
//GetPath returns the path of the artifact
3032
func (staticfile *StaticFiles) GetPath() string {
31-
container := base64.RawURLEncoding.EncodeToString([]byte(fmt.Sprintf("%d-%s", staticfile.NodeJobRunID, url.PathEscape(staticfile.Name))))
32-
return container
33+
var container string
34+
if staticfile.StaticKey != "" {
35+
container = fmt.Sprintf("%d-%s-%s", staticfile.WorkflowID, url.PathEscape(staticfile.StaticKey), url.PathEscape(staticfile.Name))
36+
} else {
37+
container = fmt.Sprintf("%d-%s", staticfile.NodeJobRunID, url.PathEscape(staticfile.Name))
38+
}
39+
return base64.RawURLEncoding.EncodeToString([]byte(container))
3340
}
3441

3542
// Equal returns true if StaticFiles are equal to another one
3643
func (staticfile StaticFiles) Equal(currStaticfile StaticFiles) bool {
3744
return currStaticfile.NodeRunID == staticfile.NodeRunID &&
3845
currStaticfile.Name == staticfile.Name &&
39-
currStaticfile.EntryPoint == staticfile.EntryPoint
46+
currStaticfile.EntryPoint == staticfile.EntryPoint &&
47+
currStaticfile.StaticKey == staticfile.StaticKey
4048
}

ui/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"build:prod": "ng build --prod",
2121
"precommit": "lint-staged",
2222
"sentry:release": "sentry-cli releases -o ${SENTRY_ORG} -p ${SENTRY_PROJECT} new ${CDS_VERSION}",
23-
"sentry:sourcemaps": "sentry-cli releases -o ${SENTRY_ORG} -p ${SENTRY_PROJECT} files ${CDS_VERSION} upload-sourcemaps"
23+
"sentry:sourcemaps": "sentry-cli releases -o ${SENTRY_ORG} -p ${SENTRY_PROJECT} files ${CDS_VERSION} upload-sourcemaps --url-prefix=${SENTRY_CDS_PREFIX_URL}"
2424
},
2525
"lint-staged": {
2626
"linters": {

0 commit comments

Comments
 (0)
0