diff --git a/api-docs.yml b/api-docs.yml index dbadbb26b..fa96f861c 100644 --- a/api-docs.yml +++ b/api-docs.yml @@ -796,6 +796,8 @@ definitions: type: integer autorun: type: boolean + extra_vars_json: + type: string Template: type: object @@ -860,6 +862,8 @@ definitions: type: array items: $ref: "#/definitions/TemplateVault" + extra_vars_json: + type: string TemplateSurveyVar: type: object diff --git a/db/Migration.go b/db/Migration.go index 44d134185..7f83abfad 100644 --- a/db/Migration.go +++ b/db/Migration.go @@ -83,6 +83,7 @@ func GetMigrations() []Migration { {Version: "2.12.5"}, {Version: "2.12.15"}, {Version: "2.13.0"}, + {Version: "2.13.6"}, } } diff --git a/db/Template.go b/db/Template.go index e0df5ce2c..373241fea 100644 --- a/db/Template.go +++ b/db/Template.go @@ -146,6 +146,8 @@ type Template struct { TaskParams MapStringAnyField `db:"task_params" json:"task_params"` RunnerTag *string `db:"runner_tag" json:"runner_tag"` + + ExtraVarsJSON string `db:"extra_vars_json" json:"extra_vars_json"` } func (tpl *Template) FillParams(target interface{}) error { @@ -193,6 +195,10 @@ func (tpl *Template) Validate() error { } } + if tpl.ExtraVarsJSON != "" && !json.Valid([]byte(tpl.ExtraVarsJSON)) { + return &ValidationError{"Extra variables must be valid JSON"} + } + return nil } diff --git a/db/sql/migrations/v2.13.6.sql b/db/sql/migrations/v2.13.6.sql new file mode 100644 index 000000000..0ca9b5c7b --- /dev/null +++ b/db/sql/migrations/v2.13.6.sql @@ -0,0 +1 @@ +alter table `project__template` add column `extra_vars_json` longtext null; \ No newline at end of file diff --git a/db/sql/template.go b/db/sql/template.go index 5da8e13ee..0f6fc3eff 100644 --- a/db/sql/template.go +++ b/db/sql/template.go @@ -19,12 +19,12 @@ func (d *SqlDb) CreateTemplate(template db.Template) (newTemplate db.Template, e "project_id, inventory_id, repository_id, environment_id, name, "+ "playbook, arguments, allow_override_args_in_task, description, `type`, "+ "start_version, build_template_id, view_id, autorun, survey_vars, "+ - "suppress_success_alerts, app, git_branch, runner_tag, task_params)"+ + "suppress_success_alerts, app, git_branch, runner_tag, task_params, extra_vars_json)"+ "values ("+ "?, ?, ?, ?, ?, "+ "?, ?, ?, ?, ?, "+ "?, ?, ?, ?, ?, "+ - "?, ?, ?, ?, ?)", + "?, ?, ?, ?, ?, ?)", template.ProjectID, template.InventoryID, template.RepositoryID, @@ -48,6 +48,7 @@ func (d *SqlDb) CreateTemplate(template db.Template) (newTemplate db.Template, e template.GitBranch, template.RunnerTag, template.TaskParams, + template.ExtraVarsJSON, ) if err != nil { @@ -97,7 +98,8 @@ func (d *SqlDb) UpdateTemplate(template db.Template) error { "app=?, "+ "`git_branch`=?, "+ "task_params=?, "+ - "runner_tag=? "+ + "runner_tag=?, "+ + "extra_vars_json=? "+ "where id=? and project_id=?", template.InventoryID, template.RepositoryID, @@ -118,6 +120,7 @@ func (d *SqlDb) UpdateTemplate(template db.Template) error { template.GitBranch, template.TaskParams, template.RunnerTag, + template.ExtraVarsJSON, template.ID, template.ProjectID, ) diff --git a/services/tasks/LocalJob.go b/services/tasks/LocalJob.go index b2058fcbd..571951b9f 100644 --- a/services/tasks/LocalJob.go +++ b/services/tasks/LocalJob.go @@ -100,6 +100,7 @@ func (t *LocalJob) getEnvironmentExtraVars(username string, incomingVersion *str func (t *LocalJob) getEnvironmentExtraVarsJSON(username string, incomingVersion *string) (str string, err error) { extraVars := make(map[string]interface{}) extraSecretVars := make(map[string]interface{}) + extraTemplateVars := make(map[string]interface{}) if t.Environment.JSON != "" { err = json.Unmarshal([]byte(t.Environment.JSON), &extraVars) @@ -115,7 +116,15 @@ func (t *LocalJob) getEnvironmentExtraVarsJSON(username string, incomingVersion } t.Secret = "{}" + if t.Template.ExtraVarsJSON != "" && t.Template.ExtraVarsJSON != "{}" { + err = json.Unmarshal([]byte(t.Template.ExtraVarsJSON), &extraTemplateVars) + if err != nil { + return + } + } + maps.Copy(extraVars, extraSecretVars) + maps.Copy(extraVars, extraTemplateVars) taskDetails := make(map[string]interface{}) diff --git a/web/src/components/TemplateForm.vue b/web/src/components/TemplateForm.vue index f8af640cf..b927b9a3b 100644 --- a/web/src/components/TemplateForm.vue +++ b/web/src/components/TemplateForm.vue @@ -328,6 +328,18 @@ v-if="needField('allow_debug')" /> +