Skip to content
Draft
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
6 changes: 4 additions & 2 deletions docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -774,8 +774,10 @@ FLAGS
the local secrets storage.
--apply-env-vars-to-build Make the environment
variables also available to the Actor build
process. When omitted, the setting currently stored
on the platform is kept.
process. Overrides the 'applyEnvVarsToBuild' field
in the '.actor/actor.json' file. When both are
omitted, the setting currently stored on the
platform is kept.
-b, --build-tag=<value> Build tag to be
applied to the successful Actor build. By default,
it is taken from the '.actor/actor.json' file.
Expand Down
19 changes: 19 additions & 0 deletions docs/vars.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,22 @@ You can use the CLI to manage secrets environment variables:
...
}
```

### Apply environment variables to the build

By default, custom environment variables are available only at runtime. To also make them available to the Actor build process (for example, as Docker build arguments), set `applyEnvVarsToBuild` in `.actor/actor.json`:

```json
{
"actorSpecification": 1,
"name": "dataset-to-mysql",
"version": "0.1",
"buildTag": "latest",
"applyEnvVarsToBuild": true,
"environmentVariables": {
"MYSQL_PASSWORD": "@mySecretPassword"
}
}
```

Alternatively, pass the `--apply-env-vars-to-build` flag to `apify push` for a one-off push. The flag overrides the `applyEnvVarsToBuild` field. When both are omitted, the setting currently stored on the Apify platform is kept.
8 changes: 4 additions & 4 deletions src/commands/actors/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,7 @@ export class ActorsPushCommand extends ApifyCommand<typeof ActorsPushCommand> {
default: false,
}),
'apply-env-vars-to-build': Flags.boolean({
description:
'Make the environment variables also available to the Actor build process. When omitted, the setting currently stored on the platform is kept.',
description: `Make the environment variables also available to the Actor build process. Overrides the 'applyEnvVarsToBuild' field in the '${LOCAL_CONFIG_PATH}' file. When both are omitted, the setting currently stored on the platform is kept.`,
required: false,
}),
};
Expand Down Expand Up @@ -411,8 +410,9 @@ Skipping push. Use --force to override.`,
allowMissing: this.flags.allowMissingSecrets,
})
: undefined;
// undefined when the flag is omitted, so the value stored on the platform is preserved
const applyEnvVarsToBuild = this.flags.applyEnvVarsToBuild || undefined;
// undefined when neither the flag nor the actor.json field is set, so the value stored on the platform is preserved
const applyEnvVarsToBuild =
this.flags.applyEnvVarsToBuild ?? (actorConfig!.applyEnvVarsToBuild as boolean | undefined);

if (actorCurrentVersion) {
const actorVersionModifier = { tarballUrl, sourceFiles, buildTag, sourceType, envVars, applyEnvVarsToBuild };
Expand Down
55 changes: 55 additions & 0 deletions test/api/commands/push.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,61 @@ describe('[api] apify push', () => {
TEST_TIMEOUT,
);

it(
'should read applyEnvVarsToBuild from actor.json, with the flag taking precedence',
async () => {
const testActor = await testUserClient.actors().create(TEST_ACTOR);
actorsForCleanup.add(testActor.id);
const testActorClient = testUserClient.actor(testActor.id);
const actorJson = JSON.parse(readFileSync(joinPath(LOCAL_CONFIG_PATH), 'utf8'));

try {
actorJson.applyEnvVarsToBuild = true;
writeFileSync(joinPath(LOCAL_CONFIG_PATH), JSON.stringify(actorJson, null, '\t'), { flag: 'w' });

await testRunCommand(ActorsPushCommand, {
args_actorId: testActor.id,
flags_noPrompt: true,
flags_force: true,
});

const versionWithFieldTrue = await testActorClient.version(actorJson.version).get();

actorJson.applyEnvVarsToBuild = false;
writeFileSync(joinPath(LOCAL_CONFIG_PATH), JSON.stringify(actorJson, null, '\t'), { flag: 'w' });
// the actor config is cached per cwd, so mid-test rewrites need a reset
resetCwdCaches();

await testRunCommand(ActorsPushCommand, {
args_actorId: testActor.id,
flags_noPrompt: true,
flags_force: true,
});

const versionWithFieldFalse = await testActorClient.version(actorJson.version).get();

// the file still says false, but the flag must win
await testRunCommand(ActorsPushCommand, {
args_actorId: testActor.id,
flags_noPrompt: true,
flags_force: true,
flags_applyEnvVarsToBuild: true,
});

const versionWithFlagOverride = await testActorClient.version(actorJson.version).get();

expect(versionWithFieldTrue!.applyEnvVarsToBuild).to.be.eql(true);
expect(versionWithFieldFalse!.applyEnvVarsToBuild).to.be.eql(false);
expect(versionWithFlagOverride!.applyEnvVarsToBuild).to.be.eql(true);
} finally {
delete actorJson.applyEnvVarsToBuild;
writeFileSync(joinPath(LOCAL_CONFIG_PATH), JSON.stringify(actorJson, null, '\t'), { flag: 'w' });
await testActorClient.delete();
}
},
TEST_TIMEOUT,
);

it(
'should upload zip for source files larger that 3MB',
async () => {
Expand Down