2

I'm working on C# project using Gitlab CI and would like to hide secrets in app.config file.

I added one variable in Gitlab and in app.config like this:

<add key="User" value="username" />
<add key="Password" value=$Password />

Then, when i try to build application in gitlab CI using msbuild, i receive an error:

$ msbuild "$PROJECTNAME.sln" /consoleloggerparameters:ErrorsOnly /t:Test_project /maxcpucount /nologo /property:Configuration=Release /verbosity:quiet app.config(5,33): error MSB3249: Application Configuration file "app.config" is invalid. '$' is an unexpected token. The expected token is '"' or '''. Line 7, position 33. [/builds/project/Test_project/Test_project.Service/Test_project.Service.csproj]

Please advice, how to add Gitlab environment variable in app.config xml file.

9
  • There should be quotes around the attribute value, which is what the error message is telling you - expected token is " or '. Commented Feb 18, 2020 at 14:58
  • @CharlesMager, if add a quotes around the attribute value it will be just text, not the value of variable: <add key="Password" value="$Password" /> cat ./Test_project.Service/bin/Release/Test_project.Service.exe.config ... <add key="Password" value="$Password" /> ... echo $Password password Commented Feb 18, 2020 at 15:08
  • Ah, ok - I've slightly missed the point here! I'm not sure I know enough about how Gitlab does this to comment, though the end result would still need quotes around it. Given it complains about the $ it would seem it's not being replaced at all. Commented Feb 18, 2020 at 15:15
  • Did you try using value="%Password%" ? Commented Feb 18, 2020 at 19:44
  • @NicolasPepinster, yes, result is a text, like in previous comment: <add key="Password" value="%Password%" /> Commented Feb 19, 2020 at 8:02

1 Answer 1

1

For .xml files it should work using ${env.MY_VAR_DEFINED_IN_GITLAB}

Where MY_VAR_DEFINED_IN_GITLAB, as specified, is the variable defined in Gitlab Project (or group) -> Settings -> CI/CD -> Variables

Therefore in your case should work:

<add key="User" value=${env.GITLAB_VAR_USERNAME}/>
<add key="Password" value=${env.GITLAB_VAR_PASSWORD}/>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.