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

Provide a output for the selected Node.js version #150

Closed
LinusU opened this issue Apr 29, 2020 · 10 comments
Closed

Provide a output for the selected Node.js version #150

LinusU opened this issue Apr 29, 2020 · 10 comments

Comments

@LinusU
Copy link

LinusU commented Apr 29, 2020

It would be great if there could be an output that tells which exact version of Node.js was installed.

An example use case for this is cacheing node_modules, which is only valid for the same OS + Node.js version + package-lock combination:

name: Node CI

on:
  pull_request:
    branches:
    - master

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2

    - name: Setup Node.js
      id: setup-node
      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-${{ steps.setup-node.outputs.node-version }}-modules-${{ hashFiles('package-lock.json') }}

    - name: Install dependencies
      run: npm ci
      if: steps.cache-node-modules.outputs.cache-hit != 'true'

    - name: Tets
      run: npm test
@kennethtran93
Copy link

There's a workaround for this in the meantime:

    - name: Get NodeJS Version
      id: nodeversion
      run: |
        echo "::set-output name=version::$(node --version)"

Then in the future steps you can call it ${{ steps.nodeversion.outputs.version }}

I only adapted this from another workflow which calls a different command, but this should work for what you are doing. Just put this after setup-node

@bryanmacfarlane
Copy link
Member

I don't think it even needs to be an output. On the target side (which would read the output for this proposal) simply do a $(node --version) to compose the key. Writing an output and executing a graph of jobs is fragile because reading the cache shouldn't be based on the previous jobs node version, it should be based on the node version in the job that's consuming the cached node-modules.

@rdmurphy
Copy link

Thanks for responding @bryanmacfarlane!

I've been happily using @kennethtran93's suggestion but I don't fully follow — I agree that's the ideal in situations when you're writing a shell script, but is it possible to do the call to $(node --version) inline like that within a YAML file using the expression syntax?

I'm using this to do the opposite of your example — based on the Node version (and the system + hashed package-lock.json) that was installed I tap into a potentially cached node_modules. The main difference is I am doing this with @v2-beta where I pass in just a simple '12'. I don't really care which v12 I get but I do want to ensure the cache hit is based on the exact version of Node that was eventually installed.

(Not to suggest every setup-* library is obligated to repeat what the others do, but FWIW setup-python also accepts version ranges as input and does do this to share what exact version was installed.)

@panva
Copy link
Contributor

panva commented Jul 16, 2020

I just stumbled upon this issue while figuring out how to recall cached node_modules with the cache key containing the node version.

- name: Store node version variable
  id: node
  run: |
    echo "::set-output name=version::$(node -v)"
- name: Cache node_modules
  uses: actions/cache@v2
  id: node_modules
  with:
    path: node_modules
    key: ${{ runner.os }}-node_modules-${{ hashFiles('**/package.json') }}-${{ steps.node.outputs.version }}

@bryanmacfarlane How would you suggest to change the key attribute to use $(node -v)?

@panva
Copy link
Contributor

panva commented Jul 17, 2020

See #173

@panva
Copy link
Contributor

panva commented Mar 3, 2021

So are we stuck having to have this extra step everywhere? Why is this present in setup-python but for setup-node it's lack of is as-designed?

🙏 #173

@simondotm
Copy link

simondotm commented May 21, 2021

I was looking here also to see if there was a succinct way to get the node version for convenience, but is there some reason why the strategy.matrix isnt a good enough fall back (rather than getting the node version some other scripty way)?

    strategy:
      matrix:
        node-version: [15.x]
    ...
    key: ${{ runner.os }}-node_modules-${{ matrix.node-version }}-${{ hashFiles('**/package.json') }}

@rdmurphy
Copy link

@simondotm The devil is in the details of how you’re trying to use the Node version so that may be good enough in some cases, but using strategy.matrix will only give you the string of the currently active matrix value. What we’re after here is the exact Node version that was installed as an output.

I’ll again note that setup-python already does this. Why setup-node doesn’t do the same is baffling.

@panva
Copy link
Contributor

panva commented May 21, 2021

#173 implements this.

@Blowmen0w
Copy link

It would be great if there could be an output that tells which exact version of Node.js was installed.

An example use case for this is cacheing node_modules, which is only valid for the same OS + Node.js version + package-lock combination:

name: Node CI

on:
  pull_request:
    branches:
    - master

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2

    - name: Setup Node.js
      id: setup-node
      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-${{ steps.setup-node.outputs.node-version }}-modules-${{ hashFiles('package-lock.json') }}

    - name: Install dependencies
      run: npm ci
      if: steps.cache-node-modules.outputs.cache-hit != 'true'

    - name: Tets
      run: npm test

Release new action version

deining pushed a commit to deining/setup-node that referenced this issue Nov 9, 2023
* feat(pull_strategy): add `NO-PULL` option

* fix: add additional NO-PULL debug log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants