使用Serverless GitHub Action时遭遇“serverless not found”错误求助
When using the Serverless GitHub Action with multiple plugins, following the plugin usage guidance leads to a /bin/sh: 1: serverless: not found error—even after adding serverless as a dependency in package.json.
Your original workflow configuration:
name: Deploy main branch on: push: branches: - main jobs: deploy: name: deploy runs-on: ubuntu-latest strategy: matrix: node-version: [18.x] steps: - uses: actions/checkout@v3 - name: Use Node.js ${{ matrix.node-version }} uses: actions/setup-node@v3 with: node-version: ${{ matrix.node-version }} - run: npm ci - name: serverless deploy uses: serverless/github-action@v3.2 with: args: -c "cd backend/endpoints && \ serverless plugin install -n serverless-deployment-bucket && \ serverless plugin install -n serverless-python-requirements && \ serverless plugin install -n serverless-iam-roles-per-function && \ serverless plugin install -n serverless-offline && \ serverless deploy" entrypoint: /bin/sh env: SERVERLESS_ACCESS_KEY: ${{ secrets.SERVERLESS_ACCESS_KEY }}
Root Cause
Overriding the action's entrypoint to /bin/sh breaks access to the Serverless CLI provided by the action itself. Additionally, the locally installed serverless (via npm ci) isn't in the shell's PATH when using this custom entrypoint, so the command can't be located.
Solutions
Solution 1: Use Locally Installed Serverless with npx
Modify the args to use npx to invoke the locally installed Serverless CLI (available after npm ci runs):
- name: serverless deploy uses: serverless/github-action@v3.2 with: args: -c "cd backend/endpoints && \ npx serverless plugin install -n serverless-deployment-bucket && \ npx serverless plugin install -n serverless-python-requirements && \ npx serverless plugin install -n serverless-iam-roles-per-function && \ npx serverless plugin install -n serverless-offline && \ npx serverless deploy" entrypoint: /bin/sh env: SERVERLESS_ACCESS_KEY: ${{ secrets.SERVERLESS_ACCESS_KEY }}
npx automatically finds the serverless binary in node_modules/.bin without needing to adjust PATH.
Solution 2: Avoid Overriding Entrypoint (Recommended)
Split plugin installation into a separate step and let the action handle the Serverless CLI invocation directly. This aligns with the action's intended usage and is cleaner:
name: Deploy main branch on: push: branches: - main jobs: deploy: name: deploy runs-on: ubuntu-latest strategy: matrix: node-version: [18.x] steps: - uses: actions/checkout@v3 - name: Use Node.js ${{ matrix.node-version }} uses: actions/setup-node@v3 with: node-version: ${{ matrix.node-version }} - run: npm ci # Install Serverless plugins in a dedicated step - name: Install Serverless plugins run: | serverless plugin install -n serverless-deployment-bucket serverless plugin install -n serverless-python-requirements serverless plugin install -n serverless-iam-roles-per-function serverless plugin install -n serverless-offline working-directory: backend/endpoints env: SERVERLESS_ACCESS_KEY: ${{ secrets.SERVERLESS_ACCESS_KEY }} # Use the action's default entrypoint for deployment - name: Serverless deploy uses: serverless/github-action@v3.2 with: args: deploy working-directory: backend/endpoints env: SERVERLESS_ACCESS_KEY: ${{ secrets.SERVERLESS_ACCESS_KEY }}
This approach uses the Serverless CLI provided by the action, though keeping serverless in package.json is still recommended for consistency.
Package.json Configuration
Ensure your package.json includes serverless as a dependency (either in dependencies or devDependencies):
{ "devDependencies": { "serverless": "^3.38.0" } }
Run npm install serverless --save-dev locally to update your package.json before pushing changes.
内容的提问来源于stack exchange,提问作者Amir Mousavi

