Skip to content

Commit ef55575

Browse files
committed
feat: assign WCP project ID via WCP_PROJECT_ID
Usually, the ID is set via webiny.project.ts, but, for dev/testing purposes, we want to be able to set the WCP project ID via an env var too.
1 parent d003c35 commit ef55575

File tree

4 files changed

+33
-29
lines changed

4 files changed

+33
-29
lines changed

example.env

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,18 @@ PULUMI_CONFIG_PASSPHRASE={PULUMI_CONFIG_PASSPHRASE}
1919
# Overrides the default `https://app.webiny.com` app URL.
2020
# WCP_APP_URL=...
2121

22+
# Besides having it in `webiny.project.ts`, we can also set the WCP project ID via this variable.
23+
# WCP_PROJECT_ID=...
24+
2225
# You can paste a WCP CI/CD environment API key via this variable.
23-
WCP_PROJECT_ENVIRONMENT_API_KEY=...
26+
# WCP_PROJECT_ENVIRONMENT_API_KEY=...
2427

2528
# Thresholds for sending telemetry logs can be modified via these variables.
2629
# Send logs to WCP once there are two logs to be sent.
27-
WCP_TELEMETRY_CLIENT_SEND_LOGS_MAX_COUNT=2
30+
# WCP_TELEMETRY_CLIENT_SEND_LOGS_MAX_COUNT=2
2831

2932
# Send logs to WCP once the last recorded log is more than 10 seconds old.
30-
WCP_TELEMETRY_CLIENT_SEND_LOGS_MAX_TIME=10
33+
# WCP_TELEMETRY_CLIENT_SEND_LOGS_MAX_TIME=10
3134

3235

3336

packages/cli/commands/wcp/hooks.js

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,39 +14,39 @@ module.exports = () => [
1414
type: "hook-before-deploy",
1515
name: "hook-before-deploy-environment-get-environment",
1616
async hook(args, context) {
17-
// If the project isn't activated, do nothing.
18-
if (!context.project.config.id) {
19-
return;
20-
}
17+
const apiKey = process.env.WCP_PROJECT_ENVIRONMENT_API_KEY;
18+
if (apiKey) {
19+
projectEnvironment = await getProjectEnvironment({ apiKey });
20+
} else {
21+
// If the project isn't activated, do nothing.
22+
const wcpProjectId = context.project.config.id || process.env.WCP_PROJECT_ID;
23+
if (!wcpProjectId) {
24+
return;
25+
}
2126

22-
if (process.env.WCP_PROJECT_ENVIRONMENT) {
23-
// If we have WCP_PROJECT_ENVIRONMENT env var, we set the WCP_PROJECT_ENVIRONMENT_API_KEY too.
24-
if (!process.env.WCP_PROJECT_ENVIRONMENT_API_KEY) {
27+
// For development purposes, we allow setting the WCP_PROJECT_ENVIRONMENT env var directly.
28+
// TODO: discuss this, do we really want to have this feature? Maybe we can remove it?
29+
if (process.env.WCP_PROJECT_ENVIRONMENT) {
30+
// If we have WCP_PROJECT_ENVIRONMENT env var, we set the WCP_PROJECT_ENVIRONMENT_API_KEY too.
2531
const decryptedProjectEnvironment = decrypt(
2632
process.env.WCP_PROJECT_ENVIRONMENT
2733
);
2834
process.env.WCP_PROJECT_ENVIRONMENT_API_KEY =
2935
decryptedProjectEnvironment.apiKey;
30-
}
3136

32-
return;
33-
}
37+
return;
38+
}
3439

35-
// The `id` has the orgId/projectId structure, for example `my-org-x/my-project-y`.
36-
const orgProject = context.project.config.id;
37-
const [orgId, projectId] = orgProject.split("/");
40+
// The `id` has the orgId/projectId structure, for example `my-org-x/my-project-y`.
41+
const [orgId, projectId] = wcpProjectId.split("/");
3842

39-
const isValidId = orgId && projectId;
40-
if (!isValidId) {
41-
throw new Error(
42-
`It seems the project ID, specified in "webiny.project.ts" file, is invalid.`
43-
);
44-
}
43+
const isValidId = orgId && projectId;
44+
if (!isValidId) {
45+
throw new Error(
46+
`It seems the project ID, specified in "webiny.project.ts" file, is invalid.`
47+
);
48+
}
4549

46-
const apiKey = process.env.WCP_PROJECT_ENVIRONMENT_API_KEY;
47-
if (apiKey) {
48-
projectEnvironment = await getProjectEnvironment({ apiKey });
49-
} else {
5050
// If there is no API key, that means we need to retrieve the currently logged-in user.
5151
const user = await getUser();
5252
const project = user.projects.find(item => item.id === projectId);

packages/cli/commands/wcp/login.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ module.exports.command = () => ({
179179
`${chalk.green("✔")} You've successfully logged in to Webiny Control Panel.`
180180
);
181181

182-
let projectInitialized = Boolean(context.project.config.id);
182+
let projectInitialized = Boolean(context.project.config.id || process.env.WCP_PROJECT_ID);
183183

184184
// If we have `orgId` and `projectId` in PAT's meta data, let's immediately activate the project.
185185
if (pat.meta && pat.meta.orgId && pat.meta.projectId) {

packages/project-utils/bundling/app/utils.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ module.exports.applyDefaults = () => {
1212
telemetry = isEnabled();
1313
}
1414

15-
if (config.id) {
16-
process.env.REACT_APP_WCP_PROJECT_ID = config.id;
15+
const wcpProjectId = config.id || process.env.WCP_PROJECT_ID;
16+
if (wcpProjectId) {
17+
process.env.REACT_APP_WCP_PROJECT_ID = wcpProjectId;
1718
}
1819

1920
if (!("REACT_APP_USER_ID" in process.env)) {

0 commit comments

Comments
 (0)