13

Excerpt from a CircleCI config file:

deploy:
  machine:
    enabled: true
  steps:
    - run:
        name: AWS EC2 deploy
        command: |
          ssh -o "StrictHostKeyChecking no" ubuntu@xxx.xxx.xxx.xxx "cd ~/circleci-aws; git pull; npm i; npm run build; pm2 restart build/server

How can I break the command into multiple lines? Tried below syntax, but it only runs the first command:

deploy:
  machine:
    enabled: true
  steps:
    - run:
        name: Deploy
        command: |
          ssh -o StrictHostKeyChecking=no ubuntu@xxx.xxx.xxx.xxx
          cd ~/circleci-aws
          git pull
          npm i
          npm run build
          pm2 restart build/server
ChrisRich
  • 8,300
  • 11
  • 48
  • 67
  • Hi I have been trying to do the same and was able to figure it out but regarding the `pm2 restart build/server` command. How is it possible that pm2 is available? I have explored the internet and I mostly found out that CirclecCI won't have pm2 access of the machine. Can you please help here in understanding it. – Shadab Jan 30 '23 at 19:14

4 Answers4

23

This is an old one, but it's had a lot of views so what I've found seems worth sharing.

In the CircleCI docs (https://circleci.com/docs/2.0/configuration-reference/#shorthand-syntax) they indicate that in using the run shorthand syntax you can also do multi-line.

That would look like the following

- run: |
    git add --all
    git commit -am "a commit message"
    git push

The difference between the question's example and this is the commands are under "run", not "command".

mlsamuelson
  • 351
  • 2
  • 5
  • 2
    To avoid any confusion: The shorthand syntax can be used for `command:` as well as `run:`. An example of this can be found here https://circleci.com/docs/2.0/configuration-reference/#example – jakxnz May 15 '21 at 02:54
  • Here's an example: https://gist.github.com/adnauseum/5953bb5b7b6455cc0790088c672eb9fa#file-config-yml-L12-L14 – adnauseam Jan 12 '23 at 11:06
3

You'll need to pass those other commands as args to a shell (like bash):

ssh -o StrictHostKeyChecking=no ubuntu@xxx.xxx.xxx.xxx bash -c '
      cd ~/circleci-aws
      git pull
      npm i
      npm run build
      pm2 restart build/server'
dnephin
  • 25,944
  • 9
  • 55
  • 45
1

From here:

     steps:
       - run: |
           s3cmd --access_key ${<< parameters.access-key >>} \\
                 --secret_key ${<< parameters.secret-key >>} \\
                 << parameters.command >>
Palisand
  • 1,302
  • 1
  • 14
  • 34
0

After going through all of the answers on this page and looking at the comments I was able to resolve it. And wanted to compile the final working syntax below.

NOTE: Don't miss the ' & \after -c and as well after the last command put an '

      - run:
          name: SSH to the server and deploy
          command: |
            ssh -o StrictHostKeyChecking=no ubuntu@xxx.xxx.xxx.xxx bash -c ' \
            ls
            mkdir today-folder
            touch shadb123.txt'
Shadab
  • 321
  • 5
  • 15