Skip to content

Commit 216cc15

Browse files
MatousMarikl2ysho
andauthored
feat: add --apply-env-vars-to-build flag to push (#734) (#1345)
Part of #734 (the `applyEnvVarsToBuild` toggle half). ## What - New `--apply-env-vars-to-build` boolean flag on `apify push` — sets `applyEnvVarsToBuild: true` on the Actor version, so env vars from `actor.json` are also available during the Docker build. `--no-apply-env-vars-to-build` explicitly turns the setting off. - When the flag is omitted, the field is left out of the API payload entirely, so a value previously set in Console is **preserved** (pushes to existing Actors don't reset it). - Flag-parser tweak: `_parseFlags` matched provided flags by truthiness. The real CLI path is unaffected (`multiple: true` means values always arrive as arrays, which are truthy), but `testRunCommand`/`internalRunCommand` inject scalar values, where an explicit `false` was silently dropped. The matcher now checks for presence, and the required-flag check is aligned to `undefined`, so harness-injected negated booleans behave the same as parsed ones (the new test relies on this). - Regenerated `docs/` via `pnpm run update-docs`. ## Why Env vars synced by `apify push` currently apply only at runtime; making them apply to the build requires flipping a switch in Console. That breaks CI and fresh pushes of Actors that need build-time secrets (e.g. a GitHub token as a Docker build arg). ## Tests - New `[api]` test in `test/api/commands/push.test.ts` covering: flag → `true`, omitted → preserved, negated flag → `false`. Full push API suite passes (13/13), plus `test:local`, lint, format, build. ## Follow-ups (separate PRs) - `applyEnvVarsToBuild` field in `.actor/actor.json` (#1346, stacked on this). - `--env KEY=VALUE` passing on push (the other half of #734). No new dependencies; no install-size impact. Co-authored-by: Richard Solar <solar.richard@gmail.com>
1 parent d292f92 commit 216cc15

4 files changed

Lines changed: 63 additions & 4 deletions

File tree

‎docs/reference.md‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -788,8 +788,8 @@ DESCRIPTION
788788
789789
USAGE
790790
$ apify actors push [actorId] [--allow-missing-secrets]
791-
[-b <value>] [--dir <value>] [-f] [--json] [--open]
792-
[-v <value>] [-w <value>]
791+
[--apply-env-vars-to-build] [-b <value>] [--dir <value>]
792+
[-f] [--json] [--open] [-v <value>] [-w <value>]
793793
794794
ARGUMENTS
795795
actorId Name or ID of the Actor to push (e.g. "apify/hello-world" or
@@ -800,6 +800,11 @@ FLAGS
800800
--allow-missing-secrets Allow the command to
801801
continue even when secret values are not found in
802802
the local secrets storage.
803+
--apply-env-vars-to-build Make the environment
804+
variables also available to the Actor build
805+
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.
803808
-b, --build-tag=<value> Build tag to be
804809
applied to the successful Actor build. By default,
805810
it is taken from the '.actor/actor.json' file.

‎src/commands/actors/push.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,11 @@ export class ActorsPushCommand extends ApifyCommand<typeof ActorsPushCommand> {
223223
required: false,
224224
default: false,
225225
}),
226+
'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.',
229+
required: false,
230+
}),
226231
};
227232

228233
static override args = {
@@ -454,9 +459,11 @@ Skipping push. Use --force to override.`,
454459
allowMissing: this.flags.allowMissingSecrets,
455460
})
456461
: 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;
457464

458465
if (actorCurrentVersion) {
459-
const actorVersionModifier = { tarballUrl, sourceFiles, buildTag, sourceType, envVars };
466+
const actorVersionModifier = { tarballUrl, sourceFiles, buildTag, sourceType, envVars, applyEnvVarsToBuild };
460467
// TODO: fix this type too -.-
461468
await actorClient.version(version).update(actorVersionModifier as never);
462469
run({ message: `Updated version ${version} for Actor ${actor.name}.` });
@@ -468,6 +475,7 @@ Skipping push. Use --force to override.`,
468475
buildTag,
469476
sourceType,
470477
envVars,
478+
applyEnvVarsToBuild,
471479
};
472480

473481
await actorClient.versions().create({

‎src/lib/command-framework/apify-command.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ export abstract class ApifyCommand<T extends typeof BuiltApifyCommand = typeof B
523523

524524
let rawFlag = rawFlags[matchingFlags[0]];
525525

526-
if (!rawFlag && builderData.required) {
526+
if (typeof rawFlag === 'undefined' && builderData.required) {
527527
throw new CommandError({
528528
code: CommandErrorCode.APIFY_MISSING_FLAG,
529529
command: this.ctor,

‎test/api/commands/push.test.ts‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,52 @@ describe('[api] apify push', () => {
215215
TEST_TIMEOUT,
216216
);
217217

218+
it(
219+
'should set applyEnvVarsToBuild when the flag is passed and keep it when omitted',
220+
async () => {
221+
const testActor = await testUserClient.actors().create(TEST_ACTOR);
222+
actorsForCleanup.add(testActor.id);
223+
const testActorClient = testUserClient.actor(testActor.id);
224+
const actorJson = JSON.parse(readFileSync(joinPath(LOCAL_CONFIG_PATH), 'utf8'));
225+
226+
await testRunCommand(ActorsPushCommand, {
227+
args_actorId: testActor.id,
228+
flags_noPrompt: true,
229+
flags_force: true,
230+
flags_applyEnvVarsToBuild: true,
231+
});
232+
233+
const versionWithFlag = await testActorClient.version(actorJson.version).get();
234+
235+
await testRunCommand(ActorsPushCommand, {
236+
args_actorId: testActor.id,
237+
flags_noPrompt: true,
238+
flags_force: true,
239+
});
240+
241+
const versionWithoutFlag = await testActorClient.version(actorJson.version).get();
242+
243+
// false is what --no-apply-env-vars-to-build parses to
244+
await testRunCommand(ActorsPushCommand, {
245+
args_actorId: testActor.id,
246+
flags_noPrompt: true,
247+
flags_force: true,
248+
flags_applyEnvVarsToBuild: false,
249+
});
250+
251+
const versionWithNegatedFlag = await testActorClient.version(actorJson.version).get();
252+
253+
await testActorClient.delete();
254+
255+
expect(versionWithFlag!.applyEnvVarsToBuild).to.be.eql(true);
256+
// omitting the flag must preserve the value stored on the platform
257+
expect(versionWithoutFlag!.applyEnvVarsToBuild).to.be.eql(true);
258+
// the negated flag must actively turn the setting off
259+
expect(versionWithNegatedFlag!.applyEnvVarsToBuild).to.be.eql(false);
260+
},
261+
TEST_TIMEOUT,
262+
);
263+
218264
it(
219265
'should upload zip for source files larger that 3MB',
220266
async () => {

0 commit comments

Comments
 (0)