feat(core): add e2e to ci workflows (#21487)

This commit is contained in:
Miroslav Jonaš 2024-02-03 00:20:39 +01:00 committed by GitHub
parent 197e670366
commit f309b259d6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 269 additions and 34 deletions

View File

@ -1,5 +1,175 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`CI Workflow generator optional e2e should add e2e to azure CI config 1`] = `
"name: CI
trigger:
- main
pr:
- main
variables:
CI: 'true'
\${{ if eq(variables['Build.Reason'], 'PullRequest') }}:
NX_BRANCH: $(System.PullRequest.PullRequestNumber)
TARGET_BRANCH: $[replace(variables['System.PullRequest.TargetBranch'],'refs/heads/','origin/')]
BASE_SHA: $(git merge-base $(TARGET_BRANCH) HEAD)
\${{ if ne(variables['Build.Reason'], 'PullRequest') }}:
NX_BRANCH: $(Build.SourceBranchName)
BASE_SHA: $(git rev-parse HEAD~1)
HEAD_SHA: $(git rev-parse HEAD)
jobs:
- job: main
pool:
vmImage: 'ubuntu-latest'
steps:
# Connect your workspace on my.nx.app and uncomment this to enable task distribution.
# The "--stop-agents-after" is optional, but allows idle agents to shut down once the "e2e-ci" targets have been requested
# - script: npx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="e2e-ci"
- script: npm ci
- script: npx nx-cloud record -- nx format:check --base=$(BASE_SHA) --head=$(HEAD_SHA)
- script: npx nx affected --base=$(BASE_SHA) --head=$(HEAD_SHA) -t lint test build e2e-ci
"
`;
exports[`CI Workflow generator optional e2e should add e2e to bitbucket pipelines config 1`] = `
"image: node:20
clone:
depth: full
pipelines:
pull-requests:
'**':
- parallel:
- step:
name: CI
script:
- export NX_BRANCH=$BITBUCKET_PR_ID
# Connect your workspace on my.nx.app and uncomment this to enable task distribution.
# The "--stop-agents-after" is optional, but allows idle agents to shut down once the "e2e-ci" targets have been requested
# - npx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="e2e-ci"
- npm ci
- npx nx-cloud record -- nx format:check
- npx nx affected -t lint test build e2e-ci
"
`;
exports[`CI Workflow generator optional e2e should add e2e to circleci CI config 1`] = `
"version: 2.1
orbs:
nx: nrwl/nx@1.6.2
jobs:
main:
docker:
- image: cimg/node:lts-browsers
steps:
- checkout
# Connect your workspace on my.nx.app and uncomment this to enable task distribution.
# The "--stop-agents-after" is optional, but allows idle agents to shut down once the "e2e-ci" targets have been requested
# - run: npx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="e2e-ci"
- run: npm ci
- nx/set-shas:
main-branch-name: 'main'
- run: npx nx-cloud record -- nx format:check --base=$NX_BASE --head=$NX_HEAD
- run: npx nx affected --base=$NX_BASE --head=$NX_HEAD -t lint test build e2e-ci
workflows:
version: 2
ci:
jobs:
- main
"
`;
exports[`CI Workflow generator optional e2e should add e2e to github CI config 1`] = `
"name: CI
on:
push:
branches:
- main
pull_request:
permissions:
actions: read
contents: read
jobs:
main:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
# Connect your workspace on my.nx.app and uncomment this to enable task distribution.
# The "--stop-agents-after" is optional, but allows idle agents to shut down once the "e2e-ci" targets have been requested
# - run: npx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="e2e-ci"
# Cache node_modules
- uses: actions/setup-node@v3
with:
node-version: 20
cache: 'npm'
- run: npm ci
- uses: nrwl/nx-set-shas@v4
- run: npx nx-cloud record -- nx format:check
- run: npx nx affected -t lint test build e2e-ci
"
`;
exports[`CI Workflow generator optional e2e should add e2e to github CI config with custom name 1`] = `
"name: My custom-workflow
on:
push:
branches:
- main
pull_request:
permissions:
actions: read
contents: read
jobs:
main:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
# Connect your workspace on my.nx.app and uncomment this to enable task distribution.
# The "--stop-agents-after" is optional, but allows idle agents to shut down once the "e2e-ci" targets have been requested
# - run: npx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="e2e-ci"
# Cache node_modules
- uses: actions/setup-node@v3
with:
node-version: 20
cache: 'npm'
- run: npm ci
- uses: nrwl/nx-set-shas@v4
- run: npx nx-cloud record -- nx format:check
- run: npx nx affected -t lint test build e2e-ci
"
`;
exports[`CI Workflow generator with npm should generate azure CI config 1`] = `
"name: CI
@ -31,7 +201,7 @@ jobs:
- script: npm ci
- script: npx nx-cloud record -- nx format:check --base=$(BASE_SHA) --head=$(HEAD_SHA)
- script: npx nx affected --base=$(BASE_SHA) --head=$(HEAD_SHA) -t=lint,test,build
- script: npx nx affected --base=$(BASE_SHA) --head=$(HEAD_SHA) -t lint test build
"
`;
@ -57,7 +227,7 @@ pipelines:
- npm ci
- npx nx-cloud record -- nx format:check
- npx nx affected -t=lint,test,build
- npx nx affected -t lint test build
"
`;
@ -83,7 +253,7 @@ jobs:
main-branch-name: 'main'
- run: npx nx-cloud record -- nx format:check --base=$NX_BASE --head=$NX_HEAD
- run: npx nx affected --base=$NX_BASE --head=$NX_HEAD -t=lint,test,build
- run: npx nx affected --base=$NX_BASE --head=$NX_HEAD -t lint test build
workflows:
version: 2
@ -128,7 +298,7 @@ jobs:
- uses: nrwl/nx-set-shas@v4
- run: npx nx-cloud record -- nx format:check
- run: npx nx affected -t=lint,test,build
- run: npx nx affected -t lint test build
"
`;
@ -166,7 +336,7 @@ jobs:
- uses: nrwl/nx-set-shas@v4
- run: npx nx-cloud record -- nx format:check
- run: npx nx affected -t=lint,test,build
- run: npx nx affected -t lint test build
"
`;
@ -191,7 +361,7 @@ CI:
- NX_BASE=\${CI_MERGE_REQUEST_DIFF_BASE_SHA:-$CI_COMMIT_BEFORE_SHA}
- npx nx-cloud record -- nx format:check --base=$NX_BASE --head=$NX_HEAD
- npx nx affected --base=$NX_BASE --head=$NX_HEAD -t=lint,test,build
- npx nx affected --base=$NX_BASE --head=$NX_HEAD -t lint test build
"
`;
@ -230,7 +400,7 @@ jobs:
- script: pnpm install --frozen-lockfile
- script: pnpm exec nx-cloud record -- nx format:check --base=$(BASE_SHA) --head=$(HEAD_SHA)
- script: pnpm exec nx affected --base=$(BASE_SHA) --head=$(HEAD_SHA) -t=lint,test,build
- script: pnpm exec nx affected --base=$(BASE_SHA) --head=$(HEAD_SHA) -t lint test build
"
`;
@ -258,7 +428,7 @@ pipelines:
- pnpm install --frozen-lockfile
- pnpm exec nx-cloud record -- nx format:check
- pnpm exec nx affected -t=lint,test,build
- pnpm exec nx affected -t lint test build
"
`;
@ -287,7 +457,7 @@ jobs:
main-branch-name: 'main'
- run: pnpm exec nx-cloud record -- nx format:check --base=$NX_BASE --head=$NX_HEAD
- run: pnpm exec nx affected --base=$NX_BASE --head=$NX_HEAD -t=lint,test,build
- run: pnpm exec nx affected --base=$NX_BASE --head=$NX_HEAD -t lint test build
workflows:
version: 2
@ -335,7 +505,7 @@ jobs:
- uses: nrwl/nx-set-shas@v4
- run: pnpm exec nx-cloud record -- nx format:check
- run: pnpm exec nx affected -t=lint,test,build
- run: pnpm exec nx affected -t lint test build
"
`;
@ -376,7 +546,7 @@ jobs:
- uses: nrwl/nx-set-shas@v4
- run: pnpm exec nx-cloud record -- nx format:check
- run: pnpm exec nx affected -t=lint,test,build
- run: pnpm exec nx affected -t lint test build
"
`;
@ -403,7 +573,7 @@ CI:
- NX_BASE=\${CI_MERGE_REQUEST_DIFF_BASE_SHA:-$CI_COMMIT_BEFORE_SHA}
- pnpm exec nx-cloud record -- nx format:check --base=$NX_BASE --head=$NX_HEAD
- pnpm exec nx affected --base=$NX_BASE --head=$NX_HEAD -t=lint,test,build
- pnpm exec nx affected --base=$NX_BASE --head=$NX_HEAD -t lint test build
"
`;
@ -440,7 +610,7 @@ jobs:
- script: yarn install --frozen-lockfile
- script: yarn nx-cloud record -- nx format:check --base=$(BASE_SHA) --head=$(HEAD_SHA)
- script: yarn nx affected --base=$(BASE_SHA) --head=$(HEAD_SHA) -t=lint,test,build
- script: yarn nx affected --base=$(BASE_SHA) --head=$(HEAD_SHA) -t lint test build
"
`;
@ -466,7 +636,7 @@ pipelines:
- yarn install --frozen-lockfile
- yarn nx-cloud record -- nx format:check
- yarn nx affected -t=lint,test,build
- yarn nx affected -t lint test build
"
`;
@ -492,7 +662,7 @@ jobs:
main-branch-name: 'main'
- run: yarn nx-cloud record -- nx format:check --base=$NX_BASE --head=$NX_HEAD
- run: yarn nx affected --base=$NX_BASE --head=$NX_HEAD -t=lint,test,build
- run: yarn nx affected --base=$NX_BASE --head=$NX_HEAD -t lint test build
workflows:
version: 2
@ -537,7 +707,7 @@ jobs:
- uses: nrwl/nx-set-shas@v4
- run: yarn nx-cloud record -- nx format:check
- run: yarn nx affected -t=lint,test,build
- run: yarn nx affected -t lint test build
"
`;
@ -575,7 +745,7 @@ jobs:
- uses: nrwl/nx-set-shas@v4
- run: yarn nx-cloud record -- nx format:check
- run: yarn nx affected -t=lint,test,build
- run: yarn nx affected -t lint test build
"
`;
@ -600,7 +770,7 @@ CI:
- NX_BASE=\${CI_MERGE_REQUEST_DIFF_BASE_SHA:-$CI_COMMIT_BEFORE_SHA}
- yarn nx-cloud record -- nx format:check --base=$NX_BASE --head=$NX_HEAD
- yarn nx affected --base=$NX_BASE --head=$NX_HEAD -t=lint,test,build
- yarn nx affected --base=$NX_BASE --head=$NX_HEAD -t lint test build
"
`;

