How to add gitlab user defined variables in json file while deploying aws?

Viewed 1906

I am deploying a aws stack using gitlab -- using the below code in .gitlab-ci.yml file.

      script:
      - | 
     aws cloudformation create-change-set \
    --change-set-type UPDATE \
    --template-body file://./abc.yaml \
    --change-set-name abcdef \
    --stack-name $STACKNAME \
    --parameters file://./params.json \
    --capabilities CAPABILITY_IAM

I want to use gitlab user defined variables in params.json file

Below is my params.json file.

 [
  {
      "ParameterKey": "someparameter",
      "ParameterValue": "$ABC",

  },
  {
      "ParameterKey": "otherparameter",
      "ParameterValue": "$DEF"
  }]

I want to add gitlab user defined variables in place of $DEF and $ABC. What is the correct way of doing so?

2 Answers

Never tried, but think the following is possible: You can write your own shell script to replace the value within the params.json file, then run the script within your gitlab CI/CD before running the aws command.

There is a lot of ways to do it but, the most important thing that you need to know is to escape json quotes (with backslash) correctly on gitlab

"{\"host\":\"$VARIABLE\"}"

You can use echo to create a new file or to send the json to a variable... you can use sed if you want to edit a template file.

echo "{\"host\":\"$VAR\"}" > config.json or JSONFILE=$(echo "{\"host\":\"$VAR\"}")

Related