Skip to content

Commit 84c878f

Browse files
authored
Add support for env vars from a file (#413)
- Closes #385 - Closes #234
1 parent f6e4cdb commit 84c878f

File tree

10 files changed

+111
-71
lines changed

10 files changed

+111
-71
lines changed

.github/workflows/integration.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,20 @@ jobs:
6161
env_vars: |-
6262
FOO=bar
6363
ZIP=zap
64+
env_vars_file: './tests/fixtures/env_vars.txt'
6465
secrets: |-
6566
MY_SECRET=secret_value:latest
6667
MY_SECOND_SECRET=new_value:latest
6768
labels: |-
6869
label1=value1
6970
label2=value2
70-
flags: "--cpu=2 --concurrency=20"
71+
flags: '--cpu=2 --concurrency=20'
7172

7273
- run: 'npm run e2e-tests'
7374
env:
7475
PROJECT_ID: ${{ secrets.DEPLOY_CLOUDRUN_PROJECT_ID }}
7576
SERVICE: '${{ env.SERVICE_NAME }}'
76-
ENV: 'FOO=bar,ZIP=zap'
77+
ENV: 'FOO=bar,ZIP=zap,TEXT_FOO=bar,TEXT_ZIP=zap'
7778
SECRET_ENV: MY_SECRET=secret_value:latest,MY_SECOND_SECRET=new_value:latest
7879
PARAMS: '{"cpu":2, "containerConcurrency":20}'
7980
LABELS: '{"label1":"value1", "label2":"value2", "commit-sha":"${{ github.sha }}", "managed-by":"github-actions"}'
@@ -94,7 +95,7 @@ jobs:
9495
env:
9596
PROJECT_ID: ${{ secrets.DEPLOY_CLOUDRUN_PROJECT_ID }}
9697
SERVICE: '${{ env.SERVICE_NAME }}'
97-
ENV: 'FOO=bar,ZIP=zap,ABC=123,DEF=456'
98+
ENV: 'FOO=bar,ZIP=zap,TEXT_FOO=bar,TEXT_ZIP=zap,ABC=123,DEF=456'
9899
SECRET_ENV: MY_SECRET=secret_value:latest,MY_SECOND_SECRET=new_value:latest
99100
SECRET_VOLUMES: /api/secrets/my-secret=secret_value:latest
100101
PARAMS: '{"cpu":2, "containerConcurrency":20}'

README.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ jobs:
8080
default value is no suffix.
8181

8282
- `env_vars`: (Optional) List of key=value pairs to set as environment
83-
variables. All existing environment variables will be retained.
83+
variables. All existing environment variables will be retained. If both
84+
`env_vars` and `env_vars_file` are specified, the keys in env_vars will take
85+
precendence over the keys in env_vars_files.
8486

8587
```yaml
8688
with:
@@ -89,6 +91,33 @@ jobs:
8991
ZIP=zap
9092
```
9193

94+
- `env_vars_file`: (Optional) Path to a file on disk, relative to the
95+
workspace, that defines environment variables. The file can be
96+
newline-separated KEY=VALUE pairs, JSON, or YAML format. If both `env_vars`
97+
and `env_vars_file` are specified, the keys in env_vars will take
98+
precendence over the keys in env_vars_files.
99+
100+
```text
101+
FOO=bar
102+
ZIP=zap
103+
```
104+
105+
or
106+
107+
```json
108+
{
109+
"FOO": "bar",
110+
"ZIP": "zap"
111+
}
112+
```
113+
114+
or
115+
116+
```yaml
117+
FOO: 'bar'
118+
ZIP: 'zap'
119+
```
120+
92121
- `secrets`: (Optional) List of key=value pairs to use as secrets. These can
93122
either be injected as environment variables or mounted as volumes. All
94123
existing environment secrets and volume mounts will be retained.

action.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@ inputs:
4040
description: |-
4141
List of key-value pairs to set as environment variables in the format:
4242
KEY1=VALUE1,KEY2=VALUE2. Existing environment variables will be retained.
43+
If both "env_vars" and "env_vars_file" are specified, the keys in
44+
"env_vars" will take precendence over the keys in "env_vars_files".
45+
required: false
46+
47+
env_vars_file:
48+
description: |-
49+
Path to a file on disk, relative to the workspace, that defines
50+
environment variables. The file can be newline-separated KEY=VALUE pairs,
51+
JSON, or YAML format. If both "env_vars" and "env_vars_file" are
52+
specified, the keys in "env_vars" will take precendence over the keys in
53+
"env_vars_files".
4354
required: false
4455

4556
secrets:

dist/main/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,12 @@
3232
"@actions/core": "^1.10.0",
3333
"@actions/exec": "^1.1.1",
3434
"@actions/tool-cache": "^2.0.1",
35-
"@google-github-actions/actions-utils": "^0.4.3",
35+
"@google-github-actions/actions-utils": "^0.4.4",
3636
"@google-github-actions/setup-cloud-sdk": "^1.0.1"
3737
},
3838
"devDependencies": {
3939
"@types/chai": "^4.3.4",
4040
"@types/js-yaml": "^4.0.5",
41-
"@types/lodash": "^4.14.190",
4241
"@types/mocha": "^10.0.1",
4342
"@types/node": "^18.11.9",
4443
"@types/sinon": "^10.0.13",
@@ -50,7 +49,6 @@
5049
"eslint-config-prettier": "^8.5.0",
5150
"eslint-plugin-prettier": "^4.2.1",
5251
"googleapis": "^109.0.1",
53-
"lodash": "^4.17.21",
5452
"mocha": "^10.1.0",
5553
"prettier": "^2.8.0",
5654
"sinon": "^15.0.0",

src/main.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {
3333
KVPair,
3434
parseFlags,
3535
parseKVString,
36+
parseKVStringAndFile,
3637
pinnedToHeadWarning,
3738
presence,
3839
} from '@google-github-actions/actions-utils';
@@ -86,7 +87,8 @@ export async function run(): Promise<void> {
8687
const projectId = getInput('project_id');
8788
const gcloudVersion = await computeGcloudVersion(getInput('gcloud_version'));
8889
const gcloudComponent = presence(getInput('gcloud_component')); // Cloud SDK component version
89-
const envVars = parseKVString(getInput('env_vars')); // String of env vars KEY=VALUE,...
90+
const envVars = getInput('env_vars'); // String of env vars KEY=VALUE,...
91+
const envVarsFile = getInput('env_vars_file'); // File that is a string of env vars KEY=VALUE,...
9092
const secrets = parseKVString(getInput('secrets')); // String of secrets KEY=VALUE,...
9193
const region = getInput('region') || 'us-central1';
9294
const source = getInput('source'); // Source directory
@@ -132,7 +134,7 @@ export async function run(): Promise<void> {
132134
image: image !== '',
133135
metadata: metadata !== '',
134136
source: source !== '',
135-
env_vars: Object.keys(envVars).length > 0,
137+
env_vars: envVars !== '',
136138
no_traffic: noTraffic,
137139
secrets: Object.keys(secrets).length > 0,
138140
suffix: suffix !== '',
@@ -152,7 +154,7 @@ export async function run(): Promise<void> {
152154
image: image !== '',
153155
service: service !== '',
154156
source: source !== '',
155-
env_vars: Object.keys(envVars).length > 0,
157+
env_vars: envVars !== '',
156158
no_traffic: noTraffic,
157159
secrets: Object.keys(secrets).length > 0,
158160
suffix: suffix !== '',
@@ -179,8 +181,9 @@ export async function run(): Promise<void> {
179181
}
180182

181183
// Set optional flags from inputs
182-
if (envVars && Object.keys(envVars).length > 0) {
183-
cmd.push('--update-env-vars', kvToString(envVars));
184+
const compiledEnvVars = parseKVStringAndFile(envVars, envVarsFile);
185+
if (compiledEnvVars && Object.keys(compiledEnvVars).length > 0) {
186+
cmd.push('--update-env-vars', kvToString(compiledEnvVars));
184187
}
185188
if (secrets && Object.keys(secrets).length > 0) {
186189
cmd.push('--update-secrets', kvToString(secrets));

0 commit comments

Comments
 (0)