Skip to content

Commit

Permalink
feat(nx-cloud): add nxCloudId field for auth (nrwl#27197)
Browse files Browse the repository at this point in the history
<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

Instead of `nxCloudAccessToken`, users can now connect to nx-cloud with
`nxCloudId`. This value will also default the runner to the cloud
runner.

Also modified the `connect-to-nx-cloud` generator such that it hits a
new API endpoint to grab a workspace's nxCloudId instead of access
token. (This feature is gated).

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
  • Loading branch information
lourw authored and ZackDeRose committed Aug 8, 2024
1 parent 6b1e81c commit a3c73e1
Show file tree
Hide file tree
Showing 13 changed files with 184 additions and 33 deletions.
10 changes: 10 additions & 0 deletions docs/generated/devkit/NxJsonConfiguration.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Nx.json configuration
- [neverConnectToCloud](../../devkit/documents/NxJsonConfiguration#neverconnecttocloud): boolean
- [nxCloudAccessToken](../../devkit/documents/NxJsonConfiguration#nxcloudaccesstoken): string
- [nxCloudEncryptionKey](../../devkit/documents/NxJsonConfiguration#nxcloudencryptionkey): string
- [nxCloudId](../../devkit/documents/NxJsonConfiguration#nxcloudid): string
- [nxCloudUrl](../../devkit/documents/NxJsonConfiguration#nxcloudurl): string
- [parallel](../../devkit/documents/NxJsonConfiguration#parallel): number
- [plugins](../../devkit/documents/NxJsonConfiguration#plugins): PluginConfiguration[]
Expand Down Expand Up @@ -189,6 +190,15 @@ Specifies the encryption key used to encrypt artifacts data before sending it to

---

### nxCloudId

`Optional` **nxCloudId**: `string`

If specified Nx will use nx-cloud by default with the given cloud id.
To use a different runner that accepts a cloud id, define it in [tasksRunnerOptions](../../devkit/documents/NxJsonConfiguration#tasksrunneroptions)

---

### nxCloudUrl

`Optional` **nxCloudUrl**: `string`
Expand Down
14 changes: 14 additions & 0 deletions docs/generated/devkit/Workspace.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use ProjectsConfigurations or NxJsonConfiguration
- [neverConnectToCloud](../../devkit/documents/Workspace#neverconnecttocloud): boolean
- [nxCloudAccessToken](../../devkit/documents/Workspace#nxcloudaccesstoken): string
- [nxCloudEncryptionKey](../../devkit/documents/Workspace#nxcloudencryptionkey): string
- [nxCloudId](../../devkit/documents/Workspace#nxcloudid): string
- [nxCloudUrl](../../devkit/documents/Workspace#nxcloudurl): string
- [parallel](../../devkit/documents/Workspace#parallel): number
- [plugins](../../devkit/documents/Workspace#plugins): PluginConfiguration[]
Expand Down Expand Up @@ -241,6 +242,19 @@ Specifies the encryption key used to encrypt artifacts data before sending it to

---

### nxCloudId

`Optional` **nxCloudId**: `string`

If specified Nx will use nx-cloud by default with the given cloud id.
To use a different runner that accepts a cloud id, define it in [tasksRunnerOptions](../../devkit/documents/NxJsonConfiguration#tasksrunneroptions)

#### Inherited from

[NxJsonConfiguration](../../devkit/documents/NxJsonConfiguration).[nxCloudId](../../devkit/documents/NxJsonConfiguration#nxcloudid)

---

### nxCloudUrl

`Optional` **nxCloudUrl**: `string`
Expand Down
3 changes: 3 additions & 0 deletions packages/nx/schemas/nx-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,9 @@
"accessToken": {
"type": "string"
},
"nxCloudId": {
"type": "string"
},
"captureStderr": {
"type": "boolean",
"description": "Defines whether the cache captures stderr or just stdout."
Expand Down
1 change: 1 addition & 0 deletions packages/nx/src/adapter/compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export const allowedWorkspaceExtensions = [
'installation',
'release',
'nxCloudAccessToken',
'nxCloudId',
'nxCloudUrl',
'nxCloudEncryptionKey',
'parallel',
Expand Down
17 changes: 16 additions & 1 deletion packages/nx/src/command-line/connect/connect-to-nx-cloud.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ describe('connect-to-nx-cloud', () => {
).toBe(false);
});

it('should say no if tasks runner options is undefined and nxCloudId is set', () => {
expect(
withEnvironmentVariables(
{
NX_ENABLE_LOGIN: 'true',
},
() =>
onlyDefaultRunnerIsUsed({
nxCloudId: 'xxxxxxx',
nxCloudUrl: 'https://my-nx-cloud.app',
})
)
).toBe(false);
});

it('should say no if cloud access token is in env', () => {
const defaultRunnerUsed = withEnvironmentVariables(
{
Expand All @@ -28,7 +43,7 @@ describe('connect-to-nx-cloud', () => {
expect(defaultRunnerUsed).toBe(false);
});

it('should say yes if tasks runner options is undefined and nxCloudAccessToken is not set', () => {
it('should say yes if tasks runner options is undefined and nxCloudAccessToken/nxCloudId is not set', () => {
expect(
withEnvironmentVariables(
{
Expand Down
5 changes: 4 additions & 1 deletion packages/nx/src/command-line/connect/connect-to-nx-cloud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ export function onlyDefaultRunnerIsUsed(nxJson: NxJsonConfiguration) {
// No tasks runner options OR no default runner defined:
// - If access token defined, uses cloud runner
// - If no access token defined, uses default
return !(nxJson.nxCloudAccessToken ?? process.env.NX_CLOUD_ACCESS_TOKEN);
return (
!(nxJson.nxCloudAccessToken ?? process.env.NX_CLOUD_ACCESS_TOKEN) &&
!nxJson.nxCloudId
);
}

return defaultRunner === 'nx/tasks-runners/default';
Expand Down
6 changes: 6 additions & 0 deletions packages/nx/src/config/nx-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,12 @@ export interface NxJsonConfiguration<T = '*' | string[]> {
*/
nxCloudAccessToken?: string;

/**
* If specified Nx will use nx-cloud by default with the given cloud id.
* To use a different runner that accepts a cloud id, define it in {@link tasksRunnerOptions}
*/
nxCloudId?: string;

/**
* Specifies the url pointing to an instance of nx cloud. Used for remote
* caching and displaying run links.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function getNxInitDate(): string | null {
}
}

async function createNxCloudWorkspace(
async function createNxCloudWorkspaceV1(
workspaceName: string,
installationSource: string,
nxInitDate: string | null
Expand All @@ -70,6 +70,28 @@ async function createNxCloudWorkspace(
return response.data;
}

async function createNxCloudWorkspaceV2(
workspaceName: string,
installationSource: string,
nxInitDate: string | null
): Promise<{ nxCloudId: string; url: string }> {
const apiUrl = getCloudUrl();
const response = await require('axios').post(
`${apiUrl}/nx-cloud/v2/create-org-and-workspace`,
{
workspaceName,
installationSource,
nxInitDate,
}
);

if (response.data.message) {
throw new Error(response.data.message);
}

return response.data;
}

export async function printSuccessMessage(
token: string | undefined,
installationSource: string,
Expand Down Expand Up @@ -125,6 +147,29 @@ function addNxCloudOptionsToNxJson(
}
}

function addNxCloudIdToNxJson(
tree: Tree,
nxCloudId: string,
directory: string = tree.root
) {
const nxJsonPath = join(directory, 'nx.json');
if (tree.exists(nxJsonPath)) {
updateJson<NxJsonConfiguration>(
tree,
join(directory, 'nx.json'),
(nxJson) => {
const overrideUrl = process.env.NX_CLOUD_API || process.env.NRWL_API;
if (overrideUrl) {
nxJson.nxCloudUrl = overrideUrl;
}
nxJson.nxCloudId = nxCloudId;

return nxJson;
}
);
}
}

export async function connectToNxCloud(
tree: Tree,
schema: ConnectToNxCloudOptions,
Expand All @@ -138,37 +183,56 @@ export async function connectToNxCloud(
} else {
const usesGithub = schema.github ?? (await repoUsesGithub(schema.github));

let responseFromCreateNxCloudWorkspace:
let responseFromCreateNxCloudWorkspaceV1:
| {
token: string;
}
| undefined;

let responseFromCreateNxCloudWorkspaceV2:
| {
nxCloudId: string;
}
| undefined;

// do NOT create Nx Cloud token (createNxCloudWorkspace)
// if user is using github and is running nx-connect
if (
!(
usesGithub &&
(schema.installationSource === 'nx-connect' ||
schema.installationSource === 'nx-console')
)
) {
responseFromCreateNxCloudWorkspace = await createNxCloudWorkspace(
getRootPackageName(tree),
schema.installationSource,
getNxInitDate()
);

addNxCloudOptionsToNxJson(
tree,
responseFromCreateNxCloudWorkspace?.token,
schema.directory
);

await formatChangedFilesWithPrettierIfAvailable(tree, {
silent: schema.hideFormatLogs,
});
return responseFromCreateNxCloudWorkspace.token;
if (!(usesGithub && schema.installationSource === 'nx-connect')) {
if (process.env.NX_ENABLE_LOGIN === 'true') {
responseFromCreateNxCloudWorkspaceV2 = await createNxCloudWorkspaceV2(
getRootPackageName(tree),
schema.installationSource,
getNxInitDate()
);

addNxCloudIdToNxJson(
tree,
responseFromCreateNxCloudWorkspaceV2?.nxCloudId,
schema.directory
);

await formatChangedFilesWithPrettierIfAvailable(tree, {
silent: schema.hideFormatLogs,
});
return responseFromCreateNxCloudWorkspaceV2.nxCloudId;
} else {
responseFromCreateNxCloudWorkspaceV1 = await createNxCloudWorkspaceV1(
getRootPackageName(tree),
schema.installationSource,
getNxInitDate()
);

addNxCloudOptionsToNxJson(
tree,
responseFromCreateNxCloudWorkspaceV1?.token,
schema.directory
);

await formatChangedFilesWithPrettierIfAvailable(tree, {
silent: schema.hideFormatLogs,
});
return responseFromCreateNxCloudWorkspaceV1.token;
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/nx/src/nx-cloud/nx-cloud-tasks-runner-shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface CloudTaskRunnerOptions extends DefaultTasksRunnerOptions {
url?: string;
useLightClient?: boolean;
clientVersion?: string;
nxCloudId?: string;
}

export const nxCloudTasksRunnerShell: TasksRunner<
Expand Down
16 changes: 12 additions & 4 deletions packages/nx/src/nx-cloud/utilities/axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,19 @@ export function createApiAxiosInstance(options: CloudTaskRunnerOptions) {
const baseUrl =
process.env.NX_CLOUD_API || options.url || 'https://cloud.nx.app';
const accessToken = ACCESS_TOKEN ? ACCESS_TOKEN : options.accessToken!;
const nxCloudId = options.nxCloudId;

if (!accessToken) {
throw new Error(
`Unable to authenticate. Either define accessToken in nx.json or set the NX_CLOUD_ACCESS_TOKEN env variable.`
);
// TODO(lourw): Update message with NxCloudId once it is supported
if (!accessToken && !nxCloudId) {
if (process.env.NX_ENABLE_LOGIN === 'true' && !nxCloudId) {
throw new Error(
`Unable to authenticate. Please connect your workspace to Nx Cloud to define a valid Nx Cloud Id. If you are in a CI context, please set the NX_CLOUD_ACCESS_TOKEN environment variable or define an access token in your nx.json.`
);
} else {
throw new Error(
`Unable to authenticate. Either define accessToken in nx.json or set the NX_CLOUD_ACCESS_TOKEN env variable. If you do not want to use Nx Cloud for this command, either set NX_NO_CLOUD=true, or pass the --no-cloud flag.`
);
}
}

if (options.customProxyConfigPath) {
Expand Down
18 changes: 18 additions & 0 deletions packages/nx/src/tasks-runner/run-command.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,24 @@ describe('getRunner', () => {
`);
});

it('uses cloud runner when tasksRunnerOptions are not present and nxCloudId is specified', () => {
const { tasksRunner, runnerOptions } = getRunner(
{},
{
nxCloudId: 'XXXX-XXX',
nxCloudUrl: 'https://my-nx-cloud.app',
}
);

expect(tasksRunner).toEqual(nxCloudTasksRunnerShell);
expect(runnerOptions).toMatchInlineSnapshot(`
{
"nxCloudId": "XXXX-XXX",
"url": "https://my-nx-cloud.app",
}
`);
});

it('uses cloud runner when tasksRunnerOptions are not present and accessToken is set in env', () => {
const { tasksRunner } = withEnvironmentVariables(
{
Expand Down
8 changes: 7 additions & 1 deletion packages/nx/src/tasks-runner/run-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,9 @@ function getTasksRunnerPath(
// No runner prop in tasks runner options, check if access token is set.
nxJson.tasksRunnerOptions?.[runner]?.options?.accessToken ||
// Cloud access token specified in env var.
process.env.NX_CLOUD_ACCESS_TOKEN;
process.env.NX_CLOUD_ACCESS_TOKEN ||
// Nx Cloud Id specified in nxJson
nxJson.nxCloudId;

return isCloudRunner ? 'nx-cloud' : require.resolve('./default-tasks-runner');
}
Expand Down Expand Up @@ -522,6 +524,10 @@ export function getRunnerOptions(
result.accessToken ??= nxJson.nxCloudAccessToken;
}

if (nxJson.nxCloudId && isCloudDefault) {
result.nxCloudId ??= nxJson.nxCloudId;
}

if (nxJson.nxCloudUrl && isCloudDefault) {
result.url ??= nxJson.nxCloudUrl;
}
Expand Down
4 changes: 3 additions & 1 deletion packages/nx/src/utils/nx-cloud-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export function isNxCloudUsed(nxJson: NxJsonConfiguration): boolean {
return (
!!process.env.NX_CLOUD_ACCESS_TOKEN ||
!!nxJson.nxCloudAccessToken ||
!!nxJson.nxCloudId ||
!!Object.values(nxJson.tasksRunnerOptions ?? {}).find(
(r) => r.runner == '@nrwl/nx-cloud' || r.runner == 'nx-cloud'
)
Expand All @@ -16,7 +17,8 @@ export function getNxCloudUrl(nxJson: NxJsonConfiguration): string {
);
if (
!cloudRunner &&
!(nxJson.nxCloudAccessToken || process.env.NX_CLOUD_ACCESS_TOKEN)
!(nxJson.nxCloudAccessToken || process.env.NX_CLOUD_ACCESS_TOKEN) &&
!nxJson.nxCloudId
)
throw new Error('nx-cloud runner not found in nx.json');
return cloudRunner?.options?.url ?? nxJson.nxCloudUrl ?? 'https://nx.app';
Expand Down

0 comments on commit a3c73e1

Please sign in to comment.