Gitlab CI/CD variable substitution in my appSettings.json

Viewed 3697

I'm using Gitlab CI/CD to deploy my .netcore application to AWS Beanstalk. How can I update the appSettings.json values in my .netcore application when deploying to different environments using variables defined in my CI/CD pipeline settings?

Azure DevOps has a JSON variable substitution feature which I really liked.

GitHub Actions can also hook into this feature

How can I achieve this with Gitlab CI/CD?

I want to use this method of deployment because

  1. I won't have to store sensitive production config values in my repository. Values will be updated by Masked variables setup in the CI/CD Pipeline.
  2. I don't have to rebuild my artefacts every time I deploy to a new environment.

If this can't be done in Gitlabs, whats the recommended best practice?

thanks

2 Answers

I do something similar here with gitlab and the solution was to build a shell script that replaces some string from variable values before starting the deploy job

something like this

script:
  - sed -i 's/STRING_TO_REPLACE/$GITLAB_VARIABLE/g' file.json

Just pay attention to escape json quotes correctly to make this possible

I use the same workaround. I filed an issue about this months ago: https://gitlab.com/gitlab-org/gitlab-foss/-/issues/78486

and I recently talked about this at the gitlab forum:

https://forum.gitlab.com/t/use-variables-per-branch-in-gitlab/43685

unfortunately there does not seem to be a nice/clean solution at the moment.

so I use the workarround: 1: use environment scopes for variables so the same variable can have different values (for test/prod environments) 2: define the environments and branches in the jobs in gitlab-ci.yml 3: create a bunch of sed one liners to do a search/replace..

call it ugly, call it low level but I searched for a nice/clean/gitlab native solution an did not find it.

Related