Skip to content

Commit b4465bc

Browse files
authored
test: add example-build-artifacts workflow (#1456)
* test: add example-build-artifacts workflow * add wait-on parameter * add reference to matrix strategy with parallel jobs
1 parent 41bd6af commit b4465bc

File tree

3 files changed

+111
-1
lines changed

3 files changed

+111
-1
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
name: example-build-artifacts
2+
# This workflow shows how to split build and test steps with artifacts.
3+
# In the build job, dependencies are installed, the app is built and the build results
4+
# are stored as an artifact using actions/upload-artifact.
5+
# No tests are run in the build job.
6+
# The test jobs use the results from the build job:
7+
# - dependencies and the Cypress binary are installed using dependency caching
8+
# - build results are restored using actions/download-artifact
9+
on:
10+
push:
11+
branches:
12+
- 'master'
13+
pull_request:
14+
workflow_dispatch:
15+
16+
jobs:
17+
build:
18+
runs-on: ubuntu-24.04
19+
steps:
20+
- name: Checkout
21+
uses: actions/checkout@v4
22+
23+
- name: Build app
24+
uses: ./ # refers to local instance of cypress-io/github-action@v6
25+
with:
26+
runTests: false # only build app, don't test yet
27+
build: npm run build
28+
working-directory: examples/nextjs
29+
- name: Store build artifacts
30+
uses: actions/upload-artifact@v4 # https://github.com/actions/upload-artifact
31+
with:
32+
name: app
33+
path: examples/nextjs/build
34+
if-no-files-found: error
35+
retention-days: 1
36+
37+
test:
38+
needs: build
39+
strategy:
40+
fail-fast: false
41+
matrix:
42+
os: [ubuntu-24.04, windows-2025, macos-15]
43+
runs-on: ${{ matrix.os }}
44+
steps:
45+
- name: Checkout
46+
uses: actions/checkout@v4
47+
48+
- name: Restore build artifacts
49+
uses: actions/download-artifact@v4 # https://github.com/actions/download-artifact
50+
with:
51+
name: app
52+
path: examples/nextjs/build
53+
54+
- name: Cypress tests
55+
uses: ./
56+
with:
57+
start: npm start # start server using the build artifacts
58+
wait-on: http://localhost:3000
59+
working-directory: examples/nextjs

README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ The following examples demonstrate the actions' functions.
7070
- Use [custom cache key](#custom-cache-key)
7171
- Run tests on multiple [Node versions](#node-versions)
7272
- Split [install and tests](#split-install-and-tests) into separate jobs
73+
- Split [install and tests](#split-install-and-test-with-artifacts) with artifacts
7374
- Use [custom install commands](#custom-install)
7475
- Install [only Cypress](#install-cypress-only) to avoid installing all dependencies
7576
- Use [timeouts](#timeouts) to avoid hanging CI jobs
@@ -1386,6 +1387,54 @@ jobs:
13861387

13871388
See [cypress-gh-action-monorepo](https://github.com/bahmutov/cypress-gh-action-monorepo) for a working example.
13881389

1390+
### Split install and test with artifacts
1391+
1392+
If your test job(s) first need a build step, you can split the jobs into a separate build job followed by test jobs. You pass the build results to any subsequent jobs using [GitHub Actions artifacts](https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/storing-and-sharing-data-from-a-workflow).
1393+
1394+
In the build job, use [upload-artifact](https://github.com/actions/upload-artifact) to store the build results, then in subsequent jobs use [download-artifact](https://github.com/actions/download-artifact) to restore them.
1395+
1396+
Your tests jobs may use a [GitHub Actions matrix strategy](https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/running-variations-of-jobs-in-a-workflow), such as when recording to [Cypress Cloud](https://on.cypress.io/cloud-introduction) with [parallel jobs](#parallel).
1397+
1398+
```yml
1399+
name: Split build and test
1400+
on: push
1401+
jobs:
1402+
build:
1403+
runs-on: ubuntu-24.04
1404+
steps:
1405+
- uses: actions/checkout@v4
1406+
- name: Build app
1407+
uses: cypress-io/github-action@v6
1408+
with:
1409+
runTests: false # only build app, don't test yet
1410+
build: npm run build
1411+
- name: Store build artifacts
1412+
uses: actions/upload-artifact@v4
1413+
with:
1414+
name: app
1415+
path: build
1416+
if-no-files-found: error
1417+
retention-days: 1
1418+
1419+
test:
1420+
needs: build
1421+
runs-on: ubuntu-24.04
1422+
steps:
1423+
- uses: actions/checkout@v4
1424+
- name: Restore build artifacts
1425+
uses: actions/download-artifact@v4
1426+
with:
1427+
name: app
1428+
path: build
1429+
1430+
- name: Cypress tests
1431+
uses: cypress-io/github-action@v6
1432+
with:
1433+
start: npm start # start server using the build artifacts
1434+
```
1435+
1436+
[![Split with build artifacts](https://github.com/cypress-io/github-action/actions/workflows/example-build-artifacts.yml/badge.svg)](.github/workflows/example-build-artifacts.yml)
1437+
13891438
### Custom install
13901439

13911440
Finally, you might not need this GH Action at all. For example, if you want to split the npm dependencies installation from the Cypress binary installation, then it makes no sense to use this action. Instead you can install and cache Cypress yourself.

examples/nextjs/next.config.mjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
/** @type {import('next').NextConfig} */
2-
const nextConfig = {};
2+
const nextConfig = {
3+
distDir: 'build',
4+
};
35

46
export default nextConfig;

0 commit comments

Comments
 (0)