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

env-loader: Added gha-env writer #303

Merged
merged 1 commit into from
Dec 20, 2024
Merged

Conversation

fheinecke
Copy link
Contributor

This adds a new writer specifically for writing to GITHUB_ENV files. This format is close to dotenv files, but doesn't actually match it for multiline strings.

Here's an example output:

key1<<EOF
multiline
value1

EOF
key2=value2
key3<<EOF
multiline
value3
EOF

Here's what it looks like for dotenv:

key1=multiline
value1

key2=value2
key3=multiline
value3

As shown above, dotenv doesn't handle new lines at all, and errors in this case as shown here.

While this is more GHA-specific logic, it is a necessary evil. We'll need something like this for any pipeline service.

@fheinecke fheinecke requested a review from a team as a code owner December 19, 2024 23:23
Copy link
Contributor

@r0mant r0mant left a comment

Choose a reason for hiding this comment

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

I think the code can be greatly simplified.

// Generates a delimiter that is guaranteed to not contain the provided string.
// This is required for writing multiline values to `GITHUB_ENV`, per
// https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/workflow-commands-for-github-actions#multiline-strings
func generateMultilineDelimiter(value string) string {
Copy link
Contributor

Choose a reason for hiding this comment

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

Since you're using UUID in your delimiter string, the chance of collision is negligible and this function is needlessly complicated and can be reduced to literally just

func getDelimiter() string {
    return delimiterPrefix + "_" + uuid.NewString()
}

Maybe even no need for the function at all and just do it inline.

// Don't format strings without new lines as multiline. This would be valid, but a little
// less readable.
var renderedValue string
if strings.Contains(value.UnderlyingValue, "\n") {
Copy link
Contributor

Choose a reason for hiding this comment

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

Worth simplifying the logic and just using the multi-line format for everything?

@fheinecke
Copy link
Contributor Author

Sure, I can simplify this as above. It'll just make the output much more difficult to visually parse when it's actually used. For example, it will turn this output:

key1=value1
key2=value2
key3=value3
..
# Repeat a ~10 more times for some pipelines

into

key1<<EOF_4D48B7F5-0DE7-4B8D-9FAE-6B54D4F46214
value1
EOF_4D48B7F5-0DE7-4B8D-9FAE-6B54D4F46214
key2<<EOF_EDA7F489-F228-4E33-83D4-421CA2DF366E
value2
EOF_EDA7F489-F228-4E33-83D4-421CA2DF366E
key3<<EOF_E0577A69-8A63-4EA5-A4B1-30CD5A5D7923
value3
EOF_E0577A69-8A63-4EA5-A4B1-30CD5A5D7923
# Repeat a ~10 more times for some pipelines

@fheinecke fheinecke requested a review from r0mant December 20, 2024 05:43
@fheinecke
Copy link
Contributor Author

@r0mant let me know what you think and I'll get the tests fixed. Using UUIDs everywhere every time will add an lot of boilerplate + regex to the tests, so I wanted to get feedback before I spend time on it.

Copy link
Contributor

@r0mant r0mant left a comment

Choose a reason for hiding this comment

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

@fheinecke Yeah it does look kinda ugly (which in hindsight it was kinda obvious it would). Up to you if you want to revert that particular change, lgtm either way.

@fheinecke
Copy link
Contributor Author

I'm going to revert. If nothing else, this means that I can merge and move forward with the GHA env security project without spending more time on tests.

@fheinecke fheinecke force-pushed the fred/add-gha-env-writer-1 branch from 92bddbe to 5055ee5 Compare December 20, 2024 17:57
@fheinecke fheinecke merged commit 54d8272 into main Dec 20, 2024
15 checks passed
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.

2 participants