在 ec2 上运行 github 操作时找不到 npm、pm2 命令

问题描述 投票:0回答:2

我正在运行 github 操作工作流程。这是代码:

name: Deploy to EC2

on:
  workflow_dispatch:  # Manual trigger

jobs:
  deploy:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Transfer code to EC2
        uses: appleboy/scp-action@master
        with:
          host: ${{ secrets.EC2_HOST }}  
          username: ${{ secrets.EC2_USERNAME }}  
          key: ${{ secrets.EC2_SSH_KEY }}  
          source: .  # Copy all files from the repository root
          target: /home/ubuntu/app/next/  
      
      - name: SSH into EC2 and Run Script
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.EC2_HOST }}  
          username: ${{ secrets.EC2_USERNAME }}  
          key: ${{ secrets.EC2_SSH_KEY }}  
          script: |
            cd /home/ubuntu/app
            cd next
            npm ci 
            npm run build
            pm2 restart 0

        

当我运行工作流程时,它会抛出此错误:

======CMD======
cd /home/***/app
cd next
npm ci 
npm run build
pm2 restart 0

======END======
err: bash: line 3: npm: command not found
err: bash: line 4: npm: command not found
2023/06/13 03:31:13 Process exited with status 127
err: bash: line 5: pm2: command not found

我在我的 ec2 实例上全局配置了 npm 和 pm2。但每次都会抛出这个错误。我尝试过不同的方法但不起作用

linux npm amazon-ec2 github-actions pm2
2个回答
0
投票

我也有同样的问题。最后,我得到了解决方案。 我解决这个问题的方法是在我的项目的根目录中创建一个bash脚本:pm2_runner.sh,我添加了:

#!/bin/bash
if ! type pm2 > /dev/null
then
  sudo npm install -g pm2 && pm2 start ./index.js 
else
  pm2 restart ./index.js
fi

在 pm2_runner.sh 文件中。

然后在 .github/workflow/ 内的 .yml 文件中,添加了 2 行:

- run: chmod +x ./pm2_runner.sh - run: bash ./pm2_runner.sh
这里是.yml 文件

runs-on: ubuntu-latest strategy: matrix: node-version: [18.x] # See supported Node.js release schedule at https://nodejs.org/en/about/releases/ steps: - uses: actions/checkout@v3 - name: Use Node.js ${{ matrix.node-version }} uses: actions/setup-node@v3 with: node-version: ${{ matrix.node-version }} cache: 'npm' - run: npm ci - run: npm run build --if-present - run: chmod +x ./pm2_runner.sh - run: bash ./pm2_runner.sh

了解更多信息:

Github Actions pm2:找不到命令


0
投票
@iconique 提供的答案是正确的。我只需稍微调整一下它即可指向

.nvm.sh

 的正确位置

要查看您的

.nvm.sh

的确切位置,您需要运行

whereis pm2
就我而言,我更新了 

.yml

 文件,如下所示:

jobs: build: name: Build & Deploy runs-on: ubuntu-latest steps: - name: Deploy to Production uses: appleboy/ssh-action@master with: host: ${{ secrets.SSH_HOST }} username: ${{ secrets.SSH_USERNAME }} password: ${{ secrets.SSH_PASSWORD }} port: ${{ secrets.SSH_PORT }} script: | export NVM_DIR=/home/${{ secrets.SSH_USERNAME }}/.nvm source /home/${{ secrets.SSH_USERNAME }}/.nvm/nvm.sh git stash git pull origin main npm i npm run build
    
© www.soinside.com 2019 - 2024. All rights reserved.