-
Notifications
You must be signed in to change notification settings - Fork 52
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
Throw error when requiredEnvVars are missing #38
Comments
Hi @swinton! Thank you for opening this - I really grappled with this decision. I'm happy to talk through it and properly I had it warn rather than throw because of tests. I didn't want users of the library to always need to define every one of those env vars just to make their tests pass. Also, its for running actions code locally - I can run my code as a Node process without having to set all those env vars (which would be cumbersome as inline CLI arguments, or you'd need Definitely open to opinions here - I'm not 100% convinced either way honestly. |
Thanks for taking a look here 🙇
Yeah, interesting point. I might be weird, but I typically mock out const tools = {
context: {
event: 'issues'
}
};
Toolkit.mockImplementation(() => {
return tools;
});
Interesting point. For what it's worth, when running locally I typically send in environment variables with #!/bin/sh
docker run \
-e HOME=/root \
-e GITHUB_WORKFLOW=main \
-e GITHUB_ACTION=my-github-action \
-e GITHUB_ACTOR=octocat \
-e GITHUB_REPOSITORY=swinton/example \
-e GITHUB_EVENT_NAME=issues \
-e GITHUB_EVENT_PATH=/test/fixtures/issues.closed.json \
-e GITHUB_WORKSPACE=/github/workspace \
-e GITHUB_SHA=0000000 \
-e GITHUB_REF=refs/heads/master \
-e GITHUB_TOKEN=$GITHUB_TOKEN \
my-github-action I know there's also
So, my slight preference is to throw when the environment is not setup as expected, as it sends a clearer signal for what went wrong / what I likely overlooked in my implementation (oops, I forgot to send in the environment variables, better whip up a script... kinda thing). Currently I'm working around this with something like: const getTools = () => {
const tools = new Toolkit();
if (typeof tools.context.event === 'undefined') {
throw new Error('GitHub Actions environment unavailable');
}
return tools;
}; But relying on |
Iiiiiiinteresting. Thanks for the clear description 🙏 The reliability of knowing that your implementation is correct makes a lot of sense.
I might steal this and put it into the bootstrap script - it looks really handy and would avoid a "why isn't my thing working oh its because I'm missing this env var." I'm convinced! Will make it throw 👍 possibly with an option to just warn in the constructor. |
@swinton 👋 I'm here to report that I reverted #41 in #70 😅 I discovered that some environment variables are not present in every event, so to depend on the existence of some but not all is as unreliable as none (that's confusing but I think it's correct). I also encountered some friction while using this in I'm going to close this issue because after investigating it and trying it out, I don't believe that the predictability is worth the friction, especially because |
Oh by the by @swinton - this may or may not work for your use-case, but you can define what events your Action is designed to be triggered by, which would help prevent |
Thanks for taking a look @JasonEtco. Makes sense! |
👋 @JasonEtco thanks for providing this utility, it's super helpful!
What are your thoughts on throwing an error (saying, e.g.
GitHub Actions environment unavailable
) when the required environment variables are missing? My thoughts are this would make it a little more explicit when the Actions environment isn't set, and would allow me to decide how to proceed (should I abort, or try and do something useful).actions-toolkit/src/index.ts
Lines 142 to 167 in eecccc3
The text was updated successfully, but these errors were encountered: