Skip to content

Commit 0e052f3

Browse files
authored
fix: Add build env vars support for function deployment. (GoogleCloudPlatform#133)
* Add build env vars support for function deployment. * Format. * Format.
1 parent aef0190 commit 0e052f3

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

invoker/function-maven-plugin/src/main/java/com/google/cloud/functions/plugin/DeployFunction.java

+23
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,27 @@ public class DeployFunction extends CloudSdkMojo {
200200
*/
201201
@Parameter(alias = "deploy.envvarsfile", property = "function.deploy.envvarsfile")
202202
String envVarsFile;
203+
/**
204+
* List of key-value pairs to set as build environment variables. All existing environment variables
205+
* will be removed first.
206+
*/
207+
@Parameter(alias = "deploy.setbuildenvvars", property = "function.deploy.setbuildenvvars")
208+
Map<String, String> buildEnvironmentVariables;
209+
/**
210+
* Path to a local YAML file with definitions for all build environment variables. All existing
211+
* environment variables will be removed before the new environment variables are added.
212+
*/
213+
@Parameter(alias = "deploy.buildenvvarsfile", property = "function.deploy.buildenvvarsfile")
214+
String buildEnvVarsFile;
203215

204216
boolean hasEnvVariables() {
205217
return (this.environmentVariables != null && !this.environmentVariables.isEmpty());
206218
}
207219

220+
boolean hasBuildEnvVariables() {
221+
return (this.buildEnvironmentVariables != null && !this.buildEnvironmentVariables.isEmpty());
222+
}
223+
208224
// Select a downloaded Cloud SDK or a user defined Cloud SDK version.
209225
static Function<String, ManagedCloudSdk> newManagedSdkFactory() {
210226
return version -> {
@@ -331,6 +347,13 @@ public List<String> getCommands() {
331347
if (envVarsFile != null) {
332348
commands.add("--env-vars-file=" + envVarsFile);
333349
}
350+
if (hasBuildEnvVariables()) {
351+
Joiner.MapJoiner mapJoiner = Joiner.on(",").withKeyValueSeparator("=");
352+
commands.add("--set-build-env-vars=" + mapJoiner.join(buildEnvironmentVariables));
353+
}
354+
if (buildEnvVarsFile != null) {
355+
commands.add("--build-env-vars-file=" + buildEnvVarsFile);
356+
}
334357
commands.add("--runtime=" + runtime);
335358

336359
if (projectId != null) {

invoker/function-maven-plugin/src/test/java/com/google/cloud/functions/plugin/DeployFunctionTest.java

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class DeployFunctionTest {
1616
public void testDeployFunctionCommandLine() {
1717
DeployFunction mojo = new DeployFunction();
1818
mojo.envVarsFile = "myfile";
19+
mojo.buildEnvVarsFile = "myfile2";
1920
mojo.functionTarget = "function";
2021
mojo.ignoreFile = "ff";
2122
mojo.maxInstances = new Integer(3);
@@ -30,6 +31,7 @@ public void testDeployFunctionCommandLine() {
3031
mojo.triggerHttp = true;
3132
mojo.allowUnauthenticated = true;
3233
mojo.environmentVariables = ImmutableMap.of("env1", "a", "env2", "b");
34+
mojo.buildEnvironmentVariables = ImmutableMap.of("env1", "a", "env2", "b");
3335
List<String> expected =
3436
ImmutableList.of(
3537
"functions",
@@ -49,6 +51,8 @@ public void testDeployFunctionCommandLine() {
4951
"--max-instances=3",
5052
"--set-env-vars=env1=a,env2=b",
5153
"--env-vars-file=myfile",
54+
"--set-build-env-vars=env1=a,env2=b",
55+
"--build-env-vars-file=myfile2",
5256
"--runtime=java11");
5357
assertThat(mojo.getCommands()).isEqualTo(expected);
5458
}

0 commit comments

Comments
 (0)