CircleCI run multi line command
Asked Answered
W

4

13

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
Wicklund answered 3/8, 2018 at 11:55 Comment(1)
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.Petro
S
3

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'
Sensuous answered 4/8, 2018 at 19:45 Comment(2)
This doesn't seem to work anymore for me, circle-ci errors with bash: -c: option requires an argumentCacomistle
@Cacomistle An escape character is needed: ssh -o StrictHostKeyChecking=no [email protected] bash -c ' \Vernettaverneuil
S
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".

Seitz answered 6/5, 2020 at 14:40 Comment(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 circleci.com/docs/2.0/configuration-reference/#exampleHufnagel
Here's an example: gist.github.com/adnauseum/…Halliday
S
3

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'
Sensuous answered 4/8, 2018 at 19:45 Comment(2)
This doesn't seem to work anymore for me, circle-ci errors with bash: -c: option requires an argumentCacomistle
@Cacomistle An escape character is needed: ssh -o StrictHostKeyChecking=no [email protected] bash -c ' \Vernettaverneuil
H
1

From here:

     steps:
       - run: |
           s3cmd --access_key ${<< parameters.access-key >>} \\
                 --secret_key ${<< parameters.secret-key >>} \\
                 << parameters.command >>
Heptahedron answered 13/9, 2022 at 20:22 Comment(0)
P
1

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 [email protected] bash -c ' \
            ls
            mkdir today-folder
            touch shadb123.txt'
Petro answered 30/1, 2023 at 18:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.