Skip to content

Commit 4e9f83d

Browse files
MatousMarikszaganekclaudel2ysho
authored
feat: read applyEnvVarsToBuild from actor.json in push (#734) (#1346)
Part of #734, stacked on #1345 (retarget to `master` once that merges — only the last commit is new here). ## What - `apify push` now reads `applyEnvVarsToBuild` from `.actor/actor.json`, so the setting persists per repo and applies on every push (useful for CI and Actors needing build-time secrets). - Precedence: `--[no-]apply-env-vars-to-build` flag > `applyEnvVarsToBuild` in actor.json > omitted (value stored on the platform is kept). An explicit `false` in actor.json turns the setting off. - Documented in `docs/vars.md`; flag description updated (+ regenerated `docs/reference.md`). ## Tests - New `[api]` test covering field `true`, field `false`, flag-over-field precedence, and the negated flag beating a `true` in the file (the case that pins `??` over `||`). Full push API suite passes (14/14), plus `test:local`, lint, format, build. - Note for reviewers: `useActorConfig` caches by cwd, so the test resets the cache when rewriting actor.json mid-test. ## Follow-up (separate PR) - `--env KEY=VALUE` passing on push (the other half of #734). No new dependencies; no install-size impact. --------- Co-authored-by: Edyta <142720610+szaganek@users.noreply.github.com> Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com> Co-authored-by: Richard Solár <solar.richard@gmail.com>
1 parent e9fb806 commit 4e9f83d

4 files changed

Lines changed: 99 additions & 6 deletions

File tree

docs/reference.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -803,8 +803,11 @@ FLAGS
803803
--apply-env-vars-to-build Make the environment
804804
variables also available to the Actor build
805805
process. Use --no-apply-env-vars-to-build to turn
806-
the setting off. When omitted, the setting
807-
currently stored on the platform is kept.
806+
the setting off. Overrides the value of the
807+
'applyEnvVarsToBuild' field in the
808+
'.actor/actor.json' file. When both the field and
809+
the flag are omitted, the setting currently stored
810+
on the platform is kept.
808811
-b, --build-tag=<value> Build tag to be
809812
applied to the successful Actor build. By default,
810813
it is taken from the '.actor/actor.json' file.

docs/vars.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,22 @@ You can use the CLI to manage secrets environment variables:
7474
...
7575
}
7676
```
77+
78+
### Apply environment variables to the build
79+
80+
By default, custom environment variables are available only at runtime. To make them available also to the Actor build process, for example, as Docker build arguments, set `applyEnvVarsToBuild` in `.actor/actor.json`:
81+
82+
```json
83+
{
84+
"actorSpecification": 1,
85+
"name": "dataset-to-mysql",
86+
"version": "0.1",
87+
"buildTag": "latest",
88+
"applyEnvVarsToBuild": true,
89+
"environmentVariables": {
90+
"MYSQL_PASSWORD": "@mySecretPassword"
91+
}
92+
}
93+
```
94+
95+
To apply the environment variables to a single push, add the `--apply-env-vars-to-build` flag to the `apify push` command. To turn off the setting for a single push, add the `--no-apply-env-vars-to-build` flag. The flag overrides the value of the `applyEnvVarsToBuild` field. If you use neither the field nor a flag, the Apify platform keeps the stored setting.

src/commands/actors/push.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,7 @@ export class ActorsPushCommand extends ApifyCommand<typeof ActorsPushCommand> {
224224
default: false,
225225
}),
226226
'apply-env-vars-to-build': Flags.boolean({
227-
description:
228-
'Make the environment variables also available to the Actor build process. Use --no-apply-env-vars-to-build to turn the setting off. When omitted, the setting currently stored on the platform is kept.',
227+
description: `Make the environment variables also available to the Actor build process. Use --no-apply-env-vars-to-build to turn the setting off. Overrides the value of the 'applyEnvVarsToBuild' field in the '${LOCAL_CONFIG_PATH}' file. When both the field and the flag are omitted, the setting currently stored on the platform is kept.`,
229228
required: false,
230229
}),
231230
};
@@ -459,8 +458,10 @@ Skipping push. Use --force to override.`,
459458
allowMissing: this.flags.allowMissingSecrets,
460459
})
461460
: undefined;
462-
// true/false when --[no-]apply-env-vars-to-build is passed, undefined when omitted so the value stored on the platform is preserved
463-
const { applyEnvVarsToBuild } = this.flags;
461+
// The flag wins when passed, then the actor.json field; undefined when neither is set, so the value
462+
// stored on the platform is preserved
463+
const applyEnvVarsToBuild =
464+
this.flags.applyEnvVarsToBuild ?? (actorConfig!.applyEnvVarsToBuild as boolean | undefined);
464465

465466
if (actorCurrentVersion) {
466467
const actorVersionModifier = { tarballUrl, sourceFiles, buildTag, sourceType, envVars, applyEnvVarsToBuild };

test/api/commands/push.test.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,76 @@ describe('[api] apify push', () => {
261261
TEST_TIMEOUT,
262262
);
263263

264+
it(
265+
'should read applyEnvVarsToBuild from actor.json, with the flag taking precedence',
266+
async () => {
267+
const testActor = await testUserClient.actors().create(TEST_ACTOR);
268+
actorsForCleanup.add(testActor.id);
269+
const testActorClient = testUserClient.actor(testActor.id);
270+
const actorJson = JSON.parse(readFileSync(joinPath(LOCAL_CONFIG_PATH), 'utf8'));
271+
272+
try {
273+
actorJson.applyEnvVarsToBuild = true;
274+
writeFileSync(joinPath(LOCAL_CONFIG_PATH), JSON.stringify(actorJson, null, '\t'), { flag: 'w' });
275+
276+
await testRunCommand(ActorsPushCommand, {
277+
args_actorId: testActor.id,
278+
flags_noPrompt: true,
279+
flags_force: true,
280+
});
281+
282+
const versionWithFieldTrue = await testActorClient.version(actorJson.version).get();
283+
284+
actorJson.applyEnvVarsToBuild = false;
285+
writeFileSync(joinPath(LOCAL_CONFIG_PATH), JSON.stringify(actorJson, null, '\t'), { flag: 'w' });
286+
// the actor config is cached per cwd, so mid-test rewrites need a reset
287+
resetCwdCaches();
288+
289+
await testRunCommand(ActorsPushCommand, {
290+
args_actorId: testActor.id,
291+
flags_noPrompt: true,
292+
flags_force: true,
293+
});
294+
295+
const versionWithFieldFalse = await testActorClient.version(actorJson.version).get();
296+
297+
// the file still says false, but the flag must win
298+
await testRunCommand(ActorsPushCommand, {
299+
args_actorId: testActor.id,
300+
flags_noPrompt: true,
301+
flags_force: true,
302+
flags_applyEnvVarsToBuild: true,
303+
});
304+
305+
const versionWithFlagOverride = await testActorClient.version(actorJson.version).get();
306+
307+
// and the negated flag must also win over a true in the file
308+
actorJson.applyEnvVarsToBuild = true;
309+
writeFileSync(joinPath(LOCAL_CONFIG_PATH), JSON.stringify(actorJson, null, '\t'), { flag: 'w' });
310+
resetCwdCaches();
311+
312+
await testRunCommand(ActorsPushCommand, {
313+
args_actorId: testActor.id,
314+
flags_noPrompt: true,
315+
flags_force: true,
316+
flags_applyEnvVarsToBuild: false,
317+
});
318+
319+
const versionWithNegatedFlagOverride = await testActorClient.version(actorJson.version).get();
320+
321+
expect(versionWithFieldTrue!.applyEnvVarsToBuild).to.be.eql(true);
322+
expect(versionWithFieldFalse!.applyEnvVarsToBuild).to.be.eql(false);
323+
expect(versionWithFlagOverride!.applyEnvVarsToBuild).to.be.eql(true);
324+
expect(versionWithNegatedFlagOverride!.applyEnvVarsToBuild).to.be.eql(false);
325+
} finally {
326+
delete actorJson.applyEnvVarsToBuild;
327+
writeFileSync(joinPath(LOCAL_CONFIG_PATH), JSON.stringify(actorJson, null, '\t'), { flag: 'w' });
328+
await testActorClient.delete();
329+
}
330+
},
331+
TEST_TIMEOUT,
332+
);
333+
264334
it(
265335
'should upload zip for source files larger that 3MB',
266336
async () => {

0 commit comments

Comments
 (0)