Search code examples
yamlgitlab-ci

How to escape a colon in .gitlab-ci.yml?


I have a line like this:

sed -i 's/"host: TND_HOST"/"host: process.env.TND_HOST"/g' services/management/tnd.js

and the option above causes linting error:

This GitLab CI configuration is invalid: (<unknown>): mapping values are not allowed in this context at line [...]

Other options that do not work are:

sed -i 's/host: TND_HOST/host: process.env.TND_HOST/g' services/management/tnd.js

sed -i "s/host: TND_HOST/host: process.env.TND_HOST/g" services/management/tnd.js

Any way to overcome the issue and keep it as a one-liner?


Solution

  • Since you are using both types of quotes it is probably easiest to put your command in a yaml template. That way you don't need to escape anything:

    stages:
      - lint
    
    .sed_template: &sed_template |
     sed -i 's/"host: TND_HOST"/"host: process.env.TND_HOST"/g' services/management/tnd.js
    
    some_job:
      image: someImage:latest
      stage: lint
      except:
        - master
      cache:
        key: ${CI_COMMIT_REF_SLUG}
        paths:
          - frontend/node_modules/
      script:
        - echo "firstLine"
        - *sed_template
        - echo "lastLine"
    

    It's not quite a one liner anymore but I guess its the cleanest option as it keeps the command itself rather readable. Another option would be using folding style which shrinks it down a bit:

    stages:
      - lint
    
    some_job:
      image: someImage:latest
      stage: lint
      except:
        - master
      cache:
        key: ${CI_COMMIT_REF_SLUG}
        paths:
          - frontend/node_modules/
      script:
        - echo "firstLine"
        - >
            sed -i 's/"host: TND_HOST"/"host: process.env.TND_HOST"/g' services/management/tnd.js
        - echo "lastLine"