Skip to content

Commit 6fd9afe

Browse files
authored
Add envs input to the plugin (#46)
The envs input enables us to selectively forward environment variables from Netlify CI to Nimbella Functions.
1 parent 2be76ce commit 6fd9afe

File tree

3 files changed

+44
-11
lines changed

3 files changed

+44
-11
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
The Nimbella add-on for Netlify is a Netlify Build Plugin that extends Netlify Sites with portable and stateful serverless functions using [Nimbella](https://nimbella.com/product/platform). The add-on enables developers to deploy sites to Netlify's CDN, and serverless functions and stateful APIs to the [Nimbella Cloud](https://nimbella.com).
1010

1111
The Nimbella add-on provides the following benefits.
12+
1213
1. **More runtimes:** implement functions in numerous languages including Python, Rust, Swift, Ruby, PHP, Java, Go, Node, and Deno.
1314
2. **Resource customization:** run functions for longer durations, and with more memory.
1415
3. **Support for key-value and object stores:** build stateful APIs, and handle images or files.
@@ -36,6 +37,7 @@ Learn more about the Nimbella add-on for Netlify [on our website](https://nimbel
3637

3738
Add the Nimbella add-on for Netlify to connect your Netlify site to Nimbella.
3839
To do that, run the following command from the base of your local project directory which is linked to your Netlify site.
40+
3941
```sh
4042
netlify addons:create nimbella
4143
```
@@ -123,10 +125,17 @@ package = "netlify-plugin-nimbella"
123125
+ functions = "functions" # Functions source directory. Use this if you would like to use Nimbella to deploy your functions.
124126
```
125127

128+
To be able to pass **environment variables** to functions, you should first set them as Netlify build environment variables and use the below plugin input to selectively forward the variables to functions deployed on Nimbella.
129+
130+
```toml
131+
[plugins.inputs]
132+
envs = ['ENV_ONE', 'ENV_TWO']
133+
```
134+
126135
This plugin builds your functions using a modified version of [netlify-lambda](https://github.com/netlify/netlify-lambda). You can get rid of any build steps you're performing on functions since the plugin handles it for you.
127136

128137
**Notes:**
129-
- None of environment variables present in the Netlify build runtime are made available to the deployed functions on Nimbella Cloud. _An enhancement to permit selective forwarding of environment variables is coming soon._
138+
130139
- Replace occurrences of `/.netlify/functions` in your API calls with `/.netlify/nimbella`, or use `/api`, as your API path instead.
131140
- All Netlify functions are deployed to a "default" package in your Nimbella namespace. The package name is required in the API path. For Netlify functions, the path will be `/.netlify/nimbella/default/` or `/api/default`. For named packages, replace `default` with your package name instead.
132141

index.js

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
const {existsSync} = require('fs');
2-
const {appendFile, readFile, readdir} = require('fs').promises;
2+
const {appendFile, readFile, readdir, writeFile} = require('fs').promises;
33
const {join} = require('path');
4+
const {homedir, tmpdir} = require('os');
45

56
const toml = require('@iarna/toml');
67
const cpx = require('cpx');
78
const build = require('netlify-lambda/lib/build');
89

910
const functionsBuildDir = `functions-build-${Date.now()}`;
10-
const nimConfig = join(require('os').homedir(), '.nimbella');
11+
const nimConfig = join(homedir(), '.nimbella');
1112
let netlifyToml = {};
1213
let isProject = false;
1314
let isActions = false;
@@ -28,22 +29,30 @@ async function deployProject(run) {
2829
* @param {function} run - function provided under utils by Netlify to build event functions.
2930
* @param {string} functionsDir - Path to the actions directory.
3031
*/
31-
async function deployActions({run, functionsDir, timeout, memory}) {
32+
async function deployActions({run, functionsDir, timeout, memory, envsFile}) {
3233
const files = await readdir(functionsDir);
34+
if (!existsSync(envsFile)) {
35+
envsFile = false;
36+
}
3337

3438
await Promise.all(
3539
files.map(async (file) => {
3640
const [actionName, extension] = file.split('.');
37-
let command =
38-
`nim action update ${actionName} ${join(functionsDir, file)} ` +
39-
`--timeout=${Number(timeout)} --memory=${Number(memory)} ` +
40-
`--web=raw `;
41+
const command = [
42+
`nim action update ${actionName} ${join(functionsDir, file)}`,
43+
`--timeout=${Number(timeout)} --memory=${Number(memory)} `,
44+
`--web=raw`
45+
];
4146

4247
if (extension === 'js') {
43-
command += '--kind nodejs-lambda:10 --main handler';
48+
command.push('--kind nodejs-lambda:10 --main handler');
49+
}
50+
51+
if (envsFile) {
52+
command.push(`--env-file=${envsFile}`);
4453
}
4554

46-
const {stderr, exitCode, failed} = await run.command(command, {
55+
const {stderr, exitCode, failed} = await run.command(command.join(' '), {
4756
reject: false,
4857
stdout: 'ignore'
4958
});
@@ -132,8 +141,20 @@ module.exports = {
132141
if (isActions) {
133142
console.log('\n------------------Functions------------------\n');
134143
try {
144+
if (inputs.envs.length > 0) {
145+
const envs = {};
146+
let envMessage = 'Forwarded the following environment variables: ';
147+
inputs.envs.forEach((env, index) => {
148+
envs[env] = process.env[env];
149+
envMessage += index === inputs.envs.length - 1 ? env : env + ', ';
150+
});
151+
await writeFile(join(tmpdir(), 'env.json'), JSON.stringify(envs));
152+
console.log(envMessage);
153+
}
154+
135155
await deployActions({
136156
run: utils.run,
157+
envsFile: join(tmpdir(), 'env.json'),
137158
functionsDir: functionsBuildDir,
138159
timeout: inputs.timeout, // Default is 6 seconds
139160
memory: inputs.memory // Default is 256MB (max for free tier)

manifest.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ inputs:
88
- name: memory
99
description: Maximum memory LIMIT in MB for the function (functions input value is required to utilize this option).
1010
default: 256
11+
- name: envs
12+
description: Specify the names of the environment variables that need to be forwarded from Netlify CI to Nimbella Functions (functions input value is required to utilize this option).
13+
default: []
1114
- name: path
1215
description: The API prefix path you would like to use to access your functions.
13-
default: '/api/'
16+
default: '/api/'

0 commit comments

Comments
 (0)