Skip to content

Commit

Permalink
fix(misc): make generated ci workflow work without nx-cloud (#23199)
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` -->

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

`ci-workflow` generators error if Nx Cloud has not been enabled.

With the new Github onboarding flow, there is a chicken or the egg
problem.

The Github onboarding flow creates a PR but we don't have a pipeline to
run against it. To create the pipeline, Nx Cloud has to be enabled...
which... creates a lackluster PR.

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

`ci-workflow` generators do not error if Nx Cloud has not been enabled.
This way, the `ci-workflow` generator will be able to create a workflow,
which the PR made by Nx Cloud's Github onboarding flow will be run
against.

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

Fixes #
  • Loading branch information
FrozenPandaz committed May 6, 2024
1 parent f13dcce commit 41d21ab
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
steps:
- checkout
# Connect your workspace on my.nx.app and uncomment this to enable task distribution.
# Connect your workspace on nx.app and uncomment this to enable task distribution.
# The "--stop-agents-after" is optional, but allows idle agents to shut down once the "build" targets have been requested
# - run: npx nx-cloud start-ci-run --distribute-on="5 linux-medium-jvm" --stop-agents-after="build"
Expand Down Expand Up @@ -56,7 +56,7 @@ jobs:
with:
fetch-depth: 0
# Connect your workspace on my.nx.app and uncomment this to enable task distribution.
# Connect your workspace on nx.app and uncomment this to enable task distribution.
# The "--stop-agents-after" is optional, but allows idle agents to shut down once the "build" targets have been requested
# - run: npx nx-cloud start-ci-run --distribute-on="5 linux-medium-jvm" --stop-agents-after="build"
Expand Down
12 changes: 1 addition & 11 deletions packages/gradle/src/generators/ci-workflow/generator.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import { Tree, updateJson, NxJsonConfiguration } from '@nx/devkit';
import { Tree } from '@nx/devkit';

import { ciWorkflowGenerator } from './generator';

Expand All @@ -10,16 +10,6 @@ describe('ci-workflow generator', () => {
tree = createTreeWithEmptyWorkspace();
});

beforeEach(() => {
updateJson<NxJsonConfiguration>(tree, 'nx.json', (json) => {
return {
...json,
nxCloudAccessToken: 'xxxx-xxx-xxxx',
nxCloudUrl: 'https://my.nx.app',
};
});
});

describe.each([
['github', '.github/workflows/ci.yml'],
['circleci', '.circleci/config.yml'],
Expand Down
21 changes: 8 additions & 13 deletions packages/gradle/src/generators/ci-workflow/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
formatFiles,
detectPackageManager,
readNxJson,
readJson,
} from '@nx/devkit';
import { join } from 'path';
import { getNxCloudUrl, isNxCloudUsed } from 'nx/src/utils/nx-cloud-utils';
Expand All @@ -33,13 +34,7 @@ export interface Schema {
export async function ciWorkflowGenerator(tree: Tree, schema: Schema) {
const ci = schema.ci;

const nxJson: NxJsonConfiguration = readNxJson(tree);
const nxCloudUsed = isNxCloudUsed(nxJson);
if (!nxCloudUsed) {
throw new Error('This workspace is not connected to Nx Cloud.');
}

const options = getTemplateData(schema, nxJson);
const options = getTemplateData(tree, schema);
generateFiles(tree, join(__dirname, 'files', ci), '', options);
await formatFiles(tree);
}
Expand All @@ -54,19 +49,19 @@ interface Substitutes {
nxCloudHost: string;
}

function getTemplateData(
options: Schema,
nxJson: NxJsonConfiguration
): Substitutes {
function getTemplateData(tree: Tree, options: Schema): Substitutes {
const { name: workflowName, fileName: workflowFileName } = names(
options.name
);
const packageManager = detectPackageManager();
const { exec: packageManagerPrefix } =
getPackageManagerCommand(packageManager);

const nxCloudUrl = getNxCloudUrl(nxJson);
const nxCloudHost = new URL(nxCloudUrl).host;
let nxCloudHost: string = 'nx.app';
try {
const nxCloudUrl = getNxCloudUrl(readJson(tree, 'nx.json'));
nxCloudHost = new URL(nxCloudUrl).host;
} catch {}

const mainBranch = deduceDefaultBase();

Expand Down

0 comments on commit 41d21ab

Please sign in to comment.