Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: start several services in parallel #238

Merged
merged 5 commits into from
Nov 18, 2020
Merged
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
14 changes: 14 additions & 0 deletions .github/workflows/example-wait-on.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,17 @@ jobs:
working-directory: examples/wait-on
start: npm run start3
wait-on: 'http://localhost:3050'

wait-multiple:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v1
- name: Cypress tests
uses: ./
with:
working-directory: examples/wait-on
# use different command formats
start: npm start -- --port 3050, npm run start2 -- --port 3060, node ./index3 --port 3070
# wait for the last server to respond
wait-on: 'http://localhost:3070'
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
- Run tests in [parallel](#parallel)
- [Build app](#build-app) before running the tests
- [Start server](#start-server) before running the tests
- [Start multiple servers](#start-multiple-servers) before running the tests
- [Wait for server](#wait-on) before running the tests
- use [command prefix](#command-prefix)
- use [own custom test command](#custom-test-command)
Expand Down Expand Up @@ -522,6 +523,27 @@ jobs:

[![start example](https://github.com/cypress-io/github-action/workflows/example-start/badge.svg?branch=master)](.github/workflows/example-start.yml)

**Note:** GitHub cleans up the running server processes automatically. This action does not stop them.

### Start multiple servers

You can start multiple server processes. For example, if you have an API to start using `npm run api` and the web server to start using `npm run web` you can put those commands in `start` using comma separation.

```yml
name: With servers
on: [push]
jobs:
cypress-run:
runs-on: ubuntu-18.04
steps:
- name: Checkout
uses: actions/checkout@v1
- name: Cypress run
uses: cypress-io/github-action@v2
with:
start: npm run api, npm run web
```

### Wait-on

If you are starting a local server and it takes a while to start, you can add a parameter `wait-on` and pass url to wait for the server to respond.
Expand Down
20 changes: 17 additions & 3 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8786,7 +8786,7 @@ const buildAppMaybe = () => {
return execCommand(buildApp, true, 'build app')
}

const startServerMaybe = () => {
const startServersMaybe = () => {
let startCommand

if (isWindows()) {
Expand All @@ -8801,7 +8801,21 @@ const startServerMaybe = () => {
return
}

return execCommand(startCommand, false, 'start server')
const separateStartCommands = startCommand
.split(',')
.map(s => s.trim())
.filter(Boolean)
core.debug(
`Separated start commands ${separateStartCommands.join(', ')}`
)

return separateStartCommands.map(startCommand => {
return execCommand(
startCommand,
false,
`start server "${startCommand}`
)
})
}

const waitOnMaybe = () => {
Expand Down Expand Up @@ -9181,7 +9195,7 @@ const installMaybe = () => {

installMaybe()
.then(buildAppMaybe)
.then(startServerMaybe)
.then(startServersMaybe)
.then(waitOnMaybe)
.then(runTests)
.then(() => {
Expand Down
11 changes: 8 additions & 3 deletions examples/wait-on/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,22 @@
// useful because shows timestamps
const log = require('debug')('*')
const http = require('http')
const arg = require('arg')

log('starting the server...')
const args = arg({
'--port': Number
})
const port = args['--port'] || 3050

log('starting the server at port %d', port)
setTimeout(function() {
const port = 3050
log('creating the server on port %d', port)
const server = http.createServer((req, res) => {
log('request %s %s', req.method, req.url)
res.writeHead(200)
res.end('all good')
})
server.listen(port, () => {
log('server is listening')
log('server is listening at port %d', port)
})
}, 7000)
7 changes: 6 additions & 1 deletion examples/wait-on/index2.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@
// useful because shows timestamps
const log = require('debug')('*')
const http = require('http')
const arg = require('arg')

const args = arg({
'--port': Number
})
const port = args['--port'] || 3050

const errorPeriodSeconds = 10
const endErrorsAt = +new Date() + errorPeriodSeconds * 1000
const port = 3050

log('creating the server on port %d', port)
log('will respond with errors for %d seconds', errorPeriodSeconds)
Expand Down
7 changes: 6 additions & 1 deletion examples/wait-on/index3.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@
// useful because shows timestamps
const log = require('debug')('*')
const http = require('http')
const arg = require('arg')

const args = arg({
'--port': Number
})
const port = args['--port'] || 3050

const errorPeriodSeconds = 40
const endErrorsAt = +new Date() + errorPeriodSeconds * 1000
const port = 3050

log('creating the server on port %d', port)
log('will not respond for the first %d seconds', errorPeriodSeconds)
Expand Down
5 changes: 5 additions & 0 deletions examples/wait-on/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/wait-on/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"cypress": "5.2.0"
},
"dependencies": {
"arg": "5.0.0",
"debug": "4.2.0"
}
}
20 changes: 17 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ const buildAppMaybe = () => {
return execCommand(buildApp, true, 'build app')
}

const startServerMaybe = () => {
const startServersMaybe = () => {
let startCommand

if (isWindows()) {
Expand All @@ -285,7 +285,21 @@ const startServerMaybe = () => {
return
}

return execCommand(startCommand, false, 'start server')
const separateStartCommands = startCommand
.split(',')
.map(s => s.trim())
.filter(Boolean)
core.debug(
`Separated start commands ${separateStartCommands.join(', ')}`
)

return separateStartCommands.map(startCommand => {
return execCommand(
startCommand,
false,
`start server "${startCommand}`
)
})
}

const waitOnMaybe = () => {
Expand Down Expand Up @@ -665,7 +679,7 @@ const installMaybe = () => {

installMaybe()
.then(buildAppMaybe)
.then(startServerMaybe)
.then(startServersMaybe)
.then(waitOnMaybe)
.then(runTests)
.then(() => {
Expand Down