View File

@ -131,6 +131,61 @@ describe('CI Workflow generator', () => {
});
});
});
describe('optional e2e', () => {
beforeEach(() => {
updateJson(tree, 'package.json', (json) => {
json.devDependencies = {
...json.devDependencies,
'@nx/cypress': 'latest',
};
return json;
});
});
it('should add e2e to github CI config', async () => {
setNxCloud(tree);
await ciWorkflowGenerator(tree, { ci: 'github', name: 'CI' });
expect(tree.read('.github/workflows/ci.yml', 'utf-8')).toMatchSnapshot();
});
it('should add e2e to circleci CI config', async () => {
setNxCloud(tree);
await ciWorkflowGenerator(tree, { ci: 'circleci', name: 'CI' });
expect(tree.read('.circleci/config.yml', 'utf-8')).toMatchSnapshot();
});
it('should add e2e to azure CI config', async () => {
setNxCloud(tree);
await ciWorkflowGenerator(tree, { ci: 'azure', name: 'CI' });
expect(tree.read('azure-pipelines.yml', 'utf-8')).toMatchSnapshot();
});
it('should add e2e to github CI config with custom name', async () => {
setNxCloud(tree);
await ciWorkflowGenerator(tree, {
ci: 'github',
name: 'My custom-workflow',
});
expect(
tree.read('.github/workflows/my-custom-workflow.yml', 'utf-8')
).toMatchSnapshot();
});
it('should add e2e to bitbucket pipelines config', async () => {
setNxCloud(tree);
await ciWorkflowGenerator(tree, {
ci: 'bitbucket-pipelines',
name: 'CI',
});
expect(tree.read('bitbucket-pipelines.yml', 'utf-8')).toMatchSnapshot();
});
});
});
function setNxCloud(tree: Tree) {

View File

@ -30,7 +30,6 @@ export async function ciWorkflowGenerator(tree: Tree, schema: Schema) {
if (!nxCloudUsed) {
throw new Error('This workspace is not connected to Nx Cloud.');
}
if (ci === 'bitbucket-pipelines' && defaultBranchNeedsOriginPrefix(nxJson)) {
writeJson(tree, 'nx.json', appendOriginPrefix(nxJson));
}
@ -48,6 +47,7 @@ interface Substitutes {
packageManagerInstall: string;
packageManagerPrefix: string;
nxCloudHost: string;
hasE2E: boolean;
tmpl: '';
}
@ -62,6 +62,15 @@ function normalizeOptions(options: Schema, tree: Tree): Substitutes {
const nxCloudUrl = getNxCloudUrl(readJson(tree, 'nx.json'));
const nxCloudHost = new URL(nxCloudUrl).host;
const packageJson = readJson(tree, 'package.json');
const allDependencies = {
...packageJson.dependencies,
...packageJson.devDependencies,
};
const hasE2E =
allDependencies['@nx/cypress'] || allDependencies['@nx/playwright'];
return {
workflowName,
workflowFileName,
@ -69,6 +78,7 @@ function normalizeOptions(options: Schema, tree: Tree): Substitutes {
packageManagerInstall,
packageManagerPrefix,
mainBranch: deduceDefaultBase(),
hasE2E,
nxCloudHost,
tmpl: '',
};

View File

@ -22,8 +22,8 @@ jobs:
vmImage: 'ubuntu-latest'
steps:
# Connect your workspace on <%= nxCloudHost %> 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
# - script: <%= packageManagerPrefix %> nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build"
# The "--stop-agents-after" is optional, but allows idle agents to shut down once the "<% if(hasE2E){ %>e2e-ci<% } else { %>build<% } %>" targets have been requested
# - script: <%= packageManagerPrefix %> nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="<% if(hasE2E){ %>e2e-ci<% } else { %>build<% } %>"
<% if(packageManager == 'pnpm'){ %>
- script: npm install --prefix=$HOME/.local -g pnpm@8
@ -31,4 +31,4 @@ jobs:
<% } %>- script: <%= packageManagerInstall %>
- script: <%= packageManagerPrefix %> nx-cloud record -- nx format:check --base=$(BASE_SHA) --head=$(HEAD_SHA)
- script: <%= packageManagerPrefix %> nx affected --base=$(BASE_SHA) --head=$(HEAD_SHA) -t=lint,test,build
- script: <%= packageManagerPrefix %> nx affected --base=$(BASE_SHA) --head=$(HEAD_SHA) -t lint test build<% if(hasE2E){ %> e2e-ci<% } %>

View File

@ -13,8 +13,8 @@ pipelines:
- export NX_BRANCH=$BITBUCKET_PR_ID
# Connect your workspace on <%= nxCloudHost %> 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
# - <%= packageManagerPrefix %> nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build"
# The "--stop-agents-after" is optional, but allows idle agents to shut down once the "<% if(hasE2E){ %>e2e-ci<% } else { %>build<% } %>" targets have been requested
# - <%= packageManagerPrefix %> nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="<% if(hasE2E){ %>e2e-ci<% } else { %>build<% } %>"
<% if(packageManager == 'pnpm'){ %>
- npm install --prefix=$HOME/.local -g pnpm@8
@ -22,4 +22,4 @@ pipelines:
- <%= packageManagerInstall %>
- <%= packageManagerPrefix %> nx-cloud record -- nx format:check
- <%= packageManagerPrefix %> nx affected -t=lint,test,build
- <%= packageManagerPrefix %> nx affected -t lint test build<% if(hasE2E){ %> e2e-ci<% } %>

View File

@ -11,8 +11,8 @@ jobs:
- checkout
# Connect your workspace on <%= nxCloudHost %> 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: <%= packageManagerPrefix %> nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build"
# The "--stop-agents-after" is optional, but allows idle agents to shut down once the "<% if(hasE2E){ %>e2e-ci<% } else { %>build<% } %>" targets have been requested
# - run: <%= packageManagerPrefix %> nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="<% if(hasE2E){ %>e2e-ci<% } else { %>build<% } %>"
<% if(packageManager == 'pnpm'){ %>
- run:
@ -23,7 +23,7 @@ jobs:
main-branch-name: '<%= mainBranch %>'
- run: <%= packageManagerPrefix %> nx-cloud record -- nx format:check --base=$NX_BASE --head=$NX_HEAD
- run: <%= packageManagerPrefix %> nx affected --base=$NX_BASE --head=$NX_HEAD -t=lint,test,build
- run: <%= packageManagerPrefix %> nx affected --base=$NX_BASE --head=$NX_HEAD -t lint test build<% if(hasE2E){ %> e2e-ci<% } %>
workflows:
version: 2

View File

@ -19,8 +19,8 @@ jobs:
fetch-depth: 0
# Connect your workspace on <%= nxCloudHost %> 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: <%= packageManagerPrefix %> nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build"
# The "--stop-agents-after" is optional, but allows idle agents to shut down once the "<% if(hasE2E){ %>e2e-ci<% } else { %>build<% } %>" targets have been requested
# - run: <%= packageManagerPrefix %> nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="<% if(hasE2E){ %>e2e-ci<% } else { %>build<% } %>"
<% if(packageManager == 'pnpm'){ %>
- uses: pnpm/action-setup@v2
@ -35,4 +35,4 @@ jobs:
- uses: nrwl/nx-set-shas@v4
- run: <%= packageManagerPrefix %> nx-cloud record -- nx format:check
- run: <%= packageManagerPrefix %> nx affected -t=lint,test,build
- run: <%= packageManagerPrefix %> nx affected -t lint test build<% if(hasE2E){ %> e2e-ci<% } %>

View File

@ -10,8 +10,8 @@ variables:
- merge_requests
script:
# Connect your workspace on <%= nxCloudHost %> 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
# - <%= packageManagerPrefix %> nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build"
# The "--stop-agents-after" is optional, but allows idle agents to shut down once the "<% if(hasE2E){ %>e2e-ci<% } else { %>build<% } %>" targets have been requested
# - <%= packageManagerPrefix %> nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="<% if(hasE2E){ %>e2e-ci<% } else { %>build<% } %>"
<% if(packageManager == 'pnpm'){ %>
- npm install --prefix=$HOME/.local -g pnpm@8
@ -21,4 +21,4 @@ variables:
- NX_BASE=${CI_MERGE_REQUEST_DIFF_BASE_SHA:-$CI_COMMIT_BEFORE_SHA}
- <%= packageManagerPrefix %> nx-cloud record -- nx format:check --base=$NX_BASE --head=$NX_HEAD
- <%= packageManagerPrefix %> nx affected --base=$NX_BASE --head=$NX_HEAD -t=lint,test,build
- <%= packageManagerPrefix %> nx affected --base=$NX_BASE --head=$NX_HEAD -t lint test build<% if(hasE2E){ %> e2e-ci<% } %>