1// SPDX-License-Identifier: MIT
2
3package executor
4
5import "os"
6
7// prepareJob is the actions part of "Set up job" (ActionManager.PrepareActionsAsync
8// and JobExtension.cs:299-362): resolve and download the actions of the job,
9// and build the queue of the steps: the steps that pull and build images, the
10// pre stages in the order of the steps, then the steps themselves.
11func (j *job) prepareJob() ([]qstep, error) {
12 j.actions = newActionState()
13 // The actions of a previous job are not reused (ActionManager.cs:97-100).
14 _ = os.RemoveAll(j.state.Layout().Actions)
15 if err := os.MkdirAll(j.state.Layout().Actions, 0o755); err != nil {
16 return nil, err
17 }
18 j.own(j.state.Layout().Actions)
19 nodes, err := newNodes(j.jb.Steps, nil, "")
20 if err != nil {
21 return nil, err
22 }
23 j.actions.nodes = nodes
24 if err := j.prepare(j.ctx(), 1, nodes, 0, nil); err != nil {
25 return nil, j.setupError(err)
26 }
27 queue := j.setupSteps()
28 // JobExtension.cs:434-440: every runner step of the job gets a random "N"
29 // context name before anything runs; its final result, skipped included,
30 // is in the steps context. The ones the composite actions run get a "__"
31 // name (CompositeActionHandler.cs:121-123) and are not registered, which is
32 // why only this queue has ids.
33 for i := range queue {
34 queue[i].ctxID = newRunnerStepID()
35 }
36 // JobExtension.cs:317-331: the images of the actions are pulled first, then
37 // the containers start.
38 if j.cs != nil {
39 ic := j.initContainersStep()
40 ic.ctxID = newRunnerStepID()
41 queue = append(queue, ic)
42 }
43 for _, n := range nodes {
44 if j.actions.pre[n] {
45 queue = append(queue, qstep{n: n, stage: stagePre, cond: n.def.preIf()})
46 }
47 }
48 // The pre steps come before every main step: they were collected while
49 // walking the steps, the mains follow.
50 for _, n := range nodes {
51 queue = append(queue, qstep{n: n, stage: stageMain})
52 }
53 return queue, nil
54}
55
56// setupError words an error of the preparation like the runner reports it:
57// errors of the manifests were logged already; "Failed to resolve action
58// download info." and "Failed to download archive ..." carry the inner message.
59func (j *job) setupError(err error) error {
60 return err
61}