Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CircleCI run multi line command

Excerpt from a CircleCI config file:

deploy:
  machine:
    enabled: true
  steps:
    - run:
        name: AWS EC2 deploy
        command: |
          ssh -o "StrictHostKeyChecking no" [email protected] "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 [email protected]
          cd ~/circleci-aws
          git pull
          npm i
          npm run build
          pm2 restart build/server
like image 552
ChrisRich Avatar asked Aug 03 '18 11:08

ChrisRich


Video Answer


2 Answers

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

like image 81
mlsamuelson Avatar answered Sep 18 '22 14:09

mlsamuelson


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

ssh -o StrictHostKeyChecking=no [email protected] bash -c '
      cd ~/circleci-aws
      git pull
      npm i
      npm run build
      pm2 restart build/server'
like image 35
dnephin Avatar answered Sep 16 '22 14:09

dnephin