Skip to content

Instantly share code, notes, and snippets.

@StarpTech
Last active December 7, 2020 03:54
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StarpTech/a5154609c671fcb09b454c3b5271c573 to your computer and use it in GitHub Desktop.
Save StarpTech/a5154609c671fcb09b454c3b5271c573 to your computer and use it in GitHub Desktop.
Generate Github Actions Workflow to run Cypress Tests in parallel (with artifact support + Basic Auth)
version: '3.4'
services:
cypress:
container_name: 'cypress-e2e'
image: 'cypress/included:3.4.1'
working_dir: /e2e
volumes:
- .:/e2e
environment:
- CYPRESS_baseUrl=${CYPRESS_BASE_URL}
entrypoint: cypress run --spec ${CYPRESS_SPEC} --config video=false --env AUTH=${CYPRESS_AUTH},USERNAME=${CYPRESS_AUTH_USERNAME},PASSWORD=${CYPRESS_AUTH_PASSWORD}
# $1: cypress integration folder, $2 github workflow file
node generate-github-workflow.js cypress/integration/ .github/workflows/cypress.yml
const { relative, basename, resolve } = require('path');
const fs = require('fs-extra');
const FileHound = require('filehound');
async function getGroups(dirPath, depth = 1) {
return FileHound.create()
.depth(depth)
.path(dirPath)
.directory()
.ignoreHiddenDirectories()
.find();
}
async function main() {
const skeleton = `
name: e2e-tests
on:
push:
paths:
- '*spec.js'
schedule:
# At 12:00 on every day-of-week from Monday through Friday.
- cron: '0 12 * * 1-5'
`;
const cypressIntegrationFolder = resolve(process.cwd(), process.argv[2]);
const yaml = [skeleton, 'jobs:'];
const groups = await getGroups(cypressIntegrationFolder);
for (const group of groups) {
const groupName = basename(group);
yaml.push(`
${groupName}:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: Run (${groupName}) tests
env:
CI: true
CYPRESS_AUTH: 1
CYPRESS_SPEC: '${relative(process.cwd(), group)}/**/*'
CYPRESS_BASE_URL: \${{ secrets.CYPRESS_BASE_URL }}
CYPRESS_AUTH_USERNAME: \${{ secrets.CYPRESS_AUTH_USERNAME }}
CYPRESS_AUTH_PASSWORD: \${{ secrets.CYPRESS_AUTH_PASSWORD }}
run: >-
docker-compose -f docker-compose.cypress.yml run
-e CYPRESS_SPEC=\${CYPRESS_SPEC}
-e CYPRESS_BASE_URL=\${CYPRESS_BASE_URL}
-e CYPRESS_AUTH=\${CYPRESS_AUTH}
-e CYPRESS_AUTH_USERNAME=\${CYPRESS_AUTH_USERNAME}
-e CYPRESS_AUTH_PASSWORD=\${CYPRESS_AUTH_PASSWORD}
cypress
# Ensure that dirs exists otherwise artifact upload will fail
- run: mkdir -p src/packages/lca-e2e/cypress/screenshots
if: always()
- run: mkdir -p src/packages/lca-e2e/cypress/videos
if: always()
- uses: actions/upload-artifact@master
if: always()
with:
name: ${groupName}-screenshots
path: src/packages/lca-e2e/cypress/screenshots
`);
}
const githubWorkflowPath = resolve(process.cwd(), process.argv[3]);
await fs.writeFile(githubWorkflowPath, yaml.join('\n'));
}
main().catch(err => {
console.error(err);
process.exit(1);
});
@StarpTech
Copy link
Author

image
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment