azureazure-devopsazure-pipelinesazure-devops-self-hosted-agent

self hosted linux agent: package.json not in _work directory: npm ERR! enoent ENOENT for package.json


I am running a local CentOs server. On that I have succesfully created a self hosted Linux agent.

Also had to install git, node and npm on that system and the first runs just echoing a command were successful.

Now I would like to build an Angular application with it using a pipeline.

I've started out with a blank boilerplate Angular project via nx.

In the project root, there are the nx/angular files and also a package.json.

./client/
./client/package.json
./client/apps
./client/libs
./client/node_modules
./client/tsconfig.base.json
./client/azure/azure-pipelines.yml # the pipeline file is here
./client/etc...

I've started out with this azure-pipelines.yml file:

trigger:
  - main

pool:
  name: some-pool
  demands:
    - Agent.Name -equals some-vm0

steps:
  - script: npm i
    displayName: npm install

In the pipeline job I get the following output:

npm ERR! code ENOENT
npm ERR! syscall open
npm ERR! path /home/username/myagent/_work/package.json
npm ERR! errno -2
npm ERR! enoent ENOENT: no such file or directory, open '/home/username/myagent/_work/package.json'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent 

So it looks like it cannot find the file package.json in the correct location.

How does it work exactly?

The first step in the pipeline is a "Starting: Checkout Repository Name@main to s" and shows that it's "Receiving objects". I guess that would contain the package.json file. Where are those files stored on the agents machine?


Solution

  • So there is a set of predefined variables that you can use.

    Did a bunch of them and eventually found out you can use:

      - script: |
          ls -la $(Agent.BuildDirectory)/s
        displayName: List Contents of Build Directory/s
      - script: |
          ls -la $(Build.SourcesDirectory)
        displayName: List Contents of Build.SourcesDirectory
      - script: |
          ls -la $(System.DefaultWorkingDirectory)
        displayName: List Contents of System.DefaultWorkingDirectory
    

    To get the location where the files of the repository are copied to.

    Since traversing is not ideal (../client) we can use the sourcesdirectory and client folder together:

    trigger:
      - main
    
    pool:
      name: some-pool
      demands:
        - Agent.Name -equals some-vm0
    
    variables:
      ClientRootFolder: 'client' # add a variable which you can re-use
    
    steps:
      - script: npm i
        displayName: npm install
        workingDirectory: $(Build.SourcesDirectory)/$(ClientRootFolder)