Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

yaml

gitlab-ci

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?

like image 757
AbreQueVoy Avatar asked Jun 22 '21 14:06

AbreQueVoy


People also ask

How do you use a colon in GitLab CI?

You do not have to formally escape colons. Instead put your command's argument with colons wrapped in single quotes in a variable. You can use other variables inside your argument string as per the example below. Be very precise with all the quotes, dashes and spaces along the script.

How do I skip CI GitLab?

Push options for GitLab CI/CD You can use push options to skip a CI/CD pipeline, or pass CI/CD variables. Do not create a CI pipeline for the latest push. Only skips branch pipelines and not merge request pipelines. Provide CI/CD variables to be used in a CI pipeline, if one is created due to the push.

How do I stop a running pipeline in GitLab?

To enable or disable GitLab CI/CD Pipelines in your project: Navigate to Settings > General > Visibility, project features, permissions. Expand the Repository section. Enable or disable the Pipelines toggle as required.


2 Answers

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 abit:

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" 
like image 102
pat Avatar answered Sep 24 '22 11:09

pat


You do not have to formally escape colons. Instead put your command's argument with colons wrapped in single quotes in a variable. You can use other variables inside your argument string as per the example below.

Then eval that "stringified' command with the variables like the following (the example is for curl to trigger external deployment, but should work for every command):

deploy_job:
  variables:
    DEPLOY_CURL_COMMAND: 'curl -X POST -H "Content-Type: application/json" -H "Authorization: Bearer $DO_APP_PLATFORM_API_TOKEN" https://api.digitalocean.com/v2/apps/$DO_APP_PLATFORM_STAGE_FRONT_APP_ID/deployments'
  script:
    - echo "Stage Deploy to DigitalOcean App Platform"
    - echo $DEPLOY_CURL_COMMAND
    - 'eval "$DEPLOY_CURL_COMMAND"'

Be very precise with all the quotes, dashes and spaces along the script. YAML is quite unforgiving. Check the syntax with GitLab pipeline linter.

If you use the project / repository level variables do not forget to uncheck protected checkbox (checked by default) when setting a variable and using it in unprotected branch (which is a branch default state).

like image 39
Valentine Shi Avatar answered Sep 21 '22 11:09

Valentine Shi