Allow override of Template git branch. Prefer Task override when running Local Job.

This commit is contained in:
Brian Zoetewey 2024-09-16 15:42:05 -04:00
parent fa24bc9ab0
commit 792d42bf26
6 changed files with 29 additions and 5 deletions

View File

@ -125,6 +125,7 @@ func resolveCapability(caps []string, resolved []string, uid string) {
case "template":
args := "[]"
desc := "Hello, World!"
branch := "main"
res, err := store.CreateTemplate(db.Template{
ProjectID: userProject.ID,
InventoryID: &inventoryID,
@ -137,6 +138,7 @@ func resolveCapability(caps []string, resolved []string, uid string) {
Description: &desc,
ViewID: &view.ID,
App: db.AppAnsible,
GitBranch: &branch,
})
printError(err)

View File

@ -713,6 +713,9 @@ definitions:
app:
type: string
example: ansible
git_branch:
type: string
example: main
survey_vars:
type: array
items:
@ -756,6 +759,9 @@ definitions:
type: boolean
app:
type: string
git_branch:
type: string
example: main
TemplateSurveyVar:
type: object
properties:

View File

@ -88,6 +88,9 @@ type Template struct {
Autorun bool `db:"autorun" json:"autorun"`
// override variables
GitBranch *string `db:"git_branch" json:"git_branch"`
// SurveyVarsJSON used internally for read from database.
// It is not used for store survey vars to database.
// Do not use it in your code. Use SurveyVars instead.

View File

@ -1 +1,3 @@
alter table `task` add `git_branch` varchar(255);
alter table task add git_branch varchar(255);
alter table project__template add git_branch varchar(255);

View File

@ -18,8 +18,8 @@ func (d *SqlDb) CreateTemplate(template db.Template) (newTemplate db.Template, e
"id",
"insert into project__template (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)"+
"values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
"build_template_id, view_id, autorun, survey_vars, suppress_success_alerts, app, git_branch)"+
"values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
template.ProjectID,
template.InventoryID,
template.RepositoryID,
@ -36,7 +36,8 @@ func (d *SqlDb) CreateTemplate(template db.Template) (newTemplate db.Template, e
template.Autorun,
db.ObjectToJSON(template.SurveyVars),
template.SuppressSuccessAlerts,
template.App)
template.App,
template.GitBranch)
if err != nil {
return
@ -82,7 +83,8 @@ func (d *SqlDb) UpdateTemplate(template db.Template) error {
"autorun=?, "+
"survey_vars=?, "+
"suppress_success_alerts=?, "+
"app=? "+
"app=?, "+
"`git_branch`=? "+
"where id=? and project_id=?",
template.InventoryID,
template.RepositoryID,
@ -100,6 +102,7 @@ func (d *SqlDb) UpdateTemplate(template db.Template) error {
db.ObjectToJSON(template.SurveyVars),
template.SuppressSuccessAlerts,
template.App,
template.GitBranch,
template.ID,
template.ProjectID,
)
@ -127,6 +130,7 @@ func (d *SqlDb) GetTemplates(projectID int, filter db.TemplateFilter, params db.
"pt.repository_id",
"pt.environment_id",
"pt.name",
"pt.description",
"pt.playbook",
"pt.arguments",
"pt.allow_override_args_in_task",
@ -134,6 +138,7 @@ func (d *SqlDb) GetTemplates(projectID int, filter db.TemplateFilter, params db.
"pt.start_version",
"pt.view_id",
"pt.`app`",
"pt.`git_branch`",
"pt.survey_vars",
"pt.start_version",
"pt.`type`",

View File

@ -465,6 +465,12 @@ func (t *LocalJob) prepareRun() error {
return err
}
// Override git branch from template if set
if t.Template.GitBranch != nil && *t.Template.GitBranch != "" {
t.Repository.GitBranch = *t.Template.GitBranch
}
// Override git branch from task if set
if t.Task.GitBranch != "" {
t.Repository.GitBranch = t.Task.GitBranch
}