]> source.dussan.org Git - gitea.git/commitdiff
Interpolate runs-on with variables when scheduling tasks(#30640) (#30672)
authorsillyguodong <33891828+sillyguodong@users.noreply.github.com>
Wed, 24 Apr 2024 20:37:05 +0000 (04:37 +0800)
committerGitHub <noreply@github.com>
Wed, 24 Apr 2024 20:37:05 +0000 (22:37 +0200)
backport: #30640

Co-authored-by: Giteabot <teabot@gitea.io>
models/actions/run.go
models/actions/variable.go
services/actions/schedule_tasks.go

index 105bb304c9583fc8d9af8a110951e3bf73f1488a..c1894a78b763bbc4690d2ed5e9bd9572ac4d389a 100644 (file)
@@ -95,13 +95,10 @@ func (run *ActionRun) LoadAttributes(ctx context.Context) error {
                return nil
        }
 
-       if run.Repo == nil {
-               repo, err := repo_model.GetRepositoryByID(ctx, run.RepoID)
-               if err != nil {
-                       return err
-               }
-               run.Repo = repo
+       if err := run.LoadRepo(ctx); err != nil {
+               return err
        }
+
        if err := run.Repo.LoadAttributes(ctx); err != nil {
                return err
        }
@@ -117,6 +114,19 @@ func (run *ActionRun) LoadAttributes(ctx context.Context) error {
        return nil
 }
 
+func (run *ActionRun) LoadRepo(ctx context.Context) error {
+       if run == nil || run.Repo != nil {
+               return nil
+       }
+
+       repo, err := repo_model.GetRepositoryByID(ctx, run.RepoID)
+       if err != nil {
+               return err
+       }
+       run.Repo = repo
+       return nil
+}
+
 func (run *ActionRun) Duration() time.Duration {
        return calculateDuration(run.Started, run.Stopped, run.Status)
 }
index 2620a566f7147631fcec268fcfd4c66df315fab3..17c5313392c3264f20d4979627fafd5cfd67eb7d 100644 (file)
@@ -100,6 +100,11 @@ func UpdateVariable(ctx context.Context, variable *ActionVariable) (bool, error)
 func GetVariablesOfRun(ctx context.Context, run *ActionRun) (map[string]string, error) {
        variables := map[string]string{}
 
+       if err := run.LoadRepo(ctx); err != nil {
+               log.Error("LoadRepo: %v", err)
+               return nil, err
+       }
+
        // Org / User level
        ownerVariables, err := FindVariables(ctx, FindVariablesOpts{OwnerID: run.Repo.OwnerID})
        if err != nil {
index de68d0152ccf1fbfac6dbbf7510f3e3b886b3fa9..24481b4f6619fe68cb5637d58b456b73e8fccec5 100644 (file)
@@ -127,8 +127,14 @@ func CreateScheduleTask(ctx context.Context, cron *actions_model.ActionSchedule)
                Status:        actions_model.StatusWaiting,
        }
 
+       vars, err := actions_model.GetVariablesOfRun(ctx, run)
+       if err != nil {
+               log.Error("GetVariablesOfRun: %v", err)
+               return err
+       }
+
        // Parse the workflow specification from the cron schedule
-       workflows, err := jobparser.Parse(cron.Content)
+       workflows, err := jobparser.Parse(cron.Content, jobparser.WithVars(vars))
        if err != nil {
                return err
        }