Skip to content

Commit

Permalink
feat(api): add cds.template.version var for current template version (#…
Browse files Browse the repository at this point in the history
…4927)

Signed-off-by: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com>
  • Loading branch information
bnjjj committed Jan 29, 2020
1 parent f3ef764 commit 0bcfe73
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 3 deletions.
13 changes: 10 additions & 3 deletions cli/cdsctl/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func template() *cobra.Command {
cli.NewCommand(templateBulkCmd, templateBulkRun, nil, withAllCommandModifiers()...),
cli.NewCommand(templatePullCmd, templatePullRun, nil, withAllCommandModifiers()...),
cli.NewCommand(templatePushCmd, templatePushRun, nil, withAllCommandModifiers()...),
cli.NewCommand(templateDeleteCmd, templateDeleteRun, nil, withAllCommandModifiers()...),
cli.NewDeleteCommand(templateDeleteCmd, templateDeleteRun, nil, withAllCommandModifiers()...),
cli.NewListCommand(templateInstancesCmd, templateInstancesRun, nil, withAllCommandModifiers()...),
cli.NewCommand(templateDetachCmd, templateDetachRun, nil, withAllCommandModifiers()...),
})
Expand Down Expand Up @@ -212,21 +212,28 @@ var templateDeleteCmd = cli.Command{
func templateDeleteRun(v cli.Values) error {
wt, err := getTemplateFromCLI(v)
if err != nil {
if v.GetBool("force") && sdk.ErrorIs(err, sdk.ErrNotFound) {
return nil
}
return err
}
if wt == nil {
wt, err = suggestTemplate()
if err != nil {
if v.GetBool("force") && sdk.ErrorIs(err, sdk.ErrNotFound) {
return nil
}
return err
}
}

if err := client.TemplateDelete(wt.Group.Name, wt.Slug); err != nil {
if v.GetBool("force") && sdk.ErrorIs(err, sdk.ErrNotFound) {
return nil
}
return err
}

fmt.Println("Template successfully deleted")

return nil
}

Expand Down
4 changes: 4 additions & 0 deletions engine/api/workflow/process_parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,10 @@ func getNodeRunBuildParameters(ctx context.Context, proj *sdk.Project, wr *sdk.W
tmp["cds.run.number"] = fmt.Sprintf("%d", run.Number)
tmp["cds.run.subnumber"] = fmt.Sprintf("%d", run.SubNumber)

if wr.Workflow.TemplateInstance != nil {
tmp["cds.template.version"] = fmt.Sprintf("%d", wr.Workflow.TemplateInstance.WorkflowTemplateVersion)
}

_, next := observability.Span(ctx, "workflow.interpolate")
params = make([]sdk.Parameter, 0, len(tmp))
for k, v := range tmp {
Expand Down
5 changes: 5 additions & 0 deletions engine/api/workflow_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"github.com/ovh/cds/engine/api/permission"
"github.com/ovh/cds/engine/api/workflowtemplate"

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

if err := workflowtemplate.AggregateTemplateInstanceOnWorkflow(ctx, api.mustDB(), wf); err != nil {
return sdk.WrapError(err, "cannot load workflow template")
}

// Check node permission
if isService := isService(ctx); !isService && !permission.AccessToWorkflowNode(ctx, api.mustDB(), wf, &wf.WorkflowData.Node, getAPIConsumer(ctx), sdk.PermissionReadExecute) {
return sdk.WrapError(sdk.ErrNoPermExecution, "not enough right on node %s", wf.WorkflowData.Node.Name)
Expand Down
49 changes: 49 additions & 0 deletions tests/04_sc_workflow_template_run.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Push a template and run this template
version: "2"
testcases:
- name: prepare test
steps:
- script: "{{.cdsctl}} -f {{.cdsctl.config}} template delete shared.infra/demo-workflow-hello-world --force"
- script: "{{.cdsctl}} -f {{.cdsctl.config}} project remove --force 04SCWORKFLOWTEMPLATERUN"
- script: "{{.cdsctl}} -f {{.cdsctl.config}} project remove --force 04SCWORKFLOWTEMPLATERUN"
- script: "{{.cdsctl}} -f {{.cdsctl.config}} group remove --force 04scworkflowtemplaterun "
- script: "{{.cdsctl}} -f {{.cdsctl.config}} project add 04SCWORKFLOWTEMPLATERUN 04scworkflowtemplaterun"

- name: template push
steps:
- 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"
assertions:
- result.code ShouldEqual 0
- "result.systemout ShouldContainSubstring successfully pushed"

- name: create workflow from template
steps:
- script: "{{.cdsctl}} -f {{.cdsctl.config}} workflow applyTemplate 04SCWORKFLOWTEMPLATERUN testtemplate shared.infra/demo-workflow-hello-world --import-push -n"
assertions:
- result.code ShouldEqual 0
- "result.systemout ShouldContainSubstring has been created"

- name: run workflow
steps:
- script: {{.cdsctl}} -f {{.cdsctl.config}} workflow run 04SCWORKFLOWTEMPLATERUN testtemplate
assertions:
- result.code ShouldEqual 0
- "result.systemout ShouldContainSubstring has been launched"

- name: check workflow
steps:
- script: {{.cdsctl}} -f {{.cdsctl.config}} workflow status 04SCWORKFLOWTEMPLATERUN testtemplate 1 --format json
retry: 20
delay: 10
assertions:
- result.code ShouldEqual 0
- result.systemoutjson.last_execution ShouldNotBeEmpty
- result.systemoutjson.start ShouldNotBeEmpty
- result.systemoutjson.num ShouldContainSubstring 1
- result.systemoutjson.status ShouldEqual Success
- result.systemoutjson.tags ShouldContainSubstring triggered_by

- name: check workflow log
steps:
- script: {{.cdsctl}} -f {{.cdsctl.config}} workflow logs download 04SCWORKFLOWTEMPLATERUN testtemplate 1 --pattern build
- script: grep "CDS_TEMPLATE_VERSION=0" *.log

0 comments on commit 0bcfe73

Please sign in to comment.