在ec2上运行github操作时未找到npm,pm2命令

8yoxcaq7  于 2023-06-23  发布在  Git
关注(0)|答案(1)|浏览(116)

我正在运行一个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。但它每次都抛出这个错误。我试过不同的方法,但不起作用

yqlxgs2m

yqlxgs2m1#

我也有同样的问题。最后,我找到了解决办法。我使用它的方法是在我的项目的根目录中创建一个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: command not found

相关问题