nrwl / nx

Smart Monorepos · Fast CI
https://nx.dev
MIT License
23.69k stars 2.37k forks source link

Agent Nx Tasks not using working-driectory (GitHub Actions) #26661

Open TheWrightDev opened 4 months ago

TheWrightDev commented 4 months ago

Current Behavior

When trying to enable distribution with nx agents the workflow no longer works. I believe this is due to nx not being configured at the root of the repo. Our repository is structured such the the Nx solution exists under /client.

Expected Behavior

Enabling distribution wouldn't break a functioning workflow.

GitHub Repo

No response

Steps to Reproduce

Our workflow:

name: Build Affected

on:
  workflow_call:

# Needed for nx-set-shas when run on the main branch
permissions:
  actions: read
  contents: read

jobs:
  main:
    runs-on: ubuntu-latest
    steps:
      - name: Support longpaths
        run: sudo git config --system core.longpaths true

      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - uses: pnpm/action-setup@v3
        with:
          version: 9

      # Removing this step allows the build to function as expected.
      - name: Enable NX Cloud Agents
        working-directory: client
        run: pnpm dlx nx-cloud start-ci-run --distribute-on="3 linux-medium-js" --stop-agents-after="build"
      # Removing this step allows the build to function as expected.

      - name: Restore cached npm dependencies
        id: cache-dependencies-restore
        uses: actions/cache/restore@v4
        with:
          path: |
            client/node_modules
            ~/.cache/Cypress # needed for the Cypress binary
          key: npm-dependencies-${{ hashFiles('client/pnpm-lock.yaml') }}
          restore-keys: |
            npm-dependencies-

      - name: Install dependencies
        run: pnpm install --frozen-lockfile
        working-directory: client

      - name: Cache npm dependencies
        id: cache-dependencies-save
        if: steps.cache-dependencies-restore.outputs.cache-hit != 'true'
        uses: actions/cache/save@v4
        with:
          path: |
            client/node_modules
            ~/.cache/Cypress # needed for the Cypress binary
          key: ${{ steps.cache-dependencies-restore.outputs.cache-primary-key }}

      - uses: nrwl/nx-set-shas@v3
      - run: git branch --track main origin/main
        if: ${{ github.event_name == 'pull_request' }}

      - name: Build
        working-directory: client
        run: pnpm nx affected -t build -c development --parallel=3

Adding the Nx Cloud Agents step causes a "Error: Could not find lock file. Please ensure you have a lock file before running this command." image

This seemed to be due to the default launch template's Install Node Modules step expecting to find some form of lock file at the root of the repo, which it wouldn't since it's inside /client.

Followed the steps here to create a custom template: https://nx.dev/ci/reference/launch-templates Modified the steps so now it's making it to the Agent Nx Tasks step where it's now complaining that it Could not find Nx Cloud Agent binary. I'm assuming this is also due to it looking in the wrong directory. But sadly not seeing anyway to customize the directory those commands are executed from (beyond simply having the working-directory property which you can see is already on that step). image

Is there anyway to use Nx Agents with a repository structure like this?

Nx Report

Node   : 18.18.0
OS     : win32-x64
pnpm   : 9.4.0

nx (global)        : 18.0.4
nx                 : 18.3.0
@nx/js             : 18.3.0
@nx/jest           : 18.3.0
@nx/eslint         : 18.3.0
@nx/workspace      : 18.3.0
@nx/angular        : 18.3.0
@nx/cypress        : 18.3.0
@nx/devkit         : 18.3.0
@nx/eslint-plugin  : 18.3.0
@nx/plugin         : 18.3.0
@nx/web            : 18.3.0
typescript         : 5.4.5

Failure Logs

No response

Package Manager Version

No response

Operating System

Additional Information

No response

wazbekker commented 4 months ago

I am experiencing the exact same issue. Our nx workspace is not in the root of our git repo. Followed the exact same steps as @TheWrightDev above and am now stuck at the same step. We are however using Azure Devops.

@StalkAltan Is there any resolution for this or maybe a work around?

sebastiandg7 commented 3 months ago

Hi there. We are experiencing the same limitation. I created a install-node-modules step with a working_directory input in AdelanteFinancialHoldings/nx-cloud-workflows. But the agents tasks execution expects the workspace to be at the repository root.

image

To use that step you need to create a custom launch template and set the right working directory. @nixallover, I'd say that to avoid going through modifying each launch template for this purpose, a --working-directory arg for the nx-cloud start-ci-run <...> command would be an easy way for most users.

SuneRadich commented 2 months ago

I am also in the same boat as the rest of you. I want to move my existing nx monorepo to a subfolder /frontend in order for me to add backend stuff in another folder - and eventually tie everything together using nx. But because of this issue, I am not able to get distribution working in my Github workflow.

nixallover commented 1 month ago

Hi folks, sorry for the delayed response. This is an issue that the Nx Cloud team is aware of. We don't have a workaround right now but the work to get Nx Agents working with workspaces in subfolders will be scheduled soon, I'll keep this thread updated. Thanks for your patience!

bhidalgo-apolitical commented 2 weeks ago

@nixallover Any progress updates of this feature coming to Nx soon? We also use a monorepo with different folders and are currently blocked as Nx Agents can't be configured to run outside of root. We tried the same workaround that @sebastiandg7 proposed but the result is the same as in the screenshot he shared