Skip to content

Deploy failing: This command can only be run in a Serverless service directory. #53

Description

@Blackglade

I have a mono repo project and a separate directory that contains the serverless yaml file. Is there a way to set the directory to execute from in the github action.

I tried doing something like this:

    - name: Navigate to serverless
      run: cd serverless

    - name: Serverless Deploy
      uses: serverless/github-action@master
      with:
        args: deploy
      env:
        AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
        AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}

but without any luck.

Activity

  1. the1adi commented on Jun 10, 2021

    @the1adi

    I am experiencing the same issue. I tried using the working directory setting and it still did not work.

    This is my main.yml:

    name: Deploy master branch
    
    on:
        push:
            branches:
                - master
    
    jobs:
        deploy:
            name: deploy
            runs-on: ubuntu-latest
            defaults:
                run:
                    working-directory: ./app
            strategy:
                matrix:
                    node-version: [12.x]
            steps:
                - uses: actions/checkout@v2
                - name: Use Node.js ${{ matrix.node-version }}
                  uses: actions/setup-node@v1
                  with:
                      node-version: ${{ matrix.node-version }}
                - run: npm ci
                - name: serverless deploy
                  uses: serverless/github-action@master
                  with:
                      args: deploy
                  env:
                      # SERVERLESS_ACCESS_KEY: ${{ secrets.SERVERLESS_ACCESS_KEY }}
                      # or if using AWS credentials directly
                      AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
                      AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
    
    

    This gave me the same error.

  2. deathemperor commented on Jun 21, 2021

    @deathemperor

    couldn't find anyway to make it run either. I think it's because of this:

    -v "/home/runner/work/berry/berry":"/github/workspace"

    By default the serverless docker maps the parent folder. Would be nice if there's an option to change that.

  3. deathemperor commented on Jun 21, 2021

    @deathemperor

    Ended up writing my own commands.

    name: GitHub Actions Deploy ID Dev
    on:
      push:
        branches:
          - main
    jobs:
      deploy:
        name: deploy
        runs-on: ubuntu-latest
        strategy:
          matrix:
            node-version: [14.x]
        steps:
          - uses: actions/checkout@v2
          - name: Use Node.js ${{ matrix.node-version }}
            uses: actions/setup-node@v1
            with:
              node-version: ${{ matrix.node-version }}
          - run: yarn install
          - run: yarn workspace @berry/shared build
          - run: node ./node_modules/serverless/bin/serverless.js config credentials --provider aws --key ${{ secrets.AWS_ACCESS_KEY_ID }} --secret ${{ secrets.AWS_SECRET_ACCESS_KEY }} --profile papaya --overwrite
          - run: node ../../node_modules/serverless/bin/serverless.js deploy --aws-s3-accelerate --stage dev --verbose
            working-directory: ./services/id
            env:
              SERVERLESS_ACCESS_KEY: ${{ secrets.SERVERLESS_ACCESS_KEY }}
              AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
              AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
              SLS_DEBUG: 1
  4. added a commit that references this issue on Jul 14, 2021
  5. russellc92 commented on Aug 16, 2021

    @russellc92

    You need to set the config variable on the serverless deploy command to the path of the serverless.yml file:

    name: serverless deploy
    uses: serverless/github-action@master
    with:
      args: deploy -s stage -c path/to/file/serverless.yml
    
  6. peimanja commented on Aug 20, 2021

    @peimanja

    You need to set the config variable on the serverless deploy command to the path of the serverless.yml file:

    name: serverless deploy
    uses: serverless/github-action@master
    with:
      args: deploy -s stage -c path/to/file/serverless.yml
    

    This does not work if you are loading other files in your serverless.yml .

     Serverless Warning --------------------------------------
     
      A valid file to satisfy the declaration 'file(dev-config.yaml):environment' could not be found.
    
  7. Blackglade commented on Aug 26, 2021

    @Blackglade
    Author

    You need to set the config variable on the serverless deploy command to the path of the serverless.yml file:

    name: serverless deploy
    uses: serverless/github-action@master
    with:
      args: deploy -s stage -c path/to/file/serverless.yml
    

    This still doesn't work due to the fact that my repo is a monorepo and so all my serverless functions are inside a serverless folder inside my repo.

    Repo Structure looks something like this

    /package.json
    /<ReactJS Project Files>
    /serverless
    /serverless/serverless.yml
    /serverless/index.js <- contains function info
    /serverless/package.json
    

    Here was the deploy command I was using but couldn't get it to work:

    name: Deploy to AWS Lambda
    
    on:
      push:
        branches: [ master ]
    
    jobs:
      build:
        runs-on: ubuntu-latest
        steps:
        - name: Checkout Code
          uses: actions/checkout@v2
          
        - name: Build w/ Node.js 14.x
          uses: actions/setup-node@v2
          with:
            node-version: 14.x
        - run: npm ci
          working-directory: ./serverless
    
        - name: Serverless Deploy
          uses: serverless/github-action@master
          with:
            args: -c "serverless plugin install --name serverless-domain-manager && serverless deploy -c serverless/serverless.yml -v"
            entrypoint: /bin/sh
          env:
            AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
            AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
    

    Honestly have sort of given up on this. It's clear Serverless has no intention of providing support for github actions.

  8. russellc92 commented on Aug 26, 2021

    @russellc92

    You need to set the config variable on the serverless deploy command to the path of the serverless.yml file:

    name: serverless deploy
    uses: serverless/github-action@master
    with:
      args: deploy -s stage -c path/to/file/serverless.yml
    

    This still doesn't work due to the fact that my repo is a monorepo and so all my serverless functions are inside a serverless folder inside my repo.

    Repo Structure looks something like this

    /package.json
    /<ReactJS Project Files>
    /serverless
    /serverless/serverless.yml
    /serverless/index.js <- contains function info
    /serverless/package.json
    

    Here was the deploy command I was using but couldn't get it to work:

    name: Deploy to AWS Lambda
    
    on:
      push:
        branches: [ master ]
    
    jobs:
      build:
        runs-on: ubuntu-latest
        steps:
        - name: Checkout Code
          uses: actions/checkout@v2
          
        - name: Build w/ Node.js 14.x
          uses: actions/setup-node@v2
          with:
            node-version: 14.x
        - run: npm ci
          working-directory: ./serverless
    
        - name: Serverless Deploy
          uses: serverless/github-action@master
          with:
            args: -c "serverless plugin install --name serverless-domain-manager && serverless deploy -c serverless/serverless.yml -v"
            entrypoint: /bin/sh
          env:
            AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
            AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
    

    Honestly have sort of given up on this. It's clear Serverless has no intention of providing support for github actions.

    Seems like the solution i provided previously only works for single lambdas.
    I apologise that this does not represent most cases that people encounter in real life scenarios.

    I have had luck calling serverless directly via npx, see here as an example. CLI commands/flags should work as indentended - but I havn't tried with a monorepo setup or using other yml files in the serverless.yml file (hopefully is not the same result as before).

    I am keen to see the result of such running serverless with the above method - hopefully we can help progress this GHA.

  9. StephenGrable1 commented on Aug 29, 2021

    @StephenGrable1

    Anyone encountering this thread who's following best practices and splitting their serverless app into separate directories, follow @deathemperor solution above (or my slightly simplified one below) to roll your own deployment step for each of your services.

    Any semi-mature application will eventually need to split their code. I recommend you do not consolidate your separate services into the root folder just so this github action will work -- you will end up having to backtrack once your app grows.

    It would be great if we could add support for this eventually, but in the meantime, I'm dropping this github action and using this manual deployment setup:

    name: GitHub Actions Deploy Prod
    on:
      push:
        branches:
          - master
    jobs:
      deploy:
        name: deploy
        runs-on: ubuntu-latest
        strategy:
          matrix:
            node-version: [14.x]
        steps:
          - uses: actions/checkout@v2
          - name: Use Node.js ${{ matrix.node-version }}
            uses: actions/setup-node@v1
            with:
              node-version: ${{ matrix.node-version }}
          - run: npm install
          - run: npm run build
          - run: node ./node_modules/serverless/bin/serverless.js config credentials --provider aws --key ${{ secrets.AWS_ACCESS_KEY_ID }} --secret ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          - run: node ../../node_modules/serverless/bin/serverless.js deploy --stage prod --verbose
            working-directory: ./serverless/backend-api
            env:
              AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
              AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
              SLS_DEBUG: 1
    

    thanks again for the suggestion @deathemperor

  10. hutch120 commented on Nov 22, 2021

    @hutch120

    Update... if you add org and app properties to serverless.yaml then the serverless client requires a SERVERLESS_ACCESS_KEY configured in serverless.com UX console.

    My subdirectory is called server, note the use of working-directory at top level rather than for each step.

    name: Deploy main branch serverless
    
    on:
      push:
        branches:
          - main
    
    jobs:
      deploy:
        name: deployserver
        runs-on: ubuntu-latest
        defaults:
          run:
            working-directory: ./server
        strategy:
          matrix:
            node-version: [14.x]
        steps:
          - uses: actions/checkout@v2
          - name: Use Node.js ${{ matrix.node-version }}
            uses: actions/setup-node@v1
            with:
              node-version: ${{ matrix.node-version }}
          - run: yarn
          - run: node ./node_modules/serverless/bin/serverless.js deploy --stage prod --verbose
            env:
              SERVERLESS_ACCESS_KEY: ${{ secrets.SERVERLESS_ACCESS_KEY }}
              SLS_DEBUG: 1
    
  11. pgrzesik commented on Nov 22, 2021

    @pgrzesik

    @hutch120 Could you elaborate more what is the problem here? Serverless never requires the use of SERVERLESS_ACCESS_KEY, what makes you think that is the case?

  12. hutch120 commented on Nov 22, 2021

    @hutch120

    @pgrzesik Thanks for the note. Sure... here are logs of attempting to run without SERVERLESS_ACCESS_KEY. Literally the only change required to get my deploy to work is to add a valid SERVERLESS_ACCESS_KEY to the env section of the yaml file.

    Run node ./node_modules/serverless/bin/serverless.js config credentials --provider aws --key *** --secret ***
      node ./node_modules/serverless/bin/serverless.js config credentials --provider aws --key *** --secret ***
      shell: /usr/bin/bash -e {0}
    Serverless: Setting up AWS...
    2s
    Run node ./node_modules/serverless/bin/serverless.js deploy --stage prod --verbose
      node ./node_modules/serverless/bin/serverless.js deploy --stage prod --verbose
      shell: /usr/bin/bash -e {0}
      env:
        AWS_ACCESS_KEY_ID: ***
        AWS_SECRET_ACCESS_KEY: ***
     
     Serverless Error ----------------------------------------
     
      You are not currently logged in. Follow instructions in http://slss.io/run-in-cicd to setup env vars for authentication.
    
  13. pgrzesik commented on Nov 22, 2021

    @pgrzesik

    Hey @hutch120 - thanks for responding. Based on the error message it seems like you're using Serverless Dashboard integration in your service - do you have org and app properties defined in your serverless.yml? If that's the case, then yes, SERVERLESS_ACCESS_KEY is required, because otherwise it would be impossible to use the Dashboard integration. If you are not using Dashboard, you can remove the org and app setttings and key will be no longer needed.

  14. hutch120 commented on Nov 22, 2021

    @hutch120

    @pgrzesik Thanks for the info. I stand corrected. I've updated my initial comment. FYI I guess the reason I got to this conclusion was by following along with this general guide and this CI guide

  15. pgrzesik commented on Nov 23, 2021

    @pgrzesik

    Hey @hutch120 - happy I could help clear up some misunderstanding 👍 As for these guides, I'm looking at them and I see that the second one about CI setup might definitely be misleading, as it suggests that if you're using CI, then you need to use Dashboard which is definitely not the case. I'll bring it up internally so we can clear that confusion in documentation as well 👍

  16. Schachte commented on Jan 16, 2022

    @Schachte

    https://ryan-schachte.com/posts/serverless-deployments-with-github-actions-on-aws

    I wrote a post setting this deployment up on Github actions. Might help some people out there.

  17. nikhuber commented on Mar 5, 2022

    @nikhuber

    I handled this issue by overriding the ENTRYPOINT and changing to the directory that contains my serverless.yml this way

    jobs:
      deploy:
        name: deployserver
    ...
        steps:
          - name: serverless deploy
            uses: serverless/github-action@v3
            with:
              args: -c "cd ./<your-dir> && serverless deploy"
              entrypoint: /bin/sh
    

    Change <your-dir> to your target directory.

  18. added a commit that references this issue on Apr 25, 2022
  19. added a commit that references this issue on May 17, 2022
  20. DavideViolante commented on May 31, 2022

    @DavideViolante
    Contributor

    Thanks, I added this info in the readme.

  21. amirhmk commented on May 29, 2023

    @amirhmk

    I handled this issue by overriding the ENTRYPOINT and changing to the directory that contains my serverless.yml this way

    jobs:
      deploy:
        name: deployserver
    ...
        steps:
          - name: serverless deploy
            uses: serverless/github-action@v3
            with:
              args: -c "cd ./<your-dir> && serverless deploy"
              entrypoint: /bin/sh
    

    Change <your-dir> to your target directory.

    So I am doing this now, but get /bin/sh: 1: serverless: not found error. Am I doing something wrong? What should the content of the package.json look like? As in does it need any specific dependencies for npm ci?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions