Skip to content

feat(template): implements extra variables in the template #2872

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions api-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,8 @@ definitions:
type: integer
autorun:
type: boolean
extra_vars_json:
type: string

Template:
type: object
Expand Down Expand Up @@ -860,6 +862,8 @@ definitions:
type: array
items:
$ref: "#/definitions/TemplateVault"
extra_vars_json:
type: string

TemplateSurveyVar:
type: object
Expand Down
1 change: 1 addition & 0 deletions db/Migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func GetMigrations() []Migration {
{Version: "2.12.5"},
{Version: "2.12.15"},
{Version: "2.13.0"},
{Version: "2.13.6"},
}
}

Expand Down
6 changes: 6 additions & 0 deletions db/Template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down
1 change: 1 addition & 0 deletions db/sql/migrations/v2.13.6.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table `project__template` add column `extra_vars_json` longtext null;
9 changes: 6 additions & 3 deletions db/sql/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -118,6 +120,7 @@ func (d *SqlDb) UpdateTemplate(template db.Template) error {
template.GitBranch,
template.TaskParams,
template.RunnerTag,
template.ExtraVarsJSON,
template.ID,
template.ProjectID,
)
Expand Down
9 changes: 9 additions & 0 deletions services/tasks/LocalJob.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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{})

Expand Down
18 changes: 18 additions & 0 deletions web/src/components/TemplateForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,18 @@
v-if="needField('allow_debug')"
/>

<div>
{{ $t('templateExtraVariablesJson') }}
<codemirror
:style="{ border: '1px solid lightgray',
resize: 'vertical', overflow: 'auto',
maxHeight: '400px', minHeight: '50px' }"
v-model="item.extra_vars_json"
:options="cmOptions"
:placeholder="$t('enterTemplateExtraVariablesJson')"
/>
</div>

<!-- <v-checkbox-->
<!-- class="mt-0"-->
<!-- :label="$t('allowLimitInTask')"-->
Expand All @@ -345,6 +357,10 @@
color: #a4a4a4 !important;
}

.CodeMirror {
height: 100%;
}

.TemplateFormBody {
display: flex;
flex-direction: column;
Expand Down Expand Up @@ -380,6 +396,7 @@
import axios from 'axios';

import ItemFormBase from '@/components/ItemFormBase';
import { codemirror } from 'vue-codemirror';
import 'codemirror/lib/codemirror.css';
import 'codemirror/mode/vue/vue.js';
import 'codemirror/addon/lint/json-lint.js';
Expand All @@ -397,6 +414,7 @@ export default {
TemplateVaults,
ArgsPicker,
SurveyVars,
codemirror,
},

props: {
Expand Down
3 changes: 3 additions & 0 deletions web/src/lang/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,4 +321,7 @@ export default {
tags: 'Tags',

limit: 'Limit',

templateExtraVariablesJson: 'Extra variables',
enterTemplateExtraVariablesJson: 'Enter extra variables JSON...',
};
Loading