admin管理员组

文章数量:1302242

I have a self hosted build agent, which has dotnet tools and nerdbank git versioning installed. If I SSH onto the agent and run nbgv, it exists:

agent@ip-172-18-0-30:~$ which nbgv
/home/agent/.dotnet/tools/nbgv

And I can run it:

agent@ip-172-18-0-30:~$ nbgv -h
Description:
  nbgv v3.7.115+d31f50f4d1

At the start of my pipeline, I am checking the build agent user, the $HOME env var and $PATH env var then trying to run nbgv:

steps:
  - checkout: self
    fetchDepth: 0
    persistCredentials: true
  
  - script: |
      echo "Current user: $(whoami)"
      echo "HOME: $HOME"
      echo "PATH: $PATH"
      ls -la $HOME/.dotnet/tools
      dotnet tool list --global
      nbgv get-version

However it is failing with nbgv: command not found:

/usr/bin/bash --noprofile --norc /home/agent/myagent/_work/_temp/976f3776-1157-4b41-a83c-b9a2e0d5e1da.sh
Current user: agent
HOME: /home/agent
PATH: $HOME/.local/bin:/opt/pipx_bin:$HOME/.dotnet/tools:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
total 84
drwxr-xr-x 3 agent agent  4096 Feb 10 13:36 .
drwxr-xr-x 3 agent agent  4096 Feb 10 13:36 ..
drwxr-xr-x 4 agent agent  4096 Feb 10 13:36 .store
-rwxr-xr-x 1 agent agent 72352 Feb 10 13:36 nbgv

Welcome to .NET 8.0!
---------------------
SDK Version: 8.0.405

Telemetry
---------
The .NET tools collect usage data in order to help us improve your experience. It is collected by Microsoft and shared with the community. You can opt-out of telemetry by setting the DOTNET_CLI_TELEMETRY_OPTOUT environment variable to '1' or 'true' using your favorite shell.

Read more about .NET CLI Tools telemetry: 

----------------
Installed an ASP.NET Core HTTPS development certificate.

To trust the certificate, view the instructions: 

----------------
Write your first app: 
Find out what's new: 
Explore documentation: 
Report issues and find source on GitHub: 
Use 'dotnet --help' to see available commands or visit: 
--------------------------------------------------------------------------------------
Package Id      Version      Commands
-------------------------------------
nbgv            3.7.115      nbgv    
/home/agent/myagent/_work/_temp/976f3776-1157-4b41-a83c-b9a2e0d5e1da.sh: line 6: nbgv: command not found

So despite $HOME being /home/agent and $PATH having $HOME/.dotnet/tools and nbgv being in /home/agent/.dotnet/tools/nbgv, it is failing saying it cant find nbgv. Why is this and how do I fix it?

For reference, my user data script is:

#!/bin/bash

AGENT_USER="agent"
AGENT_HOME="/home/$AGENT_USER"
useradd -m -s /bin/bash $AGENT_USER

export VSTS_AGENT_INPUT_URL=${url}
export VSTS_AGENT_INPUT_AUTH=pat
export VSTS_AGENT_INPUT_TOKEN=${token}
export VSTS_AGENT_INPUT_POOL=${pool}

echo "export VSTS_AGENT_INPUT_URL=${url}" >> $AGENT_HOME/.bashrc
echo "export VSTS_AGENT_INPUT_AUTH=pat" >> $AGENT_HOME/.bashrc
echo "export VSTS_AGENT_INPUT_TOKEN=${token}" >> $AGENT_HOME/.bashrc
echo "export VSTS_AGENT_INPUT_POOL=${pool}" >> $AGENT_HOME/.bashrc

chown -R $AGENT_USER:$AGENT_USER $AGENT_HOME

cd $AGENT_HOME

wget .248.0/vsts-agent-linux-x64-4.248.0.tar.gz
mkdir myagent && cd myagent
tar zxvf ../vsts-agent-linux-x64-4.248.0.tar.gz
./bin/installdependencies.sh
chown -R $AGENT_USER:$AGENT_USER .
su $AGENT_USER -c './config.sh --unattended'
echo "HOME=$AGENT_HOME" >> .env
./svc.sh install $AGENT_USER

usermod -aG docker $AGENT_USER

./svc.sh start

mkdir -p $AGENT_HOME/myagent/_work
chown -R $AGENT_USER:$AGENT_USER $AGENT_HOME/myagent/_work

I have a self hosted build agent, which has dotnet tools and nerdbank git versioning installed. If I SSH onto the agent and run nbgv, it exists:

agent@ip-172-18-0-30:~$ which nbgv
/home/agent/.dotnet/tools/nbgv

And I can run it:

agent@ip-172-18-0-30:~$ nbgv -h
Description:
  nbgv v3.7.115+d31f50f4d1

At the start of my pipeline, I am checking the build agent user, the $HOME env var and $PATH env var then trying to run nbgv:

steps:
  - checkout: self
    fetchDepth: 0
    persistCredentials: true
  
  - script: |
      echo "Current user: $(whoami)"
      echo "HOME: $HOME"
      echo "PATH: $PATH"
      ls -la $HOME/.dotnet/tools
      dotnet tool list --global
      nbgv get-version

However it is failing with nbgv: command not found:

