Skip to content

Commit 298c0db

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

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

package/lib/compileFunctions.js

+8
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ module.exports = {
4343
funcTemplate.properties.timeout = _.get(funcObject, 'timeout')
4444
|| _.get(this, 'serverless.service.provider.timeout')
4545
|| '60s';
46+
funcTemplate.properties.environmentVariables = _.get(funcObject, 'environment')
47+
|| _.get(this, 'serverless.service.provider.environment')
48+
|| null;
49+
50+
if (funcTemplate.properties.environmentVariables === null) {
51+
delete funcTemplate.properties.environmentVariables;
52+
}
53+
4654
funcTemplate.properties.labels = _.assign({},
4755
_.get(this, 'serverless.service.provider.labels') || {},
4856
_.get(funcObject, 'labels') || {},

package/lib/compileFunctions.test.js

+40
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,46 @@ describe('CompileFunctions', () => {
133133
});
134134
});
135135

136+
it('should set the environment variables based on the provider configuration', () => {
137+
googlePackage.serverless.service.functions = {
138+
func1: {
139+
handler: 'func1',
140+
events: [
141+
{ http: 'foo' },
142+
],
143+
},
144+
};
145+
googlePackage.serverless.service.provider.environment = {
146+
TEST_VAR: 'test',
147+
};
148+
149+
const compiledResources = [{
150+
type: 'cloudfunctions.v1beta2.function',
151+
name: 'my-service-dev-func1',
152+
properties: {
153+
location: 'us-central1',
154+
runtime: 'nodejs8',
155+
function: 'func1',
156+
availableMemoryMb: 256,
157+
environmentVariables: {
158+
TEST_VAR: 'test',
159+
},
160+
timeout: '60s',
161+
sourceArchiveUrl: 'gs://sls-my-service-dev-12345678/some-path/artifact.zip',
162+
httpsTrigger: {
163+
url: 'foo',
164+
},
165+
labels: {},
166+
},
167+
}];
168+
169+
return googlePackage.compileFunctions().then(() => {
170+
expect(consoleLogStub.calledOnce).toEqual(true);
171+
expect(googlePackage.serverless.service.provider.compiledConfigurationTemplate.resources)
172+
.toEqual(compiledResources);
173+
});
174+
});
175+
136176
it('should set the memory size based on the provider configuration', () => {
137177
googlePackage.serverless.service.functions = {
138178
func1: {

0 commit comments

Comments
 (0)