Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support --no-interpolation option in pipeline upload #733

Merged
merged 3 commits into from
Apr 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions agent/pipeline_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ import (
)

type PipelineParser struct {
Env *env.Environment
Filename string
Pipeline []byte
Env *env.Environment
Filename string
Pipeline []byte
NoInterpolation bool
}

func (p PipelineParser) Parse() (interface{}, error) {
Expand All @@ -32,6 +33,15 @@ func (p PipelineParser) Parse() (interface{}, error) {
errPrefix = fmt.Sprintf("Failed to parse %s", p.Filename)
}

// If interpolation is disabled, just parse and return
if p.NoInterpolation {
var result interface{}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we may need to do the same logic as we do on line 50 (with the switch between a slice or a map?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only do that so that we can parseWithEnv if it's a map. Without interpolation I don't think we need to do that, do we?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmmmm. We might end up with out-of-order env if we do that though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But we always had that, it only mattered once we started doing interpolation.

if err := unmarshalAsStringMap([]byte(p.Pipeline), &result); err != nil {
return nil, fmt.Errorf("%s: %v", errPrefix, formatYAMLError(err))
}
return result, nil
}

var pipeline interface{}
var pipelineAsSlice []interface{}

Expand Down
12 changes: 12 additions & 0 deletions agent/pipeline_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ func TestPipelineParserParsesYaml(t *testing.T) {
assert.Equal(t, `{"steps":[{"label":"hello \"friend\""}]}`, string(j))
}

func TestPipelineParserParsesYamlWithNoInterpolation(t *testing.T) {
result, err := PipelineParser{
Filename: "awesome.yml",
Pipeline: []byte("steps:\n - label: \"hello ${ENV_VAR_FRIEND}\""),
NoInterpolation: true,
}.Parse()

assert.NoError(t, err)
j, err := json.Marshal(result)
assert.Equal(t, `{"steps":[{"label":"hello ${ENV_VAR_FRIEND}"}]}`, string(j))
}

func TestPipelineParserSupportsYamlMergesAndAnchors(t *testing.T) {
complexYAML := `---
base_step: &base_step
Expand Down
12 changes: 11 additions & 1 deletion clicommand/pipeline_upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type PipelineUploadConfig struct {
AgentAccessToken string `cli:"agent-access-token" validate:"required"`
Endpoint string `cli:"endpoint" validate:"required"`
NoColor bool `cli:"no-color"`
NoInterpolation bool `cli:"no-interpolation"`
Debug bool `cli:"debug"`
DebugHTTP bool `cli:"debug-http"`
}
Expand All @@ -70,6 +71,11 @@ var PipelineUploadCommand = cli.Command{
Usage: "The job that is making the changes to it's build",
EnvVar: "BUILDKITE_JOB_ID",
},
cli.BoolFlag{
Name: "no-interpolation",
Usage: "Skip variable interpolation the pipeline when uploaded",
EnvVar: "BUILDKITE_PIPELINE_NO_INTERPOLATION",
},
AgentAccessTokenFlag,
EndpointFlag,
NoColorFlag,
Expand Down Expand Up @@ -159,7 +165,11 @@ var PipelineUploadCommand = cli.Command{
var parsed interface{}

// Parse the pipeline
parsed, err = agent.PipelineParser{Filename: filename, Pipeline: input}.Parse()
parsed, err = agent.PipelineParser{
Filename: filename,
Pipeline: input,
NoInterpolation: cfg.NoInterpolation,
}.Parse()
if err != nil {
logger.Fatal("Pipeline parsing of \"%s\" failed (%s)", filename, err)
}
Expand Down