Skip to content

Commit

Permalink
feat: assign WCP project ID via WCP_PROJECT_ID
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
adrians5j committed Jul 8, 2022
1 parent d003c35 commit ef55575
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 29 deletions.
9 changes: 6 additions & 3 deletions example.env
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@ PULUMI_CONFIG_PASSPHRASE={PULUMI_CONFIG_PASSPHRASE}
# Overrides the default `https://app.webiny.com` app URL.
# WCP_APP_URL=...

# Besides having it in `webiny.project.ts`, we can also set the WCP project ID via this variable.
# WCP_PROJECT_ID=...

# You can paste a WCP CI/CD environment API key via this variable.
WCP_PROJECT_ENVIRONMENT_API_KEY=...
# WCP_PROJECT_ENVIRONMENT_API_KEY=...

# Thresholds for sending telemetry logs can be modified via these variables.
# Send logs to WCP once there are two logs to be sent.
WCP_TELEMETRY_CLIENT_SEND_LOGS_MAX_COUNT=2
# WCP_TELEMETRY_CLIENT_SEND_LOGS_MAX_COUNT=2

# Send logs to WCP once the last recorded log is more than 10 seconds old.
WCP_TELEMETRY_CLIENT_SEND_LOGS_MAX_TIME=10
# WCP_TELEMETRY_CLIENT_SEND_LOGS_MAX_TIME=10



46 changes: 23 additions & 23 deletions packages/cli/commands/wcp/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,39 +14,39 @@ module.exports = () => [
type: "hook-before-deploy",
name: "hook-before-deploy-environment-get-environment",
async hook(args, context) {
// If the project isn't activated, do nothing.
if (!context.project.config.id) {
return;
}
const apiKey = process.env.WCP_PROJECT_ENVIRONMENT_API_KEY;
if (apiKey) {
projectEnvironment = await getProjectEnvironment({ apiKey });
} else {
// If the project isn't activated, do nothing.
const wcpProjectId = context.project.config.id || process.env.WCP_PROJECT_ID;
if (!wcpProjectId) {
return;
}

if (process.env.WCP_PROJECT_ENVIRONMENT) {
// If we have WCP_PROJECT_ENVIRONMENT env var, we set the WCP_PROJECT_ENVIRONMENT_API_KEY too.
if (!process.env.WCP_PROJECT_ENVIRONMENT_API_KEY) {
// For development purposes, we allow setting the WCP_PROJECT_ENVIRONMENT env var directly.
// TODO: discuss this, do we really want to have this feature? Maybe we can remove it?
if (process.env.WCP_PROJECT_ENVIRONMENT) {
// If we have WCP_PROJECT_ENVIRONMENT env var, we set the WCP_PROJECT_ENVIRONMENT_API_KEY too.
const decryptedProjectEnvironment = decrypt(
process.env.WCP_PROJECT_ENVIRONMENT
);
process.env.WCP_PROJECT_ENVIRONMENT_API_KEY =
decryptedProjectEnvironment.apiKey;
}

return;
}
return;
}

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

const isValidId = orgId && projectId;
if (!isValidId) {
throw new Error(
`It seems the project ID, specified in "webiny.project.ts" file, is invalid.`
);
}
const isValidId = orgId && projectId;
if (!isValidId) {
throw new Error(
`It seems the project ID, specified in "webiny.project.ts" file, is invalid.`
);
}

const apiKey = process.env.WCP_PROJECT_ENVIRONMENT_API_KEY;
if (apiKey) {
projectEnvironment = await getProjectEnvironment({ apiKey });
} else {
// If there is no API key, that means we need to retrieve the currently logged-in user.
const user = await getUser();
const project = user.projects.find(item => item.id === projectId);
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/commands/wcp/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ module.exports.command = () => ({
`${chalk.green("✔")} You've successfully logged in to Webiny Control Panel.`
);

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

// If we have `orgId` and `projectId` in PAT's meta data, let's immediately activate the project.
if (pat.meta && pat.meta.orgId && pat.meta.projectId) {
Expand Down
5 changes: 3 additions & 2 deletions packages/project-utils/bundling/app/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ module.exports.applyDefaults = () => {
telemetry = isEnabled();
}

if (config.id) {
process.env.REACT_APP_WCP_PROJECT_ID = config.id;
const wcpProjectId = config.id || process.env.WCP_PROJECT_ID;
if (wcpProjectId) {
process.env.REACT_APP_WCP_PROJECT_ID = wcpProjectId;
}

if (!("REACT_APP_USER_ID" in process.env)) {
Expand Down

0 comments on commit ef55575

Please sign in to comment.