How dotenv Flow Interacts with CI / CD
Get the project source code below, and follow along with the lesson material.
Download Project Source CodeTo set up the project on your local machine, please follow the directions provided in the README.md
file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.
This lesson preview is part of the Scaling Web App Configuration with Environment Variables course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.
Get unlimited access to Scaling Web App Configuration with Environment Variables, plus 70+ \newline books, guides and courses with the \newline Pro subscription.

[00:00 - 00:15] In this lesson I'll answer, how do you run continuous integration tasks when you're using .env? When you're using continuous integration tools, you still have to figure out how to configure your application with environment variables.
[00:16 - 00:38] For those who don't know what CI and CD are, they're typical automated processes that happen after code is merged into a designated branch of Git that is identified as being for certified ready code. This can include anything from automated testing to building production ready code and reloading the server with it.
[00:39 - 00:47] One of the most common tools for this is CircleCI. To use it, we'll start off with our code from the last lesson.
[00:48 - 01:09] The way it works is that it takes code from a Git repo and then processes it every time there's a commit. So the code which this is in, the code repo that this is in, will be used as a place to run the tests from.
[01:10 - 01:27] But first we have to add some tests to it and then run it using our test runner . I'm going to use a popular one called Jest and PM install-d. Jest.
[01:28 - 01:53] Next we'll create a test script and add just to it. Now that we have our test runner, Jest, and we've added our test script, let's add a test folder and put a test in there.
[01:54 - 02:09] In this test, we import the file from index.js and we run the dbload function. Let's see what that looks like. To run it, you do npm test.
[02:10 - 02:28] It failed because the code was set up to work as a lambda function, but we're running it locally so the dot end variables are actually not brought in. The problem with this function is we actually have to be calling the handler since that's what's being exported.
[02:29 - 02:39] So we'll run handler. We'll import that from the file.
[02:40 - 02:55] And then there's two parameters, event, which isn't really being used. So we'll just put a blank ID there and then call a context, which needs output log string name.
[02:56 - 03:14] And when we run that, that should ask. Before I show you the circle CI interface, I will create a configuration file which is required.
[03:15 - 03:23] You create a folder. Create a folder.circle CI.
[03:24 - 03:36] In there, you create a yaml file config.yml. And this is the one that I'm using. I'll paste it in the description below.
[03:37 - 03:48] And then push your code to get. And then we'll see what happens. When we open circle CI and we set up the project, we'll see what happens.
[03:49 - 03:58] And it fails because we didn't set the environment variables. Let's go do that now. Project settings.
[03:59 - 04:12] Environment variables. Add in one variable. Once we've added the four environment variables, we can safely go back and re run it.
[04:13 - 04:25] So what we have to do is press rerun. After a while, I canceled it because I realized it was working for too long.
[04:26 - 04:35] So I pressed the cancel workflow button. The problem is that we didn't run test function. We actually ran the run function.
[04:36 - 04:51] So let's fix that in the code. What we have to do is open the config, run tests, and the command is npm test.
[04:52 - 05:14] And then we'll add that to get. We'll open that up on circle CI and see what happens.
[05:15 - 05:43] One last hint, if you are using .ncli to add any additional flags to whatever you're running, you put two dashes and a space. And since we have to close the Mongo client afterwards, I just do force exit. In the index added a Mongo client export, which should not be received as a handler.
[05:44 - 05:52] And in the test, I bring the handler in and I close it. And that's what happens when I push that.
[05:53 - 05:58] And success, the test pass. Thank you. And see you in the next one.