Skip to content

Commit

Permalink
feat: allow recording in fork with secrets (#751)
Browse files Browse the repository at this point in the history
* feat: allow recording in fork with secrets

* Insert new line
  • Loading branch information
MikeMcC399 authored Mar 7, 2023
1 parent d997948 commit 7eaec0d
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 27 deletions.
49 changes: 38 additions & 11 deletions .github/workflows/example-custom-ci-build-id.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
name: example-custom-ci-build-id
#
# To set up this workflow to work with your own Cypress Cloud project
# refer to the README in the example directory examples/recording.
#
# ---
#
# Typically you would let this action
# determine a unique build id to tie multiple parallel
# test jobs together into a single logical Cypress Cloud run.
Expand All @@ -9,22 +16,48 @@
# multiple testing jobs running in parallel
# based on the recipe written in
# https://medium.com/attest-r-and-d/adding-a-unique-github-build-identifier-7aa2e83cadca
# The use of set-output is however deprecated by GitHub since the above blog was written
# in 2019 so this is replaced by GitHub Environment files, see
# The use of set-output is however deprecated by GitHub since the above blog
# was written in 2019 so this is replaced by GitHub Environment files, see
# https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#environment-files
#
name: example-custom-ci-build-id

on:
push:
branches:
- 'master'
pull_request:
workflow_dispatch:

env:
# Set up the Cypress Cloud project ID and record key as environment variables
# If the Actions secret EXAMPLE_PROJECT_ID is not defined then
# the projectId is taken from cypress.json (v9) or cypress.config.js (v10 and later).
# If the Actions secret EXAMPLE_RECORDING_KEY is not defined then recording jobs are skipped.
CYPRESS_PROJECT_ID: ${{ secrets.EXAMPLE_PROJECT_ID }}
CYPRESS_RECORD_KEY: ${{ secrets.EXAMPLE_RECORDING_KEY }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

jobs:

check-record-key:
runs-on: ubuntu-22.04
outputs:
record-key-exists: ${{ steps.record-key-check.outputs.defined }}
steps:
- name: Check for record key
id: record-key-check
run: |
if [ "${{ secrets.EXAMPLE_RECORDING_KEY }}" != '' ]; then
echo "defined=true" >> $GITHUB_OUTPUT;
else
echo "defined=false" >> $GITHUB_OUTPUT;
fi
# single job that generates and outputs a common id
prepare:
needs: [check-record-key]
runs-on: ubuntu-22.04
if: needs.check-record-key.outputs.record-key-exists == 'true'
outputs:
uuid: ${{ steps.uuid.outputs.value }}
steps:
Expand All @@ -41,7 +74,7 @@ jobs:
# let's run a small subset of the tests
# and record it to the Cypress Cloud
smoke-tests:
needs: ['prepare']
needs: [prepare]
runs-on: ubuntu-22.04
steps:
- name: Checkout 🛎
Expand All @@ -61,16 +94,13 @@ jobs:
ci-build-id: ${{ needs.prepare.outputs.uuid }}
spec: 'cypress/e2e/spec-a.cy.js'
working-directory: examples/recording
env:
# pass the Cypress Cloud record key as an environment variable
CYPRESS_RECORD_KEY: ${{ secrets.EXAMPLE_RECORDING_KEY }}

# if smoke tests pass, run all tests, splitting them in parallel
# because we record with the same build id, smoke and these
# tests should be under the same logical recorded run
# under different groups
all-tests:
needs: ['prepare', 'smoke-tests']
needs: [prepare, smoke-tests]
runs-on: ubuntu-22.04
strategy:
fail-fast: false
Expand All @@ -94,6 +124,3 @@ jobs:
--ci-build-id ${{ needs.prepare.outputs.uuid }} \
--group "2 - all tests"
working-directory: examples/recording
env:
# pass the Cypress Cloud record key as an environment variable
CYPRESS_RECORD_KEY: ${{ secrets.EXAMPLE_RECORDING_KEY }}
51 changes: 35 additions & 16 deletions .github/workflows/example-recording.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,46 @@
name: example-recording
#
# To set up this workflow to work with your own Cypress Cloud project
# refer to the README in the example directory examples/recording.
#
on:
push:
branches:
- 'master'
pull_request:
workflow_dispatch:

env:
# Set up the Cypress Cloud project ID and record key as environment variables
# If the Actions secret EXAMPLE_PROJECT_ID is not defined then
# the projectId is taken from cypress.json (v9) or cypress.config.js (v10 and later).
# If the Actions secret EXAMPLE_RECORDING_KEY is not defined then recording jobs are skipped.
CYPRESS_PROJECT_ID: ${{ secrets.EXAMPLE_PROJECT_ID }}
CYPRESS_RECORD_KEY: ${{ secrets.EXAMPLE_RECORDING_KEY }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

jobs:

check-record-key:
runs-on: ubuntu-22.04
outputs:
record-key-exists: ${{ steps.record-key-check.outputs.defined }}
steps:
- name: Check for record key
id: record-key-check
run: |
if [ "${{ secrets.EXAMPLE_RECORDING_KEY }}" != '' ]; then
echo "defined=true" >> $GITHUB_OUTPUT;
else
echo "defined=false" >> $GITHUB_OUTPUT;
fi
# ~~~~~~~~~~~~~~~~~~ Cypress v9 and below (using Legacy configuration) ~~~~~~~~~~~~~~~~~~~ #

parallel-v9:
runs-on: ubuntu-22.04
needs: [check-record-key]
if: needs.check-record-key.outputs.record-key-exists == 'true'
steps:
- name: Checkout
uses: actions/checkout@v3
Expand All @@ -32,10 +61,6 @@ jobs:
parallel: true
group: Recording example v9
tag: action
env:
# pass the Dashboard record key as an environment variable
CYPRESS_RECORD_KEY: ${{ secrets.EXAMPLE_RECORDING_KEY }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

# see "outcome" and "conclusion" of a step doc
# https://docs.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions#steps-context
Expand All @@ -47,6 +72,8 @@ jobs:
group-v9:
runs-on: ubuntu-22.04
needs: [check-record-key]
if: needs.check-record-key.outputs.record-key-exists == 'true'
steps:
- name: Checkout
uses: actions/checkout@v3
Expand All @@ -60,15 +87,13 @@ jobs:
record: true
# no parallel flag, just the group name
group: Recording group v9
env:
# pass the Dashboard record key as an environment variable
CYPRESS_RECORD_KEY: ${{ secrets.EXAMPLE_RECORDING_KEY }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Cypress v10 and higher ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #

parallel:
runs-on: ubuntu-22.04
needs: [check-record-key]
if: needs.check-record-key.outputs.record-key-exists == 'true'
steps:
- name: Checkout
uses: actions/checkout@v3
Expand All @@ -89,10 +114,6 @@ jobs:
parallel: true
group: Recording example
tag: action
env:
# pass the Dashboard record key as an environment variable
CYPRESS_RECORD_KEY: ${{ secrets.EXAMPLE_RECORDING_KEY }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

# see "outcome" and "conclusion" of a step doc
# https://docs.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions#steps-context
Expand All @@ -104,6 +125,8 @@ jobs:
group:
runs-on: ubuntu-22.04
needs: [check-record-key]
if: needs.check-record-key.outputs.record-key-exists == 'true'
steps:
- name: Checkout
uses: actions/checkout@v3
Expand All @@ -117,7 +140,3 @@ jobs:
record: true
# no parallel flag, just the group name
group: Recording group
env:
# pass the Dashboard record key as an environment variable
CYPRESS_RECORD_KEY: ${{ secrets.EXAMPLE_RECORDING_KEY }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
25 changes: 25 additions & 0 deletions examples/recording/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# example: recording

The recording example uses [Cypress Cloud](https://docs.cypress.io/guides/cloud/introduction) to record results using the Cypress Cloud `projectId` as defined in the [cypress.config.js](cypress.config.js) configuration file.

## Using your own Cypress Cloud project

In order to use the recording example with your own Cypress Cloud project, you need to replace the `projectId` and `record key` with your own values.

Follow the [Cypress Cloud](https://docs.cypress.io/guides/cloud/introduction) documentation to sign up, if you do not already have an account, or to [sign in](https://cloud.cypress.io/) if you have an account.

Create a new project if one does not exist.

Access "Project settings" in Cypress Cloud and copy the contents of each of the following parameters for the project into the Security settings of your GitHub fork, using "Secrets and variables" > "Actions", then "Secrets" for the "Project ID" and for the "Record Key" as in the table below:

| Cypress Cloud name | Actions name in fork | Variable type |
| ------------------ | --------------------- | --------------- |
| Project ID | EXAMPLE_PROJECT_ID | Actions secrets |
| Record Keys | EXAMPLE_RECORDING_KEY | Actions secrets |

Refer to the GitHub documentation
- [Creating encrypted secrets for a repository](https://docs.github.com/en/actions/security-guides/encrypted-secrets#creating-encrypted-secrets-for-a-repository).

When you have done this, the example recording will take the `projectId` from the `EXAMPLE_PROJECT_ID` variable instead of from the [cypress.config.js](cypress.config.js) configuration file.

This setup allows the example recording to run in your own Cypress Cloud project, whilst leaving the same example for use in the parent repository [cypress-io/github-action](https://github.com/cypress-io/github-action) unchanged. The parent repository has its own `EXAMPLE_RECORDING_KEY` defined as a secret and it uses the `projectId` as defined in the [cypress.config.js](cypress.config.js) configuration file.
25 changes: 25 additions & 0 deletions examples/v9/recording/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# example: recording

The recording example uses [Cypress Cloud](https://docs.cypress.io/guides/cloud/introduction) to record results using the Cypress Cloud `projectId` as defined in the [cypress.json](cypress.json) configuration file.

## Using your own Cypress Cloud project

In order to use the recording example with your own Cypress Cloud project, you need to replace the `projectId` and `record key` with your own values.

Follow the [Cypress Cloud](https://docs.cypress.io/guides/cloud/introduction) documentation to sign up, if you do not already have an account, or to [sign in](https://cloud.cypress.io/) if you have an account.

Create a new project if one does not exist.

Access "Project settings" in Cypress Cloud and copy the contents of each of the following parameters for the project into the Security settings of your GitHub fork, using "Secrets and variables" > "Actions", then "Secrets" for the "Project ID" and for the "Record Key" as in the table below:

| Cypress Cloud name | Actions name in fork | Variable type |
| ------------------ | --------------------- | --------------- |
| Project ID | EXAMPLE_PROJECT_ID | Actions secrets |
| Record Keys | EXAMPLE_RECORDING_KEY | Actions secrets |

Refer to the GitHub documentation
- [Creating encrypted secrets for a repository](https://docs.github.com/en/actions/security-guides/encrypted-secrets#creating-encrypted-secrets-for-a-repository).

When you have done this, the example recording will take the `projectId` from the `EXAMPLE_PROJECT_ID` variable instead of from the [cypress.json](cypress.json) configuration file.

This setup allows the example recording to run in your own Cypress Cloud project, whilst leaving the same example for use in the parent repository [cypress-io/github-action](https://github.com/cypress-io/github-action) unchanged. The parent repository has its own `EXAMPLE_RECORDING_KEY` defined as a secret and it uses the `projectId` as defined in the [cypress.json](cypress.json) configuration file.

0 comments on commit 7eaec0d

Please sign in to comment.