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.
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.
Therefore, In this situation we follow the following steps:
Notice that we set zipAfterPublish: false.
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:
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- task: DotNetCoreCLI@2 | |
displayName: 'Publish Web App' | |
inputs: | |
command: publish | |
publishWebProjects: True | |
arguments: '--configuration $(BuildConfiguration) --output $(Build.ArtifactStagingDirectory)' | |
zipAfterPublish: false |
On Deployment Pipeline:
- On the deployment pipeline, Add a delete file task to remove unwanted configuration files:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
steps: | |
- task: DeleteFiles@1 | |
displayName: 'Delete appsetting.json files from WebApi Folder' | |
inputs: | |
SourceFolder: '$(System.DefaultWorkingDirectory)/WebApi' | |
Contents: appsettings.Staging.json |
- 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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
steps: | |
- script: 'rename appsettings.sit.json appsettings.json' | |
workingDirectory: '$(System.DefaultWorkingDirectory)/WebApi' | |
displayName: 'Command Line Script' |
- Then we will archive the published files (if required to be archived) by adding the archive task:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
steps: | |
- task: ArchiveFiles@2 | |
displayName: 'Archive WebApi' | |
inputs: | |
rootFolderOrFile: '$(System.DefaultWorkingDirectory)/WebApi/**' | |
archiveFile: '$(System.DefaultWorkingDirectory)/WebApi.zip' |
Then we deploy, and the configuration file need will be attached to the deployment package.
- 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
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- task: DotNetCoreCLI@2 | |
displayName: 'Publish Web App' | |
inputs: | |
command: publish | |
publishWebProjects: True | |
arguments: '--configuration $(BuildConfiguration) --output $(Build.ArtifactStagingDirectory)' | |
zipAfterPublish: false |
Deployment Pipeline:
- Add Download secure file task to your deployment pipeline:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
steps: | |
- task: DownloadSecureFile@1 | |
displayName: 'Download appsettings' | |
inputs: | |
secureFile: appsettings.json |
- Then, Copy the secure file to the published folder:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- task: CopyFiles@2 | |
displayName: 'Copy AppSettings Files to: API Folder' | |
inputs: | |
SourceFolder: '$(Agent.TempDirectory)' | |
Contents: appsettings.json | |
TargetFolder: '$(System.DefaultWorkingDirectory)/API' |
- Finally, Archive the published folder if required:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
steps: | |
- task: ArchiveFiles@2 | |
displayName: 'Archive WebApi' | |
inputs: | |
rootFolderOrFile: '$(System.DefaultWorkingDirectory)/WebApi/**' | |
archiveFile: '$(System.DefaultWorkingDirectory)/WebApi.zip' |
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
Post a Comment