How to break a single command inside a `script` step on multiple lines

余生长醉 提交于 2020-01-16 14:10:02

问题


We have a project using Azure Pipeline, relying on azure-pipelines.yml file at the repo's root.

When implementing a script step, it is possible to execute successive commands in the same step simply writing them on different lines:

- script: |
  ls -la
  pwd
  echo $VALUE

Yet, if we have a single command that is very long, we would like to be able to break it on several lines in the YAML file, but cannot find the corresponding syntax?


回答1:


You can use '^' to break your command line into multiple lines. Check below exmaple. Below script will output 'hello world' like a single line command echo 'hello world'

- script: |
    echo ^
    'hello ^
    world'



回答2:


You didn't specify your agent OS so I tested on both windows-latest and ubuntu-latest. Note that the script task runs a bit differently on these 2 environments. On Windows, it uses cmd.exe. On Ubuntu, it uses bash. Therefore, you have to use the correct syntax.

On Windows:

pool:
  vmImage: 'windows-latest'

steps:
- script: |
    mkdir ^
    test ^
    -p ^
    -v

On Ubuntu:

pool:
  vmImage: 'ubuntu-latest'

steps:
- script: |
    mkdir \
    test \
    -p \
    -v

Those two files above work on my Azure DevOps.




回答3:


At the moment, the only way we found for to break a single command on multiple line is using YAML folded style:

- script: >
    echo
    'hello world'

It is all about replacing | with >.

Notes:

  • It is not possible to introduce extra indentation on the following lines! For example, trying to align all arguments given to a command would break the behaviour.
  • This style will replace newlines in the provided value with a simple white space. This means the script now can only contain a single command (maybe adding literal \n at the end of the line would actually introduce a linebreak in the string, but it feels backward compared to the usual approach of automatice linebreak unless an explicit continuation is added).


来源:https://stackoverflow.com/questions/59198459/how-to-break-a-single-command-inside-a-script-step-on-multiple-lines

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!