-
Notifications
You must be signed in to change notification settings - Fork 7
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 descriptive error message for absent .env #1546
Merged
+41
−28
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
DB_DISABLE_SSL= | ||
DB_ENABLE_SSL= | ||
DB_NAME= | ||
DB_PASSWORD= | ||
DB_URL= | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
DB_DISABLE_SSL= | ||
DB_ENABLE_SSL= | ||
DB_NAME= | ||
DB_PASSWORD= | ||
DB_URL= | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
DB_DISABLE_SSL= | ||
DB_ENABLE_SSL= | ||
DB_NAME= | ||
DB_PASSWORD= | ||
DB_URL= | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,27 +3,40 @@ | |
* Copyright (c) 2017 Beyond Essential Systems Pty Ltd | ||
*/ | ||
|
||
// Note: Must use function to guarantee environment variables have loaded. | ||
const getServerConfig = () => ({ | ||
host: process.env.DB_URL, | ||
user: process.env.DB_USER, | ||
password: process.env.DB_PASSWORD, | ||
database: process.env.DB_NAME, | ||
ssl: | ||
process.env.DB_ENABLE_SSL === 'true' | ||
? { | ||
// Test server cannot turn on ssl, so sets the env to disable it | ||
rejectUnauthorized: false, | ||
} | ||
: null, | ||
}); | ||
|
||
const getCiConfig = () => ({ | ||
host: process.env.CI_TEST_DB_URL, | ||
user: process.env.CI_TEST_DB_USER, | ||
password: process.env.CI_TEST_DB_PASSWORD, | ||
database: process.env.CI_TEST_DB_NAME, | ||
ssl: null, | ||
}); | ||
|
||
const validateEnv = () => { | ||
['DB_URL', 'DB_USER', 'DB_PASSWORD', 'DB_NAME'].forEach(requiredField => { | ||
if (!process.env[requiredField]) { | ||
throw new Error( | ||
`Invalid database connection configuration: please specify '${requiredField}' in a .env file`, | ||
); | ||
} | ||
}); | ||
}; | ||
|
||
export const getConnectionConfig = () => { | ||
return process.env.CI_NAME === 'codeship' | ||
? { | ||
host: process.env.CI_TEST_DB_URL, | ||
user: process.env.CI_TEST_DB_USER, | ||
password: process.env.CI_TEST_DB_PASSWORD, | ||
database: process.env.CI_TEST_DB_NAME, | ||
ssl: null, | ||
} | ||
: { | ||
host: process.env.DB_URL, | ||
user: process.env.DB_USER, | ||
password: process.env.DB_PASSWORD, | ||
database: process.env.DB_NAME, | ||
ssl: | ||
process.env.DB_DISABLE_SSL === 'true' | ||
? null | ||
: { | ||
// Test server cannot turn on ssl, so sets the env to disable it | ||
rejectUnauthorized: false, | ||
}, | ||
}; | ||
validateEnv(); | ||
// Note: Must use functions to guarantee environment variables have loaded | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed this is the case... In previous commits, I removed the comment (and used This functionality now fails if functions are not used to retrieve the |
||
return process.env.CI_NAME === 'codeship' ? getCiConfig() : getServerConfig(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if we should include this optional field in the examples or just skip it, I chose to include it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I think it's good to include for completeness. Please add a note that the release process needs to include some manual changes to environment variables (the process is somewhat arduous)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will add it (by the way I am the release manager this week). Could you explain the process to me in person (or point me to related documentation)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For sure. This is the best documentation so far, but I will take you through it in person and we can add a doc just for that process
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://docs.beyondessential.com.au/books/dev-ops/page/tupaia-main-server-structure-overview#bkmrk-environment-variable