Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,30 @@ jobs:
non-empty value is given, the field values will be overwritten (not
merged). To remove all values, set the value to the literal string `{}`.

If both `env_vars` and `env_vars_file` are specified, the keys in
`env_vars` will take precedence over the keys in `env_vars_file`.
This is mutually-exclusive with `env_vars_file`.

- <a name="__input_env_vars_file"></a><a href="#user-content-__input_env_vars_file"><code>env_vars_file</code></a>: _(Optional)_ Path to a local file with definitions for all environment variables. This
is passed directly to the `--env-vars-file` flag of the underlying `gcloud
run deploy` command, which accepts either a YAML file or a `.env` file:

```yaml
# YAML file
env_vars_file: '/path/to/env.yaml'

# .env file
env_vars_file: '/path/to/production.env'
```

In a YAML file, each variable is a `KEY: VALUE` mapping. In a `.env` file,
each variable is a `KEY=VALUE` line.

Note that, consistent with `gcloud`, providing this file causes all
existing environment variables on the Cloud Run service to be removed
before the values from the file are applied (the file is authoritative).
For this reason, `env_vars_update_strategy` does not apply to
`env_vars_file`.

This is mutually-exclusive with `env_vars`.

- <a name="__input_env_vars_update_strategy"></a><a href="#user-content-__input_env_vars_update_strategy"><code>env_vars_update_strategy</code></a>: _(Required, default: `merge`)_ Controls how the environment variables are set on the Cloud Run service.
If set to "merge", then the environment variables are _merged_ with any
Expand Down
29 changes: 27 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,33 @@ inputs:
non-empty value is given, the field values will be overwritten (not
merged). To remove all values, set the value to the literal string `{}`.

If both `env_vars` and `env_vars_file` are specified, the keys in
`env_vars` will take precedence over the keys in `env_vars_file`.
This is mutually-exclusive with `env_vars_file`.
required: false

env_vars_file:
description: |-
Path to a local file with definitions for all environment variables. This
is passed directly to the `--env-vars-file` flag of the underlying `gcloud
run deploy` command, which accepts either a YAML file or a `.env` file:

```yaml
# YAML file
env_vars_file: '/path/to/env.yaml'

# .env file
env_vars_file: '/path/to/production.env'
```

In a YAML file, each variable is a `KEY: VALUE` mapping. In a `.env` file,
each variable is a `KEY=VALUE` line.

Note that, consistent with `gcloud`, providing this file causes all
existing environment variables on the Cloud Run service to be removed
before the values from the file are applied (the file is authoritative).
For this reason, `env_vars_update_strategy` does not apply to
`env_vars_file`.

This is mutually-exclusive with `env_vars`.
required: false

env_vars_update_strategy:
Expand Down
2 changes: 1 addition & 1 deletion dist/main/index.js

Large diffs are not rendered by default.

16 changes: 12 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import {
parseCSV,
parseFlags,
parseKVString,
parseKVStringAndFile,
pinnedToHeadWarning,
presence,
} from '@google-github-actions/actions-utils';
Expand Down Expand Up @@ -103,6 +102,7 @@ export async function run(): Promise<void> {
const gcloudVersion = await computeGcloudVersion(getInput('gcloud_version'));
const gcloudComponent = presence(getInput('gcloud_component')); // Cloud SDK component version
const envVars = getInput('env_vars'); // String of env vars KEY=VALUE,...
const envVarsFile = getInput('env_vars_file'); // Path to a file with env vars
const envVarsUpdateStrategy = getInput('env_vars_update_strategy') || 'merge';
const secrets = parseKVString(getInput('secrets')); // String of secrets KEY=VALUE,...
const secretsUpdateStrategy = getInput('secrets_update_strategy') || 'merge';
Expand Down Expand Up @@ -135,6 +135,9 @@ export async function run(): Promise<void> {
if (service && job) {
throw new Error('Only one of `service` or `job` inputs can be set.');
}
if (envVars && envVarsFile) {
throw new Error('Only one of `env_vars` or `env_vars_file` inputs can be set.');
}

// Validate gcloud component input
if (gcloudComponent && gcloudComponent !== 'alpha' && gcloudComponent !== 'beta') {
Expand Down Expand Up @@ -182,7 +185,7 @@ export async function run(): Promise<void> {
}

// Set optional flags from inputs
setEnvVarsFlags(deployCmd, envVars, envVarsUpdateStrategy);
setEnvVarsFlags(deployCmd, envVars, envVarsFile, envVarsUpdateStrategy);
setSecretsFlags(deployCmd, secrets, secretsUpdateStrategy);

if (wait) {
Expand Down Expand Up @@ -216,7 +219,7 @@ export async function run(): Promise<void> {
}

// Set optional flags from inputs
setEnvVarsFlags(deployCmd, envVars, envVarsUpdateStrategy);
setEnvVarsFlags(deployCmd, envVars, envVarsFile, envVarsUpdateStrategy);
setSecretsFlags(deployCmd, secrets, secretsUpdateStrategy);

if (tag) {
Expand Down Expand Up @@ -373,7 +376,12 @@ async function computeGcloudVersion(str: string): Promise<string> {
return str;
}

function setEnvVarsFlags(cmd: string[], envVars: string, strategy: string) {
function setEnvVarsFlags(cmd: string[], envVars: string, envVarsFile: string, strategy: string) {
if (envVarsFile) {
cmd.push('--env-vars-file', envVarsFile);
return;
}

const compiledEnvVars = parseKVString(envVars);
if (compiledEnvVars && Object.keys(compiledEnvVars).length > 0) {
let flag = '';
Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures/env_vars.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FOO: bar
ZIP: zap
27 changes: 27 additions & 0 deletions tests/unit/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,33 @@ test('#run', { concurrency: true }, async (suite) => {
assert.deepStrictEqual(envVars, { FOO: 'BAR' });
});

await suite.test('sets envvars from a file', async (t) => {
const mocks = defaultMocks(t.mock, {
service: 'my-test-service',
env_vars_file: 'tests/fixtures/env_vars.yaml',
});

await run();

const args = mocks.getExecOutput.mock.calls?.at(0)?.arguments?.at(1);
assertMembers(args, ['--env-vars-file', 'tests/fixtures/env_vars.yaml']);
});

await suite.test('fails if env_vars and env_vars_file are both provided', async (t) => {
defaultMocks(t.mock, {
service: 'my-test-service',
env_vars: 'FOO=BAR',
env_vars_file: 'tests/fixtures/env_vars.yaml',
});

await assert.rejects(
async () => {
await run();
},
{ message: /only one of .env_vars. or .env_vars_file. inputs can be set/ },
);
});

await suite.test('merges secrets', async (t) => {
const mocks = defaultMocks(t.mock, {
service: 'my-test-service',
Expand Down