Skip to content

Commit

Permalink
feat: add cache-key parameter (#24)
Browse files Browse the repository at this point in the history
```yml
name: End-to-end tests
on: [push]
jobs:
  cypress-run:
    runs-on: ubuntu-latest
    # let's make sure our "app" works on several versions of Node
    strategy:
      matrix:
        node: [10, 12]
    name: E2E on Node v${{ matrix.node }}
    steps:
      - name: Setup Node
        uses: actions/setup-node@v1
        with:
          node-version: ${{ matrix.node }}
      - name: Checkout
        uses: actions/checkout@v1
      # run Cypress tests and record them under the same run
      # associated with commit SHA and just give a different group name
      - name: Cypress run
        uses: cypress-io/github-action@v1
        with:
          record: true
          group: Tests on Node v${{ matrix.node }}
          cache-key: node-v${{ matrix.node }}-on-${{ runner.os }}-hash-${{ hashFiles('yarn.lock') }}
        env:
          CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }}
```

* add link to more examples to readme

* use precise restore key

* feat: add cache-key parameter

* add cache-key example to the readme
  • Loading branch information
bahmutov authored Nov 16, 2019
1 parent a65bdd9 commit 20b153d
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 6 deletions.
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,46 @@ jobs:
wait-on: http://localhost:8080
```

### Custom cache key

Sometimes the default cache key does not work. For example, if you cannot share the Node modules across Node versions due to native extensions. In that case pass your own `cache-key` parameter.

```yml
name: End-to-end tests
on: [push]
jobs:
cypress-run:
runs-on: ubuntu-latest
# let's make sure our "app" works on several versions of Node
strategy:
matrix:
node: [10, 12]
name: E2E on Node v${{ matrix.node }}
steps:
- name: Setup Node
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node }}
- name: Checkout
uses: actions/checkout@v1
# run Cypress tests and record them under the same run
# associated with commit SHA and just give a different group name
- name: Cypress run
uses: cypress-io/github-action@v1
with:
record: true
group: Tests on Node v${{ matrix.node }}
cache-key: node-v${{ matrix.node }}-on-${{ runner.os }}-hash-${{ hashFiles('yarn.lock') }}
env:
CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }}
```

### More examples

| Name | Description |
| ---------------------------------------------------------------------------------- | ----------------------------------------------------------- |
| [cypress-gh-action-example](https://github.com/bahmutov/cypress-gh-action-example) | Uses Yarn, and runs in parallel on several versions of Node |

## Notes

### Installation
Expand Down
17 changes: 14 additions & 3 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1625,14 +1625,25 @@ const platformAndArch = `${process.platform}-${process.arch}`
const NPM_CACHE_FOLDER = path.join(homeDirectory, '.npm')
const NPM_CACHE = (() => {
const o = {}
let key = core.getInput('cache-key')

if (!key) {
if (useYarn) {
key = `yarn-${platformAndArch}-${lockHash}`
} else {
key = `npm-${platformAndArch}-${lockHash}`
}
} else {
console.log('using custom cache key "%s"', key)
}

if (useYarn) {
o.inputPath = path.join(homeDirectory, '.cache', 'yarn')
o.restoreKeys = `yarn-${platformAndArch}-`
o.restoreKeys = o.primaryKey = key
} else {
o.inputPath = NPM_CACHE_FOLDER
o.restoreKeys = `npm-${platformAndArch}-`
o.restoreKeys = o.primaryKey = key
}
o.primaryKey = o.restoreKeys + lockHash
return o
})()

Expand Down
17 changes: 14 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,25 @@ const platformAndArch = `${process.platform}-${process.arch}`
const NPM_CACHE_FOLDER = path.join(homeDirectory, '.npm')
const NPM_CACHE = (() => {
const o = {}
let key = core.getInput('cache-key')

if (!key) {
if (useYarn) {
key = `yarn-${platformAndArch}-${lockHash}`
} else {
key = `npm-${platformAndArch}-${lockHash}`
}
} else {
console.log('using custom cache key "%s"', key)
}

if (useYarn) {
o.inputPath = path.join(homeDirectory, '.cache', 'yarn')
o.restoreKeys = `yarn-${platformAndArch}-`
o.restoreKeys = o.primaryKey = key
} else {
o.inputPath = NPM_CACHE_FOLDER
o.restoreKeys = `npm-${platformAndArch}-`
o.restoreKeys = o.primaryKey = key
}
o.primaryKey = o.restoreKeys + lockHash
return o
})()

Expand Down

0 comments on commit 20b153d

Please sign in to comment.