Travis deploy script multiline command

Viewed 2713

I have following deploy script in Travis file and facing problem with multiline command. I've tried lots of combination and couldn't find a proper solution. If I extract it to the file and call it from script attribute, it works properly but our aim is to put all the commands in the script attribute as multiline command.

deploy:
  - provider: script
    skip_cleanup: true
    script: mvn deploy -B -DskipTests -Dbuild.number="$TRAVIS_BUILD_NUMBER"
    on:
      tags: false
      branch: dev

  - provider: script
    skip_cleanup: true
    script:
      pip install something &&
      another-command &&
      yet-another-command &&
      docker push "image-name"
    on:
      tags: false
      all_branches: true

Here is the error logs:

sh: 1: Syntax error: "&&" unexpected

If someone knows how to define multiline commands, I will be appreciated. Thanks in advance.

2 Answers

TLDR

The example you gave in description looks legit from a YAML perspective. Please make sure you don't have empty lines or comments which prevents the content in script section from becoming one string

Longer version

The script provider in deploy requires the script content being single string. Only one line is allowed so you can't use YAML array.

This is different from the top level script section. Top level script section allows you to use YAML array which means you can supply multiple single line strings leading with -

Having that said, any YAML single line (without line ending) can be used properly in deploy's script section. Here are some examples

script: pip install something &&
      another-command &&
      yet-another-command &&
      docker push "image-name"
# >- to removing the ending new line
script: >-
      pip install something &&
      another-command &&
      yet-another-command &&
      docker push "image-name"

The example below will not work for deploy's script section, but will work for top level script section

# Only works in top level script not deploy script
script:
    - pip install something && another-command
    - yet-another-command
    - docker push "image-name"

I would try one of these options:

Option 1: Create your deploy.sh script, and call it from .travis.yml. I like this option, since it will make life easier - since you can test and run it separately, and avoid this exact issue that you are facing.

Option 2: If you insist on having the commands only in .travis.yml - you can try one of these syntax changes:

a: Try either | or > in the script: definition (to denote multiline text):

script: >
  pip install something &&
  another-command &&
  yet-another-command &&
  docker push "image-name"

b: Try providing the script as an array (assuming Travis supports this syntax here as well - as it is supported in many of their other options):

script: 
  - pip install something
  - another-command
  - yet-another-command
  - docker push "image-name"
Related