Skip to content

Commit 8aa0bd3

Browse files
author
David Tabachnikov
committed
feat: add support for environment variables in Cloud Functions
1 parent 1e8dbb1 commit 8aa0bd3

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

package/lib/compileFunctions.js

+9
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ module.exports = {
4343
funcTemplate.properties.timeout = _.get(funcObject, 'timeout')
4444
|| _.get(this, 'serverless.service.provider.timeout')
4545
|| '60s';
46+
funcTemplate.properties.environmentVariables = _.merge(
47+
_.get(this, 'serverless.service.provider.environment'),
48+
funcObject.environment,
49+
);
50+
51+
if (!_.size(funcTemplate.properties.environmentVariables)) {
52+
delete funcTemplate.properties.environmentVariables;
53+
}
54+
4655
funcTemplate.properties.labels = _.assign({},
4756
_.get(this, 'serverless.service.provider.labels') || {},
4857
_.get(funcObject, 'labels') || {},

package/lib/compileFunctions.test.js

+80
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,86 @@ describe('CompileFunctions', () => {
360360
});
361361
});
362362

363+
it('should set the environment variables based on the function configuration', () => {
364+
googlePackage.serverless.service.functions = {
365+
func1: {
366+
handler: 'func1',
367+
environment: {
368+
TEST_VAR: 'test',
369+
},
370+
events: [
371+
{ http: 'foo' },
372+
],
373+
},
374+
};
375+
376+
const compiledResources = [{
377+
type: 'cloudfunctions.v1beta2.function',
378+
name: 'my-service-dev-func1',
379+
properties: {
380+
location: 'us-central1',
381+
runtime: 'nodejs8',
382+
function: 'func1',
383+
availableMemoryMb: 256,
384+
environmentVariables: {
385+
TEST_VAR: 'test',
386+
},
387+
timeout: '60s',
388+
sourceArchiveUrl: 'gs://sls-my-service-dev-12345678/some-path/artifact.zip',
389+
httpsTrigger: {
390+
url: 'foo',
391+
},
392+
labels: {},
393+
},
394+
}];
395+
396+
return googlePackage.compileFunctions().then(() => {
397+
expect(consoleLogStub.calledOnce).toEqual(true);
398+
expect(googlePackage.serverless.service.provider.compiledConfigurationTemplate.resources)
399+
.toEqual(compiledResources);
400+
});
401+
});
402+
403+
it('should set the environment variables based on the provider configuration', () => {
404+
googlePackage.serverless.service.functions = {
405+
func1: {
406+
handler: 'func1',
407+
events: [
408+
{ http: 'foo' },
409+
],
410+
},
411+
};
412+
googlePackage.serverless.service.provider.environment = {
413+
TEST_VAR: 'test',
414+
};
415+
416+
const compiledResources = [{
417+
type: 'cloudfunctions.v1beta2.function',
418+
name: 'my-service-dev-func1',
419+
properties: {
420+
location: 'us-central1',
421+
runtime: 'nodejs8',
422+
function: 'func1',
423+
availableMemoryMb: 256,
424+
environmentVariables: {
425+
TEST_VAR: 'test',
426+
},
427+
timeout: '60s',
428+
sourceArchiveUrl: 'gs://sls-my-service-dev-12345678/some-path/artifact.zip',
429+
httpsTrigger: {
430+
url: 'foo',
431+
},
432+
labels: {},
433+
},
434+
}];
435+
436+
return googlePackage.compileFunctions().then(() => {
437+
expect(consoleLogStub.calledOnce).toEqual(true);
438+
expect(googlePackage.serverless.service.provider.compiledConfigurationTemplate.resources)
439+
.toEqual(compiledResources);
440+
});
441+
});
442+
363443
it('should compile "http" events properly', () => {
364444
googlePackage.serverless.service.functions = {
365445
func1: {

0 commit comments

Comments
 (0)