feat(nx-cloud): add nxCloudId field for auth (#27197)

<!-- 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 #
This commit is contained in:
Louie Weng 2024-08-06 23:29:33 +02:00 committed by GitHub
parent 26f46c2534
commit fbecedce0f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 182 additions and 31 deletions

View File

@ -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[]
@ -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`

View File

@ -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[]
@ -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`

View File

@ -397,6 +397,9 @@
"accessToken": {
"type": "string"
},
"nxCloudId": {
"type": "string"
},
"captureStderr": {
"type": "boolean",
"description": "Defines whether the cache captures stderr or just stdout."

View File

@ -71,6 +71,7 @@ export const allowedWorkspaceExtensions = [
'installation',
'release',
'nxCloudAccessToken',
'nxCloudId',
'nxCloudUrl',
'nxCloudEncryptionKey',
'parallel',

View File

@ -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(
{
@ -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(
{

View File

@ -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';

View File

@ -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.

View File

@ -48,7 +48,7 @@ function getNxInitDate(): string | null {
}
}
async function createNxCloudWorkspace(
async function createNxCloudWorkspaceV1(
workspaceName: string,
installationSource: string,
nxInitDate: string | null
@ -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,
@ -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,
@ -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()
);
if (!(usesGithub && schema.installationSource === 'nx-connect')) {
if (process.env.NX_ENABLE_LOGIN === 'true') {
responseFromCreateNxCloudWorkspaceV2 = await createNxCloudWorkspaceV2(
getRootPackageName(tree),
schema.installationSource,
getNxInitDate()
);
addNxCloudOptionsToNxJson(
tree,
responseFromCreateNxCloudWorkspace?.token,
schema.directory
);
addNxCloudIdToNxJson(
tree,
responseFromCreateNxCloudWorkspaceV2?.nxCloudId,
schema.directory
);
await formatChangedFilesWithPrettierIfAvailable(tree, {
silent: schema.hideFormatLogs,
});
return responseFromCreateNxCloudWorkspace.token;
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;
}
}
}
}

View File

@ -23,6 +23,7 @@ export interface CloudTaskRunnerOptions extends DefaultTasksRunnerOptions {
url?: string;
useLightClient?: boolean;
clientVersion?: string;
nxCloudId?: string;
}
export const nxCloudTasksRunnerShell: TasksRunner<

View File

@ -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) {

View File

@ -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(
{

View File

@ -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');
}
@ -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;
}

View File

@ -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'
)
@ -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';