Search code examples
node.jslinuxgithub-actionsnvm

Self-hosted runner is not finding nvm or Node which are already installed in Linux


I have a Linux server at my home that has a GitHub Actions runner with the following code:

name: Build and Deploy UI - DEV

on:
  workflow_dispatch:
  push:
    branches: [ "dev" ]
    paths:
      - 'UI/**'
  pull_request:
    branches: [ "dev" ]
    paths:
      - 'UI/**'

jobs:
  build_and_deploy:
    if: github.ref == 'refs/heads/dev'
    runs-on: self-hosted
    steps:
    - name: Checkout
      uses: actions/checkout@v4      

    - name: Install Dependencies
      run: |        
        nvm list
        node -v
        npm -v

It is giving me this error

/github/actions-runner/project/_work/_temp/dc7e7354-9aa9-4676-8b50-883e4f150b34.sh:
line 2: nvm: command not found Error: Process completed with exit code 127.

I have tried using nvm and node installed as root and non-root (user running GitHub Actions) but nothing changes in the error message.

Can GitHub Actions use Node.js installed in the server? I don't want to set up Node.js and nvm every time I commit my code to save deployment time.


Solution

  • The issue was that the already installed Node and NVM were not available in the new terminal, which was by default shell (/bin/sh) for the non-root user. You can source the NVM to resolve this issue.

    In my case, to solve this I performed the following operations.

    1. Changed the default shell to bash (/bin/bash) for non-root user

      sudo chsh -s /bin/bash non-root
      
    2. Added .profile (actually copied code from /root path)

      if [ "$BASH" ]; then
         if [ -f ~/.bashrc ]; then
            . ~/.bashrc
         fi
      fi
      
      mesg n 2> /dev/null || true
      
    3. Added .bashrc files

      export HISTCONTROL=ignoreboth:erasedups
      
      export NVM_DIR="$HOME/.nvm"
      [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
      [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completi>
      
    4. Added BASH_ENV: ~/.profile in action runner

      name: Build and Deploy UI - Prod
      
      on:
        workflow_dispatch:
        push:
          branches: [ "main" ]
          paths:
            - 'UI/**'
        pull_request:
          branches: [ "main" ]
          paths:
            - 'UI/**'
      
      env:
        BASH_ENV: ~/.profile
      
      jobs:
        build_and_deploy:
          if: github.ref == 'refs/heads/main'
          runs-on: self-hosted
          steps:
          - name: Checkout
            uses: actions/checkout@v4      
      
          - name: Install Dependencies
            run: |
              cd ./path-to-project-root
              npm ci