Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
FROM cypress/included:3.2.0
# https://hub.docker.com/r/cypress/included/tags
FROM cypress/included:13.6.0

# https://docs.cypress.io/guides/continuous-integration/introduction#Machine-requirements
RUN apt-get update && apt-get install -y curl jq
Expand Down
44 changes: 37 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,60 @@
# vercel-cypress

GitHub action which allows you to run Cypress tests against a Vercel preview deployment.
GitHub action which allows you to run Cypress tests against a Vercel preview
deployment.

## Prerequesites
## Prerequisites

This guide assumes you have set up the [Vercel GitHub integration](https://vercel.com/docs/concepts/git/vercel-for-github).
- [Vercel GitHub integration](https://vercel.com/docs/concepts/git/vercel-for-github).
- [Vercel API token](https://vercel.com/account/tokens) which should be stored
as an [encrypted secrets](https://docs.github.com/en/actions/security-guides/encrypted-secrets)
- You need the project ID (which can be found in the "Project Settings" on vercel)
- If your project belongs to a team, you need the team ID as well

To use this action you will need to first generate a [Vercel token](https://vercel.com/account/tokens).
Make sure that the API token has the right access scope and that your team id is
correct. You can do this with the following check; the output should be a large
json payload.

Then you can either store this with your account or repository's [secrets](https://docs.github.com/en/actions/security-guides/encrypted-secrets).
```sh
# reference: https://vercel.com/docs/rest-api/endpoints#list-deployments
export TOKEN=YOUR_VERCEL_API_TOKEN

export PROJECT_ID=YOUR_PROJECT_ID
curl -X GET "https://api.vercel.com/v6/deployments?projectId=${PROJECT_ID}" \
-H "Authorization: Bearer ${TOKEN}"

# for team projects
export TEAM_ID=YOUR_TEAM_ID
curl -X GET "https://api.vercel.com/v6/deployments?projectId=${PROJECT_ID}&teamId=${TEAM_ID}" \
-H "Authorization: Bearer ${TOKEN}"
```

## Usage

The following action will wait for the Vercel deployment to finish, then run Cypress but override the URL provided to Cypress' config to the latest Vercel deployment URL.
The following action will wait for the Vercel deployment to finish, then run
Cypress but override the URL provided to Cypress' config to the latest Vercel
deployment URL.

```yml
name: my-action

# (only) run after a successful deployment
on: deployment

jobs:
regression:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- uses: actions/vercel-cypress@v1
with:
# required: vercel API token
vercel-token: ${{ secrets.VERCEL_TOKEN }}
repo: my-repo
# required: project id
project-id: your-project-id
# required if the project is from a team
team-id: your-team-id
# optional: parameters that are passed to 'npx cypress run <options>'
options: "--browser chrome --spec cypress/e2e/base/*"
```
18 changes: 15 additions & 3 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,24 @@ inputs:
vercel-token:
description: "Vercel API token"
required: true
repo:
description: "Repository name"
default: ""
project-id:
description: "Project ID"
required: true
default: ""
team-id:
description: "Team ID (required if the project is from a team)"
required: false
default: ""
options:
description: "Cypress options"
required: false
default: ""
runs:
using: "docker"
image: "Dockerfile"
args:
- ${{ inputs.vercel-token }}
- ${{ inputs.repo }}
- ${{ inputs.project-id }}
- ${{ inputs.team-id }}
- ${{ inputs.options }}
21 changes: 18 additions & 3 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
#!/bin/sh -l

DEPLOYMENT_URL=$(curl --insecure -H "Content-type: application/json" -H "Authorization: Bearer $1" "https://api.vercel.com/v5/deployments?meta-githubRepo=${2}" | jq -r '.deployments[0].url')
npm install
npx cypress run --config baseUrl="https://$DEPLOYMENT_URL"
# inputs arguments
# 1 -> api token
# 2 -> project id
# 3 -> (optional) team id
# 4 -> cypress options

QUERY="projectId=$2"

if [ -n "$3" ]; then
QUERY="${QUERY}&teamId=${3}"
fi

DEPLOYMENT_URL=$(curl -X GET "https://api.vercel.com/v6/deployments?${QUERY}" -H "Authorization: Bearer $1" | jq -r '.deployments[0].url')
echo "=> found deployment url: ${DEPLOYMENT_URL}"

yarn install
export CYPRESS_BASE_URL="https://${DEPLOYMENT_URL}"
npx cypress run $4