Skip to content

Commit 7aa8b8e

Browse files
Interpolate runs-on with variables when scheduling tasks(#30640) (#30672)
backport: #30640 Co-authored-by: Giteabot <[email protected]>
1 parent ddf64b8 commit 7aa8b8e

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

models/actions/run.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,10 @@ func (run *ActionRun) LoadAttributes(ctx context.Context) error {
9595
return nil
9696
}
9797

98-
if run.Repo == nil {
99-
repo, err := repo_model.GetRepositoryByID(ctx, run.RepoID)
100-
if err != nil {
101-
return err
102-
}
103-
run.Repo = repo
98+
if err := run.LoadRepo(ctx); err != nil {
99+
return err
104100
}
101+
105102
if err := run.Repo.LoadAttributes(ctx); err != nil {
106103
return err
107104
}
@@ -117,6 +114,19 @@ func (run *ActionRun) LoadAttributes(ctx context.Context) error {
117114
return nil
118115
}
119116

117+
func (run *ActionRun) LoadRepo(ctx context.Context) error {
118+
if run == nil || run.Repo != nil {
119+
return nil
120+
}
121+
122+
repo, err := repo_model.GetRepositoryByID(ctx, run.RepoID)
123+
if err != nil {
124+
return err
125+
}
126+
run.Repo = repo
127+
return nil
128+
}
129+
120130
func (run *ActionRun) Duration() time.Duration {
121131
return calculateDuration(run.Started, run.Stopped, run.Status)
122132
}

models/actions/variable.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ func UpdateVariable(ctx context.Context, variable *ActionVariable) (bool, error)
100100
func GetVariablesOfRun(ctx context.Context, run *ActionRun) (map[string]string, error) {
101101
variables := map[string]string{}
102102

103+
if err := run.LoadRepo(ctx); err != nil {
104+
log.Error("LoadRepo: %v", err)
105+
return nil, err
106+
}
107+
103108
// Org / User level
104109
ownerVariables, err := FindVariables(ctx, FindVariablesOpts{OwnerID: run.Repo.OwnerID})
105110
if err != nil {

services/actions/schedule_tasks.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,14 @@ func CreateScheduleTask(ctx context.Context, cron *actions_model.ActionSchedule)
127127
Status: actions_model.StatusWaiting,
128128
}
129129

130+
vars, err := actions_model.GetVariablesOfRun(ctx, run)
131+
if err != nil {
132+
log.Error("GetVariablesOfRun: %v", err)
133+
return err
134+
}
135+
130136
// Parse the workflow specification from the cron schedule
131-
workflows, err := jobparser.Parse(cron.Content)
137+
workflows, err := jobparser.Parse(cron.Content, jobparser.WithVars(vars))
132138
if err != nil {
133139
return err
134140
}

0 commit comments

Comments
 (0)