如何在 Github 中指定操作/设置节点的路径

Posted

技术标签:

【中文标题】如何在 Github 中指定操作/设置节点的路径【英文标题】:How to specifiy path for actions/setup-node in Github 【发布时间】:2021-10-02 06:38:03 【问题描述】:

注意:这两个我已经看过了:

How do I run my CI steps in a specific folder in github action How to specify node's path in Github action?

但我仍然无法让它工作,这就是为什么我要问我如何能够为uses 命令设置工作目录。我的 yaml 目前看起来如下:

# This workflow will build a Java project with Maven
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven

name: Java CI with Maven

on:
  push:
    branches: [ main, Create-.yml-file ]
  pull_request:
    branches: [ main, Create-.yml-file ]

jobs:
  javatest:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - uses: actions/setup-java@v2
      with:
        java-version: '16'
        distribution: 'adopt'
    - name: Cache Maven packages
      uses: actions/cache@v2
      with:
        path: ~/.m2
        key: $ runner.os -m2-$ hashFiles('**/pom.xml') 
        restore-keys: $ runner.os -m2
    - name: Build with Maven
      run: |
          mvn -f ./backend/pom.xml -B test
          #mvn -f ./notification/pom.xml -B test

    - name: Generate JaCoCo Badge
      uses: cicirello/jacoco-badge-generator@v2
      with:
        generate-branches-badge: true
        on-missing-report: quiet
        jacoco-csv-file: >
          -backend/target/site/jacoco/jacoco.csv

    - name: Log coverage percentage
      run: |
        echo "coverage = $ steps.jacoco.outputs.coverage "
        echo "branch coverage = $ steps.jacoco.outputs.branches "

    - name: Commit the badge (if it changed)
      run: |
        if [[ `git status --porcelain` ]]; then
          git config --global user.name 'myusername'
          git config --global user.email 'myId@users.noreply.github.com'
          git add -A
          git commit -m "Autogenerated JaCoCo coverage badge"
          git push
        fi

    - name: Upload JaCoCo coverage report
      uses: actions/upload-artifact@v2
      with:
        name: jacoco-report
        path: target/site/jacoco/
      
  nodejstest:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Use Node.js $ matrix.node-version 
      uses: actions/setup-node@v2
      with:
        node-version: $ matrix.node-version 
        cache: 'npm'
    - run: npm ci
      working-directory: ./frontend
    - run: npm run build --if-present
      working-directory: ./frontend
    - run: npm test
      working-directory: ./frontend

这里发生错误:

 - name: Use Node.js $ matrix.node-version 
      uses: actions/setup-node@v2
      with:
        node-version: $ matrix.node-version 
        cache: 'npm'

看起来像这样:

Run actions/setup-node@v2
/usr/local/bin/npm config get cache
/home/runner/.npm
Error: Dependencies lock file is not found in /home/runner/work/path/to/main/directory. Supported file patterns: package-lock.json,yarn.lock

我的 package-lock ist 位于 .../path/to/main/directory/frontend 所以很明显它找不到但是根据其他两个解决方案这个 sn-p 应该工作不应该它?我也已经尝试将最后三个运行语句组合到以及将工作目录设置移动到不同的位置。都有不同程度的失败

【问题讨论】:

"At the moment, only lock files in the project root are supported" @jonrsharpe 可能使用缓存依赖路径 【参考方案1】:

在 2.4 版中添加了对自定义路径(相对于存储库根目录)的支持: https://github.com/actions/setup-node/releases/tag/v2.4.0

    您可以尝试在 cache-dependency-path(它不适用于其他模式)。 您还可以尝试将working directory(适用于所有作业或您的nodejstest 特定作业)设置为指向您的frontend 文件夹。
  nodejstest:
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: 'frontend' # Here the path to the folder where package-lock.json is located.
    strategy:
      matrix:
        node-version: [16.x] # Are you are missing this specification?
    steps:
    - uses: actions/checkout@v2
    - name: Use Node.js $ matrix.node-version 
      uses: actions/setup-node@v2
      with:
        node-version: $ matrix.node-version 
        cache: 'npm'
        cache-dependency-path: '**/package-lock.json' # THIS PATTERN did the trick for me.

【讨论】:

"尝试在缓存依赖路径中指定 '**/package-lock.json'" 【参考方案2】:
setup_build_environment:

runs-on: ubuntu-latest
defaults:
  run:
    shell: bash
strategy:
  matrix:
    node-version: [14.x]
    # See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:


- uses: actions/checkout@v2
- name: Use Node.js $ matrix.node-version 
  uses: actions/setup-node@v2
  with:
    node-version: $ matrix.node-version 
    cache: 'yarn'
    cache-dependency-path: ./node-app/yarn.lock

- name: run npm commands
  working-directory: node-app
  run: | 
    yarn && CI=true
    # yarn build --if-present
    # yarn test
    node test.js

参考:https://github.com/actions/setup-node#caching-packages-dependencies

该操作默认在存储库根目录中搜索依赖文件(package-lock.json 或 yarn.lock),并将其哈希用作缓存键的一部分。当使用多个依赖文件,或者它们位于不同的子目录时,使用 cache-dependency-path。

我使用过yarn,并且node应用驻留在/node-app目录下,所以在cache-dependency-path中我使用yarn的锁文件。并使用 working-directory 在 node-app 目录中运行 yarn 命令。

【讨论】:

【参考方案3】:

您可以将节点设置和缓存分解为两个步骤,如下所示

  nodejstest:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Use Node.js $ matrix.node-version 
      uses: actions/setup-node@v2
      with:
        node-version: $ matrix.node-version 
    - name : Cache
      uses: actions/cache@v2
      with:
        path: "frontend/node_modules"
        key: node-modules-$ hashFiles('frontend/package.json') 
    - run: npm ci
      working-directory: ./frontend
    - run: npm run build --if-present
      working-directory: ./frontend
    - run: npm test
      working-directory: ./frontend

【讨论】:

以上是关于如何在 Github 中指定操作/设置节点的路径的主要内容,如果未能解决你的问题,请参考以下文章

如果没有在 Github Actions 中指定,使用啥节点版本

如何设置 React 自定义端口,如 3129

C# 如何得到一XML文件中指定的节点属性值

基于 Azure AD 证书的身份验证:如何在 Node JS 中指定证书路径?

sudo:没有 tty 存在,也没有在 github 操作中指定 askpass 程序

如何在 Wix Installer 中指定安装目录路径