Hi all,
I have Gitlab CE onpremise.
A dotnet core project has an (real)appsettings.json file store database connectionstrings.
I want to replace it with an (fake)appsettings.json like this:
{
"ConnectionStrings": {
"SQLConnection": "Server=$MSSQLSERVER; Database=$MSSQLDBNAME; User ID=$MSSSQLUSER; Password=$MSSQLPASS"
}
}
Then pass environment variables from Gitlab cd/cd variables to container later.
I’m new to Gitlab ci/cd and I’m not a programmer so I think of 2 solutions :
Solution 1: Replace (fake)appsettings.json with (real)appsettings.json when building image
- Create a (type) file key=appsettings_json variable in gitlab ci/cd variables , with value is content of (real)appsettings.json
- Pass (type) file key=appsettings_json to Dockerfile when building image
.gitlab-ci.yml
build_image:
stage: build
image: docker:20.10.9
services:
- docker:20.10.9-dind
variables:
DOCKER_TLS_CERTDIR: "/certs"
before_script:
- docker login -u $REGISTRY_USER -p $REGISTRY_PASS
script:
- docker build --build-arg appsettings_json=$appsettings_json -f Dockerfile -t name:tag .
- Create (real)appsettings.json file inside image
Dockerfile
FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build
WORKDIR /app
COPY ./ ./
RUN echo $appsettings_json
RUN echo $appsettings_json > ./appsettings.json
RUN cat ./appsettings.json
But it doesn’t work , the $appsettings_json and appsettings.json are empty
Solution 2 :
Pass $MSSQLSERVER , $MSSQLDBNAME , $MSSSQLUSER ,$MSSQLPASS to container when running docker compose
- Create gitlab ci/cd variables corresponding to $MSSQLSERVER , $MSSQLDBNAME , $MSSSQLUSER ,$MSSQLPASS
- echo variables to .env file
deploy:
stage: deploy
before_script:
- chmod 400 $SSH_KEY
script:
- ssh to deploy server "
docker login ... && do something to pull source code including docker-compose.yml , .env &&
echo "MSSQLPASS=$MSSQLPASS" >> .env && echo others $ && docker-compose down &&
docker-compose up -d"
But it doesn’t work also, app complains that it cannot connect to sql server althought I can verify that information is correct and container has environment variables imported (I checked with export command)
Please give me some advice thank you very much.