Ehvince
Ehvince

Reputation: 18395

How to create configuration files that contain colons in YAML?

According to CI's lint, this yml is not valid:

pages:
  stage: deploy
  image: python:3.5
  script:
  - echo "foo: $VAR" > site.yml
  - cat ~/.python-gitlab.cfg

  artifacts:
    paths:
      - _build
  only:
    - master

error:

jobs:pages:script config should be a string or an array of strings

If I remove the colon on the echo line, it works.

What I want to do is to create some configuration files on the fly, to comply to existing tools, using private variables, like echo "url: $CI_PROJECT_URL" > site.yml to produce

url: "https://gitlab.com/group/project"

But I can't do this because the yaml is said invalid, and I don't find workarounds. Or I must write code around my tools to pass command line arguments instead of reading config files. Still, this colon stuff seems a bug.

Upvotes: 19

Views: 5622

Answers (2)

Daniel Rich
Daniel Rich

Reputation: 159

Single quotes around the whole line, as depicted by @Jawad, works:

- 'echo "foo: $VAR" > site.yml'

But if your command contains other single quotes, I find it easiest to use the pipe Block Scaler Style: |

- |
  echo "I want to echo the key"
  echo 'foo: $VAR' > site.yml

More documentation on Block Scaler Styles can be found here: http://www.yaml.org/spec/1.2/spec.html#id2760844

Upvotes: 8

Jawad
Jawad

Reputation: 4675

It should work if you wrap the whole line within quotes like this:

- 'echo "foo: $VAR" > site.yml'

Gitlab's CI lint marks it as correct syntax.

See here for more info.

Upvotes: 18

Related Questions