Skip to content

Commit

Permalink
fix: Add build env vars support for function deployment. (GoogleCloud…
Browse files Browse the repository at this point in the history
…Platform#133)

* Add build env vars support for function deployment.

* Format.

* Format.
  • Loading branch information
ludoch committed Mar 24, 2022
1 parent aef0190 commit 0e052f3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,27 @@ public class DeployFunction extends CloudSdkMojo {
*/
@Parameter(alias = "deploy.envvarsfile", property = "function.deploy.envvarsfile")
String envVarsFile;
/**
* List of key-value pairs to set as build environment variables. All existing environment variables
* will be removed first.
*/
@Parameter(alias = "deploy.setbuildenvvars", property = "function.deploy.setbuildenvvars")
Map<String, String> buildEnvironmentVariables;
/**
* Path to a local YAML file with definitions for all build environment variables. All existing
* environment variables will be removed before the new environment variables are added.
*/
@Parameter(alias = "deploy.buildenvvarsfile", property = "function.deploy.buildenvvarsfile")
String buildEnvVarsFile;

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

boolean hasBuildEnvVariables() {
return (this.buildEnvironmentVariables != null && !this.buildEnvironmentVariables.isEmpty());
}

// Select a downloaded Cloud SDK or a user defined Cloud SDK version.
static Function<String, ManagedCloudSdk> newManagedSdkFactory() {
return version -> {
Expand Down Expand Up @@ -331,6 +347,13 @@ public List<String> getCommands() {
if (envVarsFile != null) {
commands.add("--env-vars-file=" + envVarsFile);
}
if (hasBuildEnvVariables()) {
Joiner.MapJoiner mapJoiner = Joiner.on(",").withKeyValueSeparator("=");
commands.add("--set-build-env-vars=" + mapJoiner.join(buildEnvironmentVariables));
}
if (buildEnvVarsFile != null) {
commands.add("--build-env-vars-file=" + buildEnvVarsFile);
}
commands.add("--runtime=" + runtime);

if (projectId != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class DeployFunctionTest {
public void testDeployFunctionCommandLine() {
DeployFunction mojo = new DeployFunction();
mojo.envVarsFile = "myfile";
mojo.buildEnvVarsFile = "myfile2";
mojo.functionTarget = "function";
mojo.ignoreFile = "ff";
mojo.maxInstances = new Integer(3);
Expand All @@ -30,6 +31,7 @@ public void testDeployFunctionCommandLine() {
mojo.triggerHttp = true;
mojo.allowUnauthenticated = true;
mojo.environmentVariables = ImmutableMap.of("env1", "a", "env2", "b");
mojo.buildEnvironmentVariables = ImmutableMap.of("env1", "a", "env2", "b");
List<String> expected =
ImmutableList.of(
"functions",
Expand All @@ -49,6 +51,8 @@ public void testDeployFunctionCommandLine() {
"--max-instances=3",
"--set-env-vars=env1=a,env2=b",
"--env-vars-file=myfile",
"--set-build-env-vars=env1=a,env2=b",
"--build-env-vars-file=myfile2",
"--runtime=java11");
assertThat(mojo.getCommands()).isEqualTo(expected);
}
Expand Down

0 comments on commit 0e052f3

Please sign in to comment.