How to Automatically deploy Configuration Files with Azure DevOps CD pipelines

Introduction


Part of the CI/CD Automation is to add environment-related configuration files through deployment automatically, rather than manually uploading them.
This will provide less human error and more security by splitting the developer's responsibility of knowing the environment configuration or editing it, as to be handled by Operation side.

We will take an example of CI for DotNet Core 6 WebApi project, and on that project, we need to copy the related appsetting.json to its environment.

Pre-Requested Knowledge:

To benefit from this article you need to be familiar with the following: 
  • Azure DevOps.
  • the Concept of CI/CD.
  • Azure CI/CD Pipelines.
  • DotNet Core Web Api Deployment (or similar).

Adding From Source Code Branch


In some cases, we will have configuration files included in our source code, for example, in DotNet Core Web Application code, we can have multiple appsettings.json files for each environment.

 On deployment, we need to check: if the environment is a dev environment, then upload the appsettings.Development.json file.
 Else if it's a staging environment then upload appsettings.Stage.json on deployment and so on.

Therefore, In this situation we follow the following steps:

On Build Pipeline: 

  • On the build pipeline, Archiving the published files or Artifacts must be disabled.

Notice that we set zipAfterPublish: false.

On Deployment Pipeline:

  • On the deployment pipeline, Add a delete file task to remove unwanted configuration files:
  • Then, we need to rename the appsettings.Development.json file to appsettings.json to be the default app settings. to do so, we will use the Command Line Script task:
  • Then we will archive the published files (if required to be archived) by adding the archive task:
Then we deploy, and the configuration file need will be attached to the deployment package. 

This way is simple, But, it cost the developer to upload the configuration files on the source code, which is not always a best practice for two reasons: 
  • You may have environment-related sensitive data in your configuration files, and we don't want to be exposed to anyone that can access the source code.
  • Any change in configuration files will cost the developers to make commits to the source code, which means a process of Pull Request and review for any environmental change.
Next, we will see how to solve this issue at Azure DevOps.

Adding From Azure DevOps Secure Files

Secure files allow you to store files you can share across pipelines. Use the "Secure files" library to store files such as:
  • Signing certificates
  • Apple Provisioning Profiles
  • Android Keystore files
  • SSH keys
These files can be stored on the server without committing them to your repository.
See more on how to add azure secure files here.

To add files on deployment from Secure Files, follow the steps:

Build Pipeline:

  • On the build pipeline, Archiving the published files or Artifacts must be disabled.

Deployment Pipeline:

  • Add Download secure file task to your deployment pipeline:
  • Then, Copy the secure file to the published folder:
  • Finally, Archive the published folder if required:

Conclusion

In this article, we show how to add configuration files while automated deployment, we also introduce how to use Azure DevOps Secure Files to make a Zero Trust Deployment.


Comments

Popular posts from this blog

Adding Multiple DB Contexts in your DotNet Project

Adapting a New Technology: A General Guide to keep Your Systems up-to-date