/usr/bin/bash --noprofile --norc /home/agent/myagent/_work/_temp/976f3776-1157-4b41-a83c-b9a2e0d5e1da.sh
Current user: agent
HOME: /home/agent
PATH: $HOME/.local/bin:/opt/pipx_bin:$HOME/.dotnet/tools:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
total 84
drwxr-xr-x 3 agent agent  4096 Feb 10 13:36 .
drwxr-xr-x 3 agent agent  4096 Feb 10 13:36 ..
drwxr-xr-x 4 agent agent  4096 Feb 10 13:36 .store
-rwxr-xr-x 1 agent agent 72352 Feb 10 13:36 nbgv

Welcome to .NET 8.0!
---------------------
SDK Version: 8.0.405

Telemetry
---------
The .NET tools collect usage data in order to help us improve your experience. It is collected by Microsoft and shared with the community. You can opt-out of telemetry by setting the DOTNET_CLI_TELEMETRY_OPTOUT environment variable to '1' or 'true' using your favorite shell.

Read more about .NET CLI Tools telemetry: https://aka.ms/dotnet-cli-telemetry

----------------
Installed an ASP.NET Core HTTPS development certificate.

To trust the certificate, view the instructions: https://aka.ms/dotnet-https-linux

----------------
Write your first app: https://aka.ms/dotnet-hello-world
Find out what's new: https://aka.ms/dotnet-whats-new
Explore documentation: https://aka.ms/dotnet-docs
Report issues and find source on GitHub: https://github/dotnet/core
Use 'dotnet --help' to see available commands or visit: https://aka.ms/dotnet-cli
--------------------------------------------------------------------------------------
Package Id      Version      Commands
-------------------------------------
nbgv            3.7.115      nbgv    
/home/agent/myagent/_work/_temp/976f3776-1157-4b41-a83c-b9a2e0d5e1da.sh: line 6: nbgv: command not found

So despite $HOME being /home/agent and $PATH having $HOME/.dotnet/tools and nbgv being in /home/agent/.dotnet/tools/nbgv, it is failing saying it cant find nbgv. Why is this and how do I fix it?

For reference, my user data script is:

#!/bin/bash

AGENT_USER="agent"
AGENT_HOME="/home/$AGENT_USER"
useradd -m -s /bin/bash $AGENT_USER

export VSTS_AGENT_INPUT_URL=${url}
export VSTS_AGENT_INPUT_AUTH=pat
export VSTS_AGENT_INPUT_TOKEN=${token}
export VSTS_AGENT_INPUT_POOL=${pool}

echo "export VSTS_AGENT_INPUT_URL=${url}" >> $AGENT_HOME/.bashrc
echo "export VSTS_AGENT_INPUT_AUTH=pat" >> $AGENT_HOME/.bashrc
echo "export VSTS_AGENT_INPUT_TOKEN=${token}" >> $AGENT_HOME/.bashrc
echo "export VSTS_AGENT_INPUT_POOL=${pool}" >> $AGENT_HOME/.bashrc

chown -R $AGENT_USER:$AGENT_USER $AGENT_HOME

cd $AGENT_HOME

wget https://vstsagentpackage.azureedge/agent/4.248.0/vsts-agent-linux-x64-4.248.0.tar.gz
mkdir myagent && cd myagent
tar zxvf ../vsts-agent-linux-x64-4.248.0.tar.gz
./bin/installdependencies.sh
chown -R $AGENT_USER:$AGENT_USER .
su $AGENT_USER -c './config.sh --unattended'
echo "HOME=$AGENT_HOME" >> .env
./svc.sh install $AGENT_USER

usermod -aG docker $AGENT_USER

./svc.sh start

mkdir -p $AGENT_HOME/myagent/_work
chown -R $AGENT_USER:$AGENT_USER $AGENT_HOME/myagent/_work
Share asked Feb 10 at 21:22 Tom McLeanTom McLean 6,3691 gold badge21 silver badges49 bronze badges 2
  • Variables aren't expanded when using PATH; you must set PATH to contain the value (i.e. expansion) of $HOME, not literal dollarsign-H-O-M-E. (This differs from Windows where the PATH registry entry has a special type, REG_EXPAND_SZ, which does cause such 'recursive' expansion when it is read from the registry.) Similarly ~ in PATH isn't expanded, but it usually is and must be in a shell command that uses it to set PATH. Alternatively you could use e.g. ./nbgv to specify the location of nbgv so PATH isn't needed. – dave_thompson_085 Commented Feb 10 at 22:22
  • I have created my AMI based on the azure runner image repo which adds that $HOME/.dotnet/tools to /etc/environment (github/actions/runner-images/blob/main/images/ubuntu/… line 92), how should I load this correctly for the build agent user? Why does it work for the build agent user, but not when I run the build agent as a service? – Tom McLean Commented Feb 10 at 22:42
Add a comment  | 

1 Answer 1

Reset to default 1

Based on your description, I could reproduce the issue when I installed nbgv after I setup the self-hosted pipeline agent.

As suggested in this document, we need to update environment variables after installing new software.

./env.sh
sudo ./svc.sh stop
sudo ./svc.sh start

本文标签: linuxSelf hosted azure devops build agent not getting tool from PATHStack Overflow