Skip to content

Commit 2f6b1c4

Browse files
Interpolate runs-on with variables when scheduling tasks (#30640)
Follow #29468 1. Interpolate runs-on with variables when scheduling tasks. 2. The `GetVariablesOfRun` function will check if the `Repo` of the run is nil. --------- Co-authored-by: Giteabot <[email protected]>
1 parent b79e3db commit 2f6b1c4

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

models/actions/run.go

+16-6
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,10 @@ func (run *ActionRun) LoadAttributes(ctx context.Context) error {
9898
return nil
9999
}
100100

101-
if run.Repo == nil {
102-
repo, err := repo_model.GetRepositoryByID(ctx, run.RepoID)
103-
if err != nil {
104-
return err
105-
}
106-
run.Repo = repo
101+
if err := run.LoadRepo(ctx); err != nil {
102+
return err
107103
}
104+
108105
if err := run.Repo.LoadAttributes(ctx); err != nil {
109106
return err
110107
}
@@ -120,6 +117,19 @@ func (run *ActionRun) LoadAttributes(ctx context.Context) error {
120117
return nil
121118
}
122119

120+
func (run *ActionRun) LoadRepo(ctx context.Context) error {
121+
if run == nil || run.Repo != nil {
122+
return nil
123+
}
124+
125+
repo, err := repo_model.GetRepositoryByID(ctx, run.RepoID)
126+
if err != nil {
127+
return err
128+
}
129+
run.Repo = repo
130+
return nil
131+
}
132+
123133
func (run *ActionRun) Duration() time.Duration {
124134
return calculateDuration(run.Started, run.Stopped, run.Status) + run.PreviousDuration
125135
}

models/actions/variable.go

+5
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ func DeleteVariable(ctx context.Context, id int64) error {
9292
func GetVariablesOfRun(ctx context.Context, run *ActionRun) (map[string]string, error) {
9393
variables := map[string]string{}
9494

95+
if err := run.LoadRepo(ctx); err != nil {
96+
log.Error("LoadRepo: %v", err)
97+
return nil, err
98+
}
99+
95100
// Global
96101
globalVariables, err := db.Find[ActionVariable](ctx, FindVariablesOpts{})
97102
if err != nil {

services/actions/schedule_tasks.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,14 @@ func CreateScheduleTask(ctx context.Context, cron *actions_model.ActionSchedule)
132132
Status: actions_model.StatusWaiting,
133133
}
134134

135+
vars, err := actions_model.GetVariablesOfRun(ctx, run)
136+
if err != nil {
137+
log.Error("GetVariablesOfRun: %v", err)
138+
return err
139+
}
140+
135141
// Parse the workflow specification from the cron schedule
136-
workflows, err := jobparser.Parse(cron.Content)
142+
workflows, err := jobparser.Parse(cron.Content, jobparser.WithVars(vars))
137143
if err != nil {
138144
return err
139145
}

0 commit comments

Comments
 (0)