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

Add --write-env-file cli option #643

Merged
merged 3 commits into from
Jan 31, 2018
Merged

Add --write-env-file cli option #643

merged 3 commits into from
Jan 31, 2018

Conversation

DazWorrall
Copy link
Contributor

Partially addresses #615

This fulfils our desire to present only a clean environment to docker container (our runner gear can check for the presence of this file), and a future buildkite-agent env command can simply echo this back if it exists locally, before falling back to agent API.

WDYT @lox?

@lox
Copy link
Contributor

lox commented Jan 30, 2018

Hey @DazWorrall, thanks for taking the time to implement this and also being patient whilst we thought it over!

I know I suggested it initially, but reckon we should ditch the config option and just expose a env file to each job with it's path in BUILDKITE_ENV_FILE. Happy to collab, or if you want to have a shot at it, that would be amazing :)

@lox
Copy link
Contributor

lox commented Jan 30, 2018

(What you suggested in #615 (comment) 😓)

@lox
Copy link
Contributor

lox commented Jan 30, 2018

One question we've been talking back and forward on is whether you'd expect environmental changes from hooks to be reflected in this file. Thoughts?

@DazWorrall
Copy link
Contributor Author

We wouldn't I don't think - we're explicitly looking to see what was configured on the job (i.e. the equivalent of calling the jobs/env REST api), to provide a clean environment to a docker container. We may whitelist some other variables from the agent environment as well, but we still need to know what was given to the job.

@DazWorrall
Copy link
Contributor Author

@lox removed the config option.

// Prepare a file to recieve the given job environment
if file, err := ioutil.TempFile("", fmt.Sprintf("job-env-%s", runner.Job.ID)) ; err != nil {
return runner, err
} else {
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Without the else block file will be out of scope:

if file, err := ioutil.TempFile("", fmt.Sprintf("job-env-%s", runner.Job.ID)) ; err != nil {
    return runner, err
}
logger.Debug("[JobRunner] Created env file: %s", file.Name())
runner.envFile = file

Will fail to compile with agent/job_runner.go:73:51: undefined: file.

I could do:

file, err := foo()
if err != nil { }

But that doesn't match the style elsewhere.

Copy link
Contributor

Choose a reason for hiding this comment

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

But that doesn't match the style elsewhere.

Yup, that's fair. Match the style of the surrounding code and I'll deal with it holistically to match the golang style at some point.

if r.envFile != nil {
if err := os.Remove(r.envFile.Name()); err != nil {
logger.Warn("[JobRunner] Error cleaning up env file: %s", err)
} else {
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as above with else block.

@lox
Copy link
Contributor

lox commented Jan 30, 2018

Thanks @DazWorrall will talk over with @keithpitt!

@lox
Copy link
Contributor

lox commented Jan 31, 2018

Cool @keithpitt and I discussed it. I think we will subsequently write env updates to that file in our bootstrap, but that shouldn't affect you, or if it does you could take a snapshot of the env file in a bootstrap script or the env hook.

Beyond that, the only concern that popped up was how we'd handle newlines in env variables. Docker is notoriously bad at this moby/moby#12997 docker/compose#3527. This can be hacked around if needed at least.

I'm going to merge this and then do some testing. Thanks so much driving this discussion and for the contribution! We'll endeavour to get this out in a release soon.

@lox lox merged commit a72b61e into buildkite:master Jan 31, 2018
@DazWorrall DazWorrall deleted the env-file branch January 31, 2018 10:27
@DazWorrall
Copy link
Contributor Author

We already sanitise variables before passing them to docker FYI, not our first brush with newlines 🙂

@sander-m
Copy link

@lox Thanks for shipping this quickly. I have done some testing and we're running into issues with the newlines in environment variables (as you pointed out already). We have concluded that encoding the output file as JSON would solve our issues. That would make it trivial to parse for our use case.

@lox
Copy link
Contributor

lox commented Mar 14, 2018

Another option would be the export format we parse here https://github.com/buildkite/agent/blob/master/env/export.go

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants