CircleCI运行多行命令

13

来自CircleCI配置文件的摘录:

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

我该如何将命令分成多行?我尝试了以下语法,但它只运行第一条命令:

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

嗨, 我一直在尝试做同样的事情,并成功解决了问题,但是关于 pm2 restart build/server 命令,我有疑问。pm2 怎么可能可用?我已经搜索了互联网,大多数情况下发现 CirclecCI 没有机器上的 pm2 访问权限。你能帮忙理解吗? - Shadab
4个回答

23

这是一个旧的内容,但是它已经有很多浏览量,所以我发现这个信息值得分享。

在CircleCI文档(https://circleci.com/docs/2.0/configuration-reference/#shorthand-syntax)中指出,在使用run快捷语法时,您还可以进行多行操作。

如下所示:

- run: |
    git add --all
    git commit -am "a commit message"
    git push
这个问题的例子和这个不同之处在于命令是在"run"下而不是"command"下。

2
为避免任何混淆:缩写语法可用于 command:run:。此处可找到示例 https://circleci.com/docs/2.0/configuration-reference/#example - jakxnz
这是一个例子:https://gist.github.com/adnauseum/5953bb5b7b6455cc0790088c672eb9fa#file-config-yml-L12-L14 - adnauseam

3

你需要将这些其他的命令作为参数传递给一个shell(比如 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'

这个对我来说似乎不再起作用了,circle-ci 报错 bash: -c: option requires an argument - tutuDajuju
1
@tutuDajuju 需要一个转义字符:ssh -o StrictHostKeyChecking=no ubuntu@xxx.xxx.xxx.xxx bash -c ' \ - Chad Johnson

1

来自这里

     steps:
       - run: |
           s3cmd --access_key ${<< parameters.access-key >>} \\
                 --secret_key ${<< parameters.secret-key >>} \\
                 << parameters.command >>

0

浏览了本页面上的所有答案并查看了评论后,我成功解决了问题。以下是最终工作语法的编译。

注意:不要错过'\-c之后以及在最后一个命令之后放置一个'

      - 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'

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接