mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-10-29 10:57:44 +09:00 
			
		
		
		
	Support upload outputs and use needs context on Actions (#24230)
				
					
				
			See [Defining outputs for jobs](https://docs.github.com/en/actions/using-jobs/defining-outputs-for-jobs) and [Example usage of the needs context](https://docs.github.com/en/actions/learn-github-actions/contexts#example-usage-of-the-needs-context). Related to: - [actions-proto-def #5](https://gitea.com/gitea/actions-proto-def/pulls/5) - [act_runner #133](https://gitea.com/gitea/act_runner/pulls/133) <details> <summary>Tests & screenshots</summary> Test workflow file: ```yaml name: outputs on: push jobs: job1: runs-on: ubuntu-latest outputs: output1: ${{ steps.step1.outputs.output1 }} output2: ${{ steps.step2.outputs.output2 }} steps: - name: step1 id: step1 run: | date -Is > output1 cat output1 echo "output1=$(cat output1)" >> $GITHUB_OUTPUT - name: step2 id: step2 run: | cat /proc/sys/kernel/random/uuid > output2 cat output2 echo "output2=$(cat output2)" >> $GITHUB_OUTPUT job2: needs: job1 runs-on: ubuntu-latest steps: - run: echo ${{ needs.job1.outputs.output1 }} - run: echo ${{ needs.job1.outputs.output2 }} - run: echo ${{ needs.job1.result }} ``` <img width="397" alt="image" src="https://user-images.githubusercontent.com/9418365/233313322-903e7ebf-49a7-48e2-8c17-95a4581b3284.png"> <img width="385" alt="image" src="https://user-images.githubusercontent.com/9418365/233313442-30909135-1711-4b78-a5c6-133fcc79f47c.png"> </details> --------- Co-authored-by: Giteabot <teabot@gitea.io>
This commit is contained in:
		| @@ -37,6 +37,17 @@ func pickTask(ctx context.Context, runner *actions_model.ActionRunner) (*runnerv | ||||
| 		Context:         generateTaskContext(t), | ||||
| 		Secrets:         getSecretsOfTask(ctx, t), | ||||
| 	} | ||||
|  | ||||
| 	if needs, err := findTaskNeeds(ctx, t); err != nil { | ||||
| 		log.Error("Cannot find needs for task %v: %v", t.ID, err) | ||||
| 		// Go on with empty needs. | ||||
| 		// If return error, the task will be wild, which means the runner will never get it when it has been assigned to the runner. | ||||
| 		// In contrast, missing needs is less serious. | ||||
| 		// And the task will fail and the runner will report the error in the logs. | ||||
| 	} else { | ||||
| 		task.Needs = needs | ||||
| 	} | ||||
|  | ||||
| 	return task, true, nil | ||||
| } | ||||
|  | ||||
| @@ -124,3 +135,46 @@ func generateTaskContext(t *actions_model.ActionTask) *structpb.Struct { | ||||
|  | ||||
| 	return taskContext | ||||
| } | ||||
|  | ||||
| func findTaskNeeds(ctx context.Context, task *actions_model.ActionTask) (map[string]*runnerv1.TaskNeed, error) { | ||||
| 	if err := task.LoadAttributes(ctx); err != nil { | ||||
| 		return nil, fmt.Errorf("LoadAttributes: %w", err) | ||||
| 	} | ||||
| 	if len(task.Job.Needs) == 0 { | ||||
| 		return nil, nil | ||||
| 	} | ||||
| 	needs := map[string]struct{}{} | ||||
| 	for _, v := range task.Job.Needs { | ||||
| 		needs[v] = struct{}{} | ||||
| 	} | ||||
|  | ||||
| 	jobs, _, err := actions_model.FindRunJobs(ctx, actions_model.FindRunJobOptions{RunID: task.Job.RunID}) | ||||
| 	if err != nil { | ||||
| 		return nil, fmt.Errorf("FindRunJobs: %w", err) | ||||
| 	} | ||||
|  | ||||
| 	ret := make(map[string]*runnerv1.TaskNeed, len(needs)) | ||||
| 	for _, job := range jobs { | ||||
| 		if _, ok := needs[job.JobID]; !ok { | ||||
| 			continue | ||||
| 		} | ||||
| 		if job.TaskID == 0 || !job.Status.IsDone() { | ||||
| 			// it shouldn't happen, or the job has been rerun | ||||
| 			continue | ||||
| 		} | ||||
| 		outputs := make(map[string]string) | ||||
| 		got, err := actions_model.FindTaskOutputByTaskID(ctx, job.TaskID) | ||||
| 		if err != nil { | ||||
| 			return nil, fmt.Errorf("FindTaskOutputByTaskID: %w", err) | ||||
| 		} | ||||
| 		for _, v := range got { | ||||
| 			outputs[v.OutputKey] = v.OutputValue | ||||
| 		} | ||||
| 		ret[job.JobID] = &runnerv1.TaskNeed{ | ||||
| 			Outputs: outputs, | ||||
| 			Result:  runnerv1.Result(job.Status), | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	return ret, nil | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user