How do I create a JSON file containing secret variables in a .gitlab-ci.yml?

Hey, I’m trying to figure out how to set up GitLab CI, and could really use some help. I’d like to deploy via FTP using a gulp task and secret variables, but I’m having a really hard time getting it to work correctly. I can’t figure out how to properly output the config.json I’m trying to generate.

Every time I figure out a different way to escape the stuff properly, it makes the variable stop working (i.e. outputting as $ftp_host instead of the stored value in Gitlab). I think it’s because of the quotes around the variable, but I can’t figure out how to escape them and print the variable while still printing the quotes in the final config.json.

Once output, the config.json should look like this:

{"host": "example.com"}

Here’s current my .gitlab-ci.yml

image: node:latest

cache:
  paths:
   - node_modules/

before_script:
  # - npm install -g -q gulp
  # - npm install -q
  - echo '{"host"":" "$ftp_host"}' > config.json
file-check:
  only:
   - master
  script:
   # - gulp
   # - gulp ftp
   - cat config.json

That outputs to:

{"host": "$ftp_host"}

I think what I’m looking for is a way to tell GitLab CI that $ftp_host is a variable even when it’s in quotes. In SCSS, that would be something like "#{$ftp_host}", but I doubt that will work (will give it a shot right now though)

I figured it out! I think I was just overthinking it.

- echo "{\"host\":\"$ftp_host\"}" > config.json

1 Like