Skip to content
Snippets Groups Projects

Native Step Runner Integration for Docker Executor

Merged Axel von Bertoldi requested to merge avonbertoldi/47414/steps-integration-docker into main
2 files
+ 133
0
Compare changes
  • Side-by-side
  • Inline
Files
2
  • This function just transforms a Build into a RunRequest. Things to note:
    - This is where we stop the STEPS job variable.
    - The WorkDir and BuildDir are currently the same. We will consolidate
      the in the future according to step-runner!113 (comment 2146733345)
    - Env is superfluous and will be removed in the future.
    - Steps, (int the Build.Run field) come form GitLab in json format, but
      step-runner currently expects them in yaml, so we have to make the
      conversion here. We also add the empty step-spec preamble. Note that
      this will likely also change in the future when we compile steps
      before sending them to the step-runner.
steps/steps.go 0 → 100644
+ 65
0
package steps
import (
"encoding/json"
"fmt"
"strconv"
"gitlab.com/gitlab-org/gitlab-runner/common"
"gitlab.com/gitlab-org/step-runner/pkg/api/client"
"gitlab.com/gitlab-org/step-runner/schema/v1"
"gopkg.in/yaml.v2"
)
func NewRequest(build *common.Build) *client.RunRequest {
return &client.RunRequest{
Id: strconv.FormatInt(build.ID, 10),
WorkDir: build.FullProjectDir(),
BuildDir: build.FullProjectDir(),
Env: map[string]string{}, // TODO: Is this superflous???
Steps: addStepsPreamble(build.JobResponse.Run),
Variables: addVariables(build.GetAllVariables()),
}
}
var variablesToOmit = map[string]bool{
"STEPS": true,
}
func addVariables(vars common.JobVariables) []client.Variable {
result := make([]client.Variable, 0, len(vars))
for _, v := range vars {
if variablesToOmit[v.Key] {
continue
}
result = append(result, client.Variable{
Key: v.Key,
Value: v.Value,
File: v.File,
Masked: v.Masked,
})
}
return result
}
const stepsPreamble = "{}\n---\n"
// When using the run CI keyword, steps are written in yaml, parsed to json, validated, and finally sent over the wire
// (as json) to the runner. However the step-runner expects steps as yaml :-( Plus we have to add this here preamble.
// Syntactic correctness has already been ensured by GitLab so panicking here is OK since it's theoretically not
// possible for it to happen.
func addStepsPreamble(steps string) string {
def := &schema.Step{}
err := json.Unmarshal([]byte(steps), &def.Steps)
if err != nil {
panic(fmt.Errorf("unmarshalling steps %q: %w", steps, err))
}
runningSteps, err := yaml.Marshal(def)
if err != nil {
panic(fmt.Errorf("marshalling steps to yaml: %w", err))
}
return stepsPreamble + string(runningSteps)
}
Loading