Skip to content

Commit

Permalink
feat: use working directory when running build and start commands (#92)
Browse files Browse the repository at this point in the history
* add another example

* add badge

* pass working directory to build and start

* add one more assertion
  • Loading branch information
bahmutov authored Feb 19, 2020
1 parent 4a923d7 commit 0bc0772
Show file tree
Hide file tree
Showing 10 changed files with 2,489 additions and 157 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/example-start.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: example-start
on: [push]
jobs:
start:
# example with web application build,
# server start and waiting for the server
# to respond before running tests
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v1

- name: Cypress tests
uses: ./
with:
working-directory: examples/start
build: npm run build
start: npm start
wait-on: 'http://localhost:5000'
2 changes: 1 addition & 1 deletion .prettierrc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"printWidth": 60,
"printWidth": 70,
"tabWidth": 2,
"useTabs": false,
"semi": false,
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,8 @@ jobs:
start-windows: npm run start:windows:server
```

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

### 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
103 changes: 25 additions & 78 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2543,10 +2543,7 @@ const io = __webpack_require__(1)
const { Octokit } = __webpack_require__(0)
const hasha = __webpack_require__(309)
const got = __webpack_require__(798)
const {
restoreCache,
saveCache
} = __webpack_require__(428)
const { restoreCache, saveCache } = __webpack_require__(428)
const fs = __webpack_require__(747)
const os = __webpack_require__(87)
const path = __webpack_require__(622)
Expand Down Expand Up @@ -2594,10 +2591,7 @@ const cypressCommandOptions = {
cwd: workingDirectory
}

const yarnFilename = path.join(
workingDirectory,
'yarn.lock'
)
const yarnFilename = path.join(workingDirectory, 'yarn.lock')
const packageLockFilename = path.join(
workingDirectory,
'package-lock.json'
Expand All @@ -2606,9 +2600,7 @@ const packageLockFilename = path.join(
const useYarn = () => fs.existsSync(yarnFilename)

const lockHash = () => {
const lockFilename = useYarn()
? yarnFilename
: packageLockFilename
const lockFilename = useYarn() ? yarnFilename : packageLockFilename
return hasha.fromFileSync(lockFilename)
}

Expand Down Expand Up @@ -2671,10 +2663,7 @@ const restoreCachedNpm = () => {
const saveCachedNpm = () => {
core.debug('saving NPM modules')
const NPM_CACHE = getNpmCache()
return saveCache(
NPM_CACHE.inputPath,
NPM_CACHE.primaryKey
)
return saveCache(NPM_CACHE.inputPath, NPM_CACHE.primaryKey)
}

const restoreCachedCypressBinary = () => {
Expand All @@ -2699,10 +2688,7 @@ const saveCachedCypressBinary = () => {
const install = () => {
// prevent lots of progress messages during install
core.exportVariable('CI', '1')
core.exportVariable(
'CYPRESS_CACHE_FOLDER',
CYPRESS_CACHE_FOLDER
)
core.exportVariable('CYPRESS_CACHE_FOLDER', CYPRESS_CACHE_FOLDER)

// Note: need to quote found tool to avoid Windows choking on
// npm paths with spaces like "C:\Program Files\nodejs\npm.cmd ci"
Expand All @@ -2719,28 +2705,18 @@ const install = () => {
})
} else {
core.debug('installing NPM dependencies')
core.exportVariable(
'npm_config_cache',
NPM_CACHE_FOLDER
)
core.exportVariable('npm_config_cache', NPM_CACHE_FOLDER)

return io.which('npm', true).then(npmPath => {
core.debug(`npm at "${npmPath}"`)
return exec.exec(
quote(npmPath),
['ci'],
cypressCommandOptions
)
return exec.exec(quote(npmPath), ['ci'], cypressCommandOptions)
})
}
}

const verifyCypressBinary = () => {
core.debug('Verifying Cypress')
core.exportVariable(
'CYPRESS_CACHE_FOLDER',
CYPRESS_CACHE_FOLDER
)
core.exportVariable('CYPRESS_CACHE_FOLDER', CYPRESS_CACHE_FOLDER)
return io.which('npx', true).then(npxPath => {
return exec.exec(
quote(npxPath),
Expand Down Expand Up @@ -2776,8 +2752,7 @@ const buildAppMaybe = () => {

core.debug(`building application using "${buildApp}"`)

// TODO: allow specifying custom working folder?
return exec.exec(buildApp)
return exec.exec(buildApp, [], cypressCommandOptions)
}

const startServerMaybe = () => {
Expand All @@ -2786,8 +2761,7 @@ const startServerMaybe = () => {
if (isWindows()) {
// allow custom Windows start command
startCommand =
core.getInput('start-windows') ||
core.getInput('start')
core.getInput('start-windows') || core.getInput('start')
} else {
startCommand = core.getInput('start')
}
Expand All @@ -2796,14 +2770,8 @@ const startServerMaybe = () => {
return
}

console.log(
'starting server with command "%s"',
startCommand
)
console.log(
'current working directory "%s"',
process.cwd()
)
console.log('starting server with command "%s"', startCommand)
console.log('current working directory "%s"', process.cwd())

const args = cliParser.parse(startCommand)
core.debug(`parsed command: ${args.join(' ')}`)
Expand All @@ -2813,15 +2781,12 @@ const startServerMaybe = () => {
core.debug(`with arguments ${args.slice(1).join(' ')}`)

const toolArguments = args.slice(1)
core.debug(
`running ${quote(toolPath)} ${toolArguments.join(
' '
)}`
)
const argsString = toolArguments.join(' ')
const cwd = cypressCommandOptions.cwd
core.debug(`running ${quote(toolPath)} ${argsString} in ${cwd}`)
core.debug('without waiting for the promise to resolve')

// TODO specify working directory when running the server?
exec.exec(quote(toolPath), toolArguments)
exec.exec(quote(toolPath), toolArguments, cypressCommandOptions)
})
}

Expand All @@ -2831,8 +2796,7 @@ const waitOnMaybe = () => {
return
}

const waitOnTimeout =
core.getInput('wait-on-timeout') || '60'
const waitOnTimeout = core.getInput('wait-on-timeout') || '60'

console.log(
'waiting on "%s" with timeout of %s seconds',
Expand All @@ -2850,9 +2814,7 @@ const I = x => x
const runTests = async () => {
const runTests = getInputBool('runTests', true)
if (!runTests) {
console.log(
'Skipping running tests: runTests parameter is false'
)
console.log('Skipping running tests: runTests parameter is false')
return
}

Expand All @@ -2868,20 +2830,15 @@ const runTests = async () => {
// split potentially long

const npxPath = await io.which('npx', true)
core.exportVariable(
'CYPRESS_CACHE_FOLDER',
CYPRESS_CACHE_FOLDER
)
core.exportVariable('CYPRESS_CACHE_FOLDER', CYPRESS_CACHE_FOLDER)

let cmd = []
if (commandPrefix) {
// we need to split the command prefix into individual arguments
// otherwise they are passed all as a single string
const parts = commandPrefix.split(' ')
cmd = cmd.concat(parts)
core.debug(
`with concatenated command prefix: ${cmd.join(' ')}`
)
core.debug(`with concatenated command prefix: ${cmd.join(' ')}`)
}
// push each CLI argument separately
cmd.push('cypress')
Expand Down Expand Up @@ -2947,10 +2904,7 @@ const runTests = async () => {
)

if (resp && resp.data) {
core.exportVariable(
'GH_BRANCH',
resp.data.head_branch
)
core.exportVariable('GH_BRANCH', resp.data.head_branch)
}

const runsList = await client.request(
Expand All @@ -2970,8 +2924,7 @@ const runTests = async () => {
}
}

const customCiBuildId =
core.getInput('ci-build-id') || parallelId
const customCiBuildId = core.getInput('ci-build-id') || parallelId
cmd.push('--ci-build-id')
cmd.push(quoteArgument(customCiBuildId))
}
Expand Down Expand Up @@ -3001,18 +2954,14 @@ const runTests = async () => {
windowsVerbatimArguments: false
}

core.debug(
`in working directory "${cypressCommandOptions.cwd}"`
)
core.debug(`in working directory "${cypressCommandOptions.cwd}"`)
return exec.exec(quote(npxPath), cmd, opts)
}

const installMaybe = () => {
const installParameter = getInputBool('install', true)
if (!installParameter) {
console.log(
'Skipping install because install parameter is false'
)
console.log('Skipping install because install parameter is false')
return Promise.resolve()
}

Expand All @@ -3025,9 +2974,7 @@ const installMaybe = () => {

return install().then(() => {
if (npmCacheHit && cypressCacheHit) {
core.debug(
'no need to verify Cypress binary or save caches'
)
core.debug('no need to verify Cypress binary or save caches')
return
}

Expand Down
6 changes: 6 additions & 0 deletions examples/start/cypress.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"fixturesFolder": false,
"supportFile": false,
"pluginsFile": false,
"baseUrl": "http://localhost:5000"
}
4 changes: 4 additions & 0 deletions examples/start/cypress/integration/spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
it('loads the page', () => {
cy.visit('/')
cy.contains('This is a page').should('be.visible')
})
Loading

0 comments on commit 0bc0772

Please sign in to comment.