I am using an Azure DevOps pipeline to execute unit tests and deploy my Blazor Server application to an agent. I use the NUnit test framework and PostgreSQL as database. For the unit tests, I use a separate test database with a ConnectionString stored in my test project's appsettings.json. But the appsettings.json is not checked into Git, because its content is or should be different, depending on where the application is running. Therefore, the appsettings.json is missing on the agent.
I tried putting the appsettings.json with the desired content into the C:\agent\_work\...\...\TestProject folder, where the test project is built, by hand. I expected the appsettings.json to remain in this folder and be picked up by my test project. But this folder is deleted by the pipeline before the project is rebuilt.
What I tried next, was to put the appsettings.json to a location outside the pipeline build folder and to give the test project the absolute path to the appsettings.json. This works so far. But I'm not quite happy with it, as it means I'd have to have my appsettings.json at the exact same path on my local PC. And I think the appsettings.json belongs somewhere near the actual project.
So my goal would be to have different appsettings.json for my agent and my local PC. I also don't want to define the agent appsettings.json in the pipeline, because it doesn't seem like a clean solution to this problem, as these settings really only concern the agent.
Is it possible to achieve this?
Update:
I improved my temporary solution with the appsettings.json outside the project folder by having my test project search at both the project folder and the outside absolute path for a appsettings.json. But I still find it suboptimal.
Based on your description, I understand you need to use different
appsettings.jsonfiles with different ConnectionStrings stored in them for your test project when running pipeline agent jobs; and due to the same reason, theappsettings.jsonis not pushed to Git repo.As the benefit of Git version control, would you like to create different branches containing different
appsettings.jsonfiles in them? You may then select different sources from different branches when running your pipeline without worrying about the contents inappsettngs.jsonOn the other hand, instead of changing/copying
appsettings.jsonfile into your pipeline working directory manually, you may also consider using the Copy File task as a workaround to copy theappsettings.jsonfiles automatically. If you use this task to copy files from a shared path on your local agent machine, please make sure the local agent service/process has sufficient permission to access that path. If you would prefer theappsetting.jsonto be checked out from your repo, kindly note that you need to push them into different paths of your repo first and then you can use this task as well to copy the expectedappsettings.jsonfile into the path of$(Build.SourcesDirectory)\**\TestProject.