Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0bcfe73

Browse files
author
Coenen Benjamin
authored
feat(api): add cds.template.version var for current template version (#4927)
Signed-off-by: Benjamin Coenen <[email protected]>
1 parent f3ef764 commit 0bcfe73

File tree

4 files changed

+68
-3
lines changed

4 files changed

+68
-3
lines changed

cli/cdsctl/template.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func template() *cobra.Command {
2727
cli.NewCommand(templateBulkCmd, templateBulkRun, nil, withAllCommandModifiers()...),
2828
cli.NewCommand(templatePullCmd, templatePullRun, nil, withAllCommandModifiers()...),
2929
cli.NewCommand(templatePushCmd, templatePushRun, nil, withAllCommandModifiers()...),
30-
cli.NewCommand(templateDeleteCmd, templateDeleteRun, nil, withAllCommandModifiers()...),
30+
cli.NewDeleteCommand(templateDeleteCmd, templateDeleteRun, nil, withAllCommandModifiers()...),
3131
cli.NewListCommand(templateInstancesCmd, templateInstancesRun, nil, withAllCommandModifiers()...),
3232
cli.NewCommand(templateDetachCmd, templateDetachRun, nil, withAllCommandModifiers()...),
3333
})
@@ -212,21 +212,28 @@ var templateDeleteCmd = cli.Command{
212212
func templateDeleteRun(v cli.Values) error {
213213
wt, err := getTemplateFromCLI(v)
214214
if err != nil {
215+
if v.GetBool("force") && sdk.ErrorIs(err, sdk.ErrNotFound) {
216+
return nil
217+
}
215218
return err
216219
}
217220
if wt == nil {
218221
wt, err = suggestTemplate()
219222
if err != nil {
223+
if v.GetBool("force") && sdk.ErrorIs(err, sdk.ErrNotFound) {
224+
return nil
225+
}
220226
return err
221227
}
222228
}
223229

224230
if err := client.TemplateDelete(wt.Group.Name, wt.Slug); err != nil {
231+
if v.GetBool("force") && sdk.ErrorIs(err, sdk.ErrNotFound) {
232+
return nil
233+
}
225234
return err
226235
}
227236

228-
fmt.Println("Template successfully deleted")
229-
230237
return nil
231238
}
232239

engine/api/workflow/process_parameters.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,10 @@ func getNodeRunBuildParameters(ctx context.Context, proj *sdk.Project, wr *sdk.W
220220
tmp["cds.run.number"] = fmt.Sprintf("%d", run.Number)
221221
tmp["cds.run.subnumber"] = fmt.Sprintf("%d", run.SubNumber)
222222

223+
if wr.Workflow.TemplateInstance != nil {
224+
tmp["cds.template.version"] = fmt.Sprintf("%d", wr.Workflow.TemplateInstance.WorkflowTemplateVersion)
225+
}
226+
223227
_, next := observability.Span(ctx, "workflow.interpolate")
224228
params = make([]sdk.Parameter, 0, len(tmp))
225229
for k, v := range tmp {

engine/api/workflow_run.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"time"
1212

1313
"github.com/ovh/cds/engine/api/permission"
14+
"github.com/ovh/cds/engine/api/workflowtemplate"
1415

1516
"github.com/go-gorp/gorp"
1617
"github.com/gorilla/mux"
@@ -924,6 +925,10 @@ func (api *API) postWorkflowRunHandler() service.Handler {
924925
return sdk.WrapError(errWf, "unable to load workflow %s", name)
925926
}
926927

928+
if err := workflowtemplate.AggregateTemplateInstanceOnWorkflow(ctx, api.mustDB(), wf); err != nil {
929+
return sdk.WrapError(err, "cannot load workflow template")
930+
}
931+
927932
// Check node permission
928933
if isService := isService(ctx); !isService && !permission.AccessToWorkflowNode(ctx, api.mustDB(), wf, &wf.WorkflowData.Node, getAPIConsumer(ctx), sdk.PermissionReadExecute) {
929934
return sdk.WrapError(sdk.ErrNoPermExecution, "not enough right on node %s", wf.WorkflowData.Node.Name)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
name: Push a template and run this template
2+
version: "2"
3+
testcases:
4+
- name: prepare test
5+
steps:
6+
- script: "{{.cdsctl}} -f {{.cdsctl.config}} template delete shared.infra/demo-workflow-hello-world --force"
7+
- script: "{{.cdsctl}} -f {{.cdsctl.config}} project remove --force 04SCWORKFLOWTEMPLATERUN"
8+
- script: "{{.cdsctl}} -f {{.cdsctl.config}} project remove --force 04SCWORKFLOWTEMPLATERUN"
9+
- script: "{{.cdsctl}} -f {{.cdsctl.config}} group remove --force 04scworkflowtemplaterun "
10+
- script: "{{.cdsctl}} -f {{.cdsctl.config}} project add 04SCWORKFLOWTEMPLATERUN 04scworkflowtemplaterun"
11+
12+
- name: template push
13+
steps:
14+
- script: "{{.cdsctl}} -f {{.cdsctl.config}} template push https://raw.githubusercontent.com/ovh/cds/master/contrib/workflow-templates/demo-workflow-hello-world/demo-workflow-hello-world.yml"
15+
assertions:
16+
- result.code ShouldEqual 0
17+
- "result.systemout ShouldContainSubstring successfully pushed"
18+
19+
- name: create workflow from template
20+
steps:
21+
- script: "{{.cdsctl}} -f {{.cdsctl.config}} workflow applyTemplate 04SCWORKFLOWTEMPLATERUN testtemplate shared.infra/demo-workflow-hello-world --import-push -n"
22+
assertions:
23+
- result.code ShouldEqual 0
24+
- "result.systemout ShouldContainSubstring has been created"
25+
26+
- name: run workflow
27+
steps:
28+
- script: {{.cdsctl}} -f {{.cdsctl.config}} workflow run 04SCWORKFLOWTEMPLATERUN testtemplate
29+
assertions:
30+
- result.code ShouldEqual 0
31+
- "result.systemout ShouldContainSubstring has been launched"
32+
33+
- name: check workflow
34+
steps:
35+
- script: {{.cdsctl}} -f {{.cdsctl.config}} workflow status 04SCWORKFLOWTEMPLATERUN testtemplate 1 --format json
36+
retry: 20
37+
delay: 10
38+
assertions:
39+
- result.code ShouldEqual 0
40+
- result.systemoutjson.last_execution ShouldNotBeEmpty
41+
- result.systemoutjson.start ShouldNotBeEmpty
42+
- result.systemoutjson.num ShouldContainSubstring 1
43+
- result.systemoutjson.status ShouldEqual Success
44+
- result.systemoutjson.tags ShouldContainSubstring triggered_by
45+
46+
- name: check workflow log
47+
steps:
48+
- script: {{.cdsctl}} -f {{.cdsctl.config}} workflow logs download 04SCWORKFLOWTEMPLATERUN testtemplate 1 --pattern build
49+
- script: grep "CDS_TEMPLATE_VERSION=0" *.log

0 commit comments

Comments
 (0)