Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Blackglade opened this issue Jun 8, 2021 · 18 comments

Comments

@Blackglade
Copy link

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.

@the1adi
Copy link

the1adi commented Jun 10, 2021

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.

@deathemperor
Copy link

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.

@deathemperor
Copy link

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

timarney added a commit to cds-snc/platform-mvp that referenced this issue Jul 14, 2021
* initial setup
* add serverless deploy (see issue: serverless/github-action#53)
* specify a single node version
@russellc92
Copy link

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

@peimanja
Copy link

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.

@Blackglade
Copy link
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.

@russellc92
Copy link

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.

@StephenGrable1
Copy link

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

zuka-e added a commit to zuka-e/laravel-react-task-spa that referenced this issue Sep 20, 2021
to deal with a Serverless Error `This command can only be run in a Serverless service directory.`

see: serverless/github-action#53
zuka-e added a commit to zuka-e/laravel-react-task-spa that referenced this issue Sep 20, 2021
zuka-e added a commit to zuka-e/laravel-react-task-spa that referenced this issue Sep 20, 2021
to deal with a Serverless Error `This command can only be run in a Serverless service directory.`

see: serverless/github-action#53
zuka-e added a commit to zuka-e/laravel-react-task-spa that referenced this issue Sep 20, 2021
@hutch120
Copy link

hutch120 commented Nov 22, 2021

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

@pgrzesik
Copy link

@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?

@hutch120
Copy link

@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.

@pgrzesik
Copy link

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.

@hutch120
Copy link

hutch120 commented Nov 22, 2021

@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

@pgrzesik
Copy link

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 👍

@Schachte
Copy link

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.

@nikhuber
Copy link

nikhuber commented Mar 5, 2022

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.

roc referenced this issue in nhsx/standards-registry Apr 25, 2022
The struggle with the serverless config living
outside the root of the folder are detailed [here](https://github.com/serverless/github-action/issues/53\#issuecomment-1059839383)
The github action _should_ respect sub folders, though this should force
it run from the correct folder
roc referenced this issue in nhsx/standards-registry May 17, 2022
The struggle with the serverless config living
outside the root of the folder are detailed [here](https://github.com/serverless/github-action/issues/53\#issuecomment-1059839383)
The github action _should_ respect sub folders, though this should force
it run from the correct folder
@DavideViolante
Copy link
Contributor

Thanks, I added this info in the readme.

@amirhmk
Copy link

amirhmk commented May 29, 2023

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests