首页 > 解决方案 > 如何在 Github Actions 和 Monorepo 中使用子文件夹?

问题描述

它有一个 monorepo,其中将包含两个子文件夹,它们是:

文件夹

每个都有各自的项目和包。我正在尝试访问某个子文件夹以执行其各自的操作,但是当我运行命令以使用 lint 进行测试时出现错误,即:

error Couldn't find a package.json file in "/github/workspace"

它可能不应该访问前端子文件夹。我需要它来运行这个子文件夹中的所有命令,我该怎么做?

我的.YML

name: PIPELINE OF TESTS

on:
  push:
    branches: [frontend-develop, backend-develop]
  pull_request_target:
    types: [opened, edited, closed]
    branches: [main]

jobs:
  test-frontend:
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: ./frontend
    strategy:
      matrix:
        node-version: [14.x]
        architecture: [x64]
    steps:
      - name: CHECK-OUT GIT REPOSITORY
        uses: actions/checkout@v2

      - name: USE NODEJS ${{ matrix.node-version }} - ${{ matrix.architecture }}
        uses: actions/setup-node@v2

      - name: INSTALL PROJECT DEPENDENCIES (YARN)
        uses: borales/actions-yarn@v2.0.0
        with:
          cmd: install

      - name: CODE ANALYSE (LINT) AND STYLE-GUIDE ANALYSE (PRETTIER + AIRBNB)
        uses: borales/actions-yarn@v2.0.0
        with:
          cmd: lint-check

      - name: UNIT TEST (JEST)
        uses: borales/actions-yarn@v2.0.0
        with:
          cmd: test

标签: github-actions

解决方案


使用defaultswithrun只会应用于运行步骤(例如,您自己执行的脚本/命令而不是操作)。请参阅文档

为作业中的所有步骤提供默认值shell和。本节不允许使用上下文和表达式。working-directoryrun

当您使用 GitHub 操作(您有uses:)时,无法更改工作目录。请记住,某些操作支持这一点 - 您可以将附加参数传递给with:,但在您的情况下borales/actions-yarn不支持。

你能做什么?

正如borales/actions-yarn REAME.md中所建议的:

请记住,这个 Action 最初是为 GitHub Actions beta 编写的(当时 Docker 是唯一的做事方式)。考虑使用操作/设置节点来处理 Yarn。该存储库将主要支持现有流程。

您可以删除这些操作并直接在run:. 您的工作流程应如下所示:

name: PIPELINE OF TESTS

on:
  push:
    branches: [frontend-develop, backend-develop]
  pull_request_target:
    types: [opened, edited, closed]
    branches: [main]

jobs:
  test-frontend:
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: ./frontend
    strategy:
      matrix:
        node-version: [14.x]
        architecture: [x64]
    steps:
      - name: CHECK-OUT GIT REPOSITORY
        uses: actions/checkout@v2

      - name: USE NODEJS ${{ matrix.node-version }} - ${{ matrix.architecture }}
        uses: actions/setup-node@v2

      - name: INSTALL PROJECT DEPENDENCIES (YARN)
        run: yarn install

      - name: CODE ANALYSE (LINT) AND STYLE-GUIDE ANALYSE (PRETTIER + AIRBNB)
        run: yarn lint-check

      - name: UNIT TEST (JEST)
        run: yarn test

推荐阅读