mirror of
				https://github.com/go-gitea/gitea
				synced 2025-11-03 21:08:25 +00:00 
			
		
		
		
	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 <teabot@gitea.io>
This commit is contained in:
		@@ -98,13 +98,10 @@ func (run *ActionRun) LoadAttributes(ctx context.Context) error {
 | 
				
			|||||||
		return nil
 | 
							return nil
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if run.Repo == nil {
 | 
						if err := run.LoadRepo(ctx); err != nil {
 | 
				
			||||||
		repo, err := repo_model.GetRepositoryByID(ctx, run.RepoID)
 | 
							return err
 | 
				
			||||||
		if err != nil {
 | 
					 | 
				
			||||||
			return err
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		run.Repo = repo
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if err := run.Repo.LoadAttributes(ctx); err != nil {
 | 
						if err := run.Repo.LoadAttributes(ctx); err != nil {
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -120,6 +117,19 @@ func (run *ActionRun) LoadAttributes(ctx context.Context) error {
 | 
				
			|||||||
	return nil
 | 
						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 {
 | 
					func (run *ActionRun) Duration() time.Duration {
 | 
				
			||||||
	return calculateDuration(run.Started, run.Stopped, run.Status) + run.PreviousDuration
 | 
						return calculateDuration(run.Started, run.Stopped, run.Status) + run.PreviousDuration
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -92,6 +92,11 @@ func DeleteVariable(ctx context.Context, id int64) error {
 | 
				
			|||||||
func GetVariablesOfRun(ctx context.Context, run *ActionRun) (map[string]string, error) {
 | 
					func GetVariablesOfRun(ctx context.Context, run *ActionRun) (map[string]string, error) {
 | 
				
			||||||
	variables := map[string]string{}
 | 
						variables := map[string]string{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if err := run.LoadRepo(ctx); err != nil {
 | 
				
			||||||
 | 
							log.Error("LoadRepo: %v", err)
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Global
 | 
						// Global
 | 
				
			||||||
	globalVariables, err := db.Find[ActionVariable](ctx, FindVariablesOpts{})
 | 
						globalVariables, err := db.Find[ActionVariable](ctx, FindVariablesOpts{})
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -132,8 +132,14 @@ func CreateScheduleTask(ctx context.Context, cron *actions_model.ActionSchedule)
 | 
				
			|||||||
		Status:        actions_model.StatusWaiting,
 | 
							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
 | 
						// 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 {
 | 
						if err != nil {
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user