-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Can this module be used from other JavaScript actions? #55
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
Comments
I don't think states are accessible from other actions, as you can see here. Also, it's kind of hard to use this repository's code as npm dependency due to the ncc bundled code, like you mentioned. However, I'm also interested if we can implement the caching mechanism in 3rd party actions. Right now, I'm trying to implement the necessary API calls based on Wouldn't it make sense to create a toolkit package that handles most of the cache code? |
absolutely would make sense to create a caching utility that other actions could reuse programmatically - and I think even rewriting this module would make sense. For example - when you require |
Saving state is meant to allow a given instance of an action in a workflow to save some information that can be read in its Right now e do not have any plans to create a core sharable module for the caching service. |
I'd like to see this too. I created actions for setting up Dart and Flutter, for example: - name: Setup Dart ${{ matrix.dart-version }}
uses: DanTup/gh-actions/setup-dart@master
with:
channel: ${{ matrix.dart-version }} Since the Dart stable branch doesn't change very often (and similar for Flutter) it seems ideal for caching, however I don't want to do it in the workflow file because:
It would be nice if the action could just manage this for the user. It may expose a flag to opt in/out, but ideally it would just always be on and be transparent (and reliable). In the action I can send a request for a JSON file that describes the version, figure out if it's one that already exists in the cache, and if so use that. It's actually what I thought https://github.com/actions/toolkit/tree/master/packages/tool-cache was going to help me to do, but sadly it appears not :-) |
We have cloned this repo and factored out |
I've marked this as A few concerns I have with a separate library for this:
While I appreciate the integration and ease of use that can come from Example: - name: Setup Dart ${{ matrix.dart-version }}
id: setup-dart
uses: DanTup/gh-actions/setup-dart@master
with:
channel: ${{ matrix.dart-version }}
- name: Cache
uses: actions/cache@v1
with:
path: ${{ steps.setup-dart.outputs.cache-path }}
key: ${{ steps.setup-dart.outputs.cache-key }} |
I do understand the concerns, but actions could easily provide a flag to let users enable/disable the cache. It's not in action authors interests to build something that works sub-optimally, since people won't use their actions. The problem with the example above, is that if you have multiple things to set up, you'll very quickly end up with tens of lines of junk just to get a (potentially very small) benefit of caching. Most likely you just won't bother. One of my workflow files already has like 47 lines) of "setup bloat" in it. Of course, I could wrap some of that up into an action that's stored in the same repo - but then we're back to the same request - how do I implement the caching in the action instead of the workflow file? When coding, if a function gets too big or complex, we might extract parts of it into smaller functions. It feels like the same should be possible here - if my workflow file gets large and complex, I should be able to extract a chunk of it into an action. It's hard to do that if some features only work embedded directly in the workflow file. |
I have to agree with Danny, but I'd like to think about how the separated cache approach is supposed to work. For example, how does the original setup action "knows" there's a cache? Or is the cache action always executed before any other action, and because of that, it should only check a directory? |
Right now, there would be no way to know. Checking the directory and proper placement of the setup step in a workflow is required. We've talked about the concept of "pre" actions that run before all other non-pre actions, but we don't support that at the moment (similar to the "post" action that |
Just as another data point - I have published https://github.com/bahmutov/npm-install which makes any Node project a breeze to use - because it takes care of NPM installation (or yarn) name: main
on: [push]
jobs:
build-and-test:
runs-on: ubuntu-latest
name: Build and test
steps:
- uses: actions/checkout@v1
- uses: bahmutov/npm-install@v1
- run: npm t I doubt users will be confused by what it does - most projects just need |
If you don't want people to accidentally use caching then add an "allows implicit caching" property to jobs that defaults to false that just makes restore/save just do nothing in the importable version of the caching action. |
@Cyberbeni I am not sure how adding a property would do anything really becasue the action would have to respect that setting. What we would likely have to do is have that setting control the scopes in the runtime token and for the given job you simply would not have permissions to read or update the cache. |
Is the issue here one of convenience/defaults, or security? My understanding was that an action basically has full access to everything running anyway (it can run arbitrary processes?) so it could easily read/write data that comes from/goes to the cache? (eg. you should never use actions you don't completely trust?) |
@chrispat Can't you just copy paste the cache action code into your action to be able to use caching? If yes, then having a property you have to respect is no different, just would be more convenient. |
@Cyberbeni Yes that's an option. We're working on moving the core cache logic into a package ( |
@joshmgross Any updates on this yet? |
No updates at this time, but this is definitely something we plan on doing in the future. |
👍 for this, it would allow to use caching for |
Also, now that there is a artifact package in the toolkit, this feels like the logical next step. |
I found a way to monkeypatch this module to allow To do this, we must import the source code. Therefore we have to update tsconfigs.json to include {
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"lib": ["esnext"],
"moduleResolution": "node",
"outDir": "./lib",
"rootDir": ".",
"strict": true,
"noImplicitAny": true,
"esModuleInterop": true,
"preserveSymlinks": true,
},
"include": [
"./src",
"./node_modules/@actions"
],
"exclude": ["**/*.test.ts", "__tests__"]
} With this, I managed to create a Github Action that was able to setup multiple cache layers in one step. E.g. steps:
- uses: actions/checkout@v2
- uses: ktmud/cached-dependencies@v1
with:
parallel: true
run: |
cache-restore npm
npm install
cache-save npm
cache-restore pip
pip install -r requirements.txt
cache-save pip
cache-restore cypress
cd cypress/
npm install
cache-save cypress |
@chrispat @ethomson Could you prioritize this? If there was a |
@eregon I'm picking this up. Hopefully it will get done next week. |
One more reason for this is it's actually very difficult to write a correct cache key if caching any result of native compilation such as Ruby gems with C extensions. Probably the key should include OS, architecture, Ruby engine, exact Ruby version, Gemfile/Gemfile.lock contents, and maybe even processor model version. |
Another use case: I'm currently using this workflow: - name: Use Node.js 12.15.0
uses: actions/setup-node@v1
with:
node-version: 12.15.0
- name: Cache dependencies
id: cache-node-modules
uses: actions/cache@v1
with:
path: node_modules
key: ${{ runner.os }}-node-12.15.0-modules-${{ hashFiles('package-lock.json') }}
- name: Install dependencies
run: npm ci
if: steps.cache-node-modules.outputs.cache-hit != 'true' The problem is that the key leaves some things to be desired, like including the current processor arch, not including I would love it if I could just do: - name: Use Node.js 12.15.0
uses: actions/setup-node@v1
with:
node-version: 12.15.0
- name: Install dependencies w/ cache
uses: LinusU/npm-install@v1 |
I found that there are quite a few cases where people stumbled upon @actions/tool-cache and tried to use it for caching within the action. I spent quite some time under its illusion as well ("why isn't it caching anything???") before searching to find this issue. |
|
This is great, I managed to simplify our caching a lot: 1-liner NPM cache: https://github.com/c-hive/gha-npm-cache |
I am trying to write a JavaScript action that runs
npm ci
and caches~/.npm
and~/.cache/Cypress
folders. I can use this action from YML file like thisBut I would like to have our own Cypress action that does the caching and NPM install for the user, something like
This means from Cypress GH action JS code I need to call
restore
andsave
files in this action. I have tried but without success, for examplewhich gives me
I think this particular error is due to a different
core
singleton between "regular" npm module andncc
- bundledactions/cache
one. But in general, do you have by any chance a plan to document how to use this module from JavaScript? I could write TypeScript and bundle it as a top level action, which should bundleactions/cache
code I think.The text was updated successfully, but these errors were encountered: