Before I was shown Octopus Deploy I thought that environment dependent appSettings should be a part of Build Configuration in project properties.
Now in times of software as a service it is a deployment process that buckles everything up.
I want my environment configuration in release process to just open web.config and substitute appsettings and connection strings based on variable names i defined for the release definition.
How can I do it? The closest I could get was Magic chunks. The problem with it is that I have to give it a json with the mapping and I have to define it for each environment separately, so it makes no use of environment variables, really, or at least you have to define it in "enviroment variables" section and then, again, in each process of environment.
You can use the "Tokenizer" task in "Release Management Utility tasks" extension or "Replace Tokens" task.
These tasks can replace the strings in a file with the custom variables in definition.
Related
I want to develop a pipeline for a .net web app using Azure tfs. I used the IIS Web Deploy template/utility already provided for such deployments. In the release pipeline, I am using XML variable transformation. But one issue I am facing is, that in the project, I have multiple .config files and I have included them in my web.config file as follows
<configuration>
<connectionStrings configSource="None"/>
<appSettings file="None">
</appSettings>
Based on the release for different environments, I perform variable substitution during the release. For example, a folder app_data contains app_sttings_*.config files and file variable does get replaced. But I am having an issue with the connection String file, I can not get configSource to change during release.
Am I doing this wrong or missing a step?
Since the configSource attribute cannot be replaced by the IIS Web App Deploy task. You can use the extension tool Magic Chunks to replace the configSource value.
You can install Magic Chunks extension in your azure devops organization. And add Config transformation task before IIS Web App Deploy task to replace the configSource value.
First you need to define a variable (eg. configSource) to hold configSource value in the Release Pipeline variables section. Then configure the Transformations Section of Config transformation task as below
{
"configuration/connectionStrings/#configSource": "$(configSource)"
}
There is another Replace Tokens task that can replace the configSource value. You can check it out here.
It said in the IIS Web App Deploy task that Variables defined in the Release Pipeline will be matched against the 'key' or 'name' entries in the appSettings, applicationSettings, and connectionStrings sections of the config files. Maybe that is the reason that configSource cannot be replaced by the IIS Web App Deploy task.
See File transforms and variable substitution reference for more information.
Azure DevOps XDT Transform tasks allow you to build release profiles that transform the base config file with settings that are specific to each environment, such as a connection string that points to different db servers for different environments. The app.dev.config file has transformations for the dev environment, app.qa.config for qa, etc, which are applied during the deployment to the base app.config file.
I need to take this one step further and deploy custom config files for each individual server in a load balanced environment. For example, the DEV environment has two servers dev1.mysite.com and dev2.mysite.com that are load balanced by dev.mysite.com. Each of the two servers needs specific settings in the config file deployed to that server.
I don't (yet) see a way in Azure DevOps to do this. Part of the solution might be to set up variables with the setting that needs to be applied to each environment/server but I haven't figured out how to apply the correct variable to each config.
You can use task Magic Chunks to apply the variable to each config.
You can search for Magic Chunks task in your pipeline and install it to your organization. Then add Config transform task before the deployment task to update the config file with specific setting. For below example settings of magic chunk task:
As above screenshot shows, You can reference your pipeline variables in the tasks.
There are other extension tasks like RegEx Find & Replace you can use to replace the variables in the config files.
I'm using an Azure DevOps Pipeline to release an ASP.NET MVC system to an Azure Web App.
I've configured the build not to apply the XML transforms, so I can apply them later, during the release step, and the same build artefact can be released to multiple environments.
The build works fine, and I end up with an artefact containing an untransformed web.config, and the environment-specific transform files.
The Azure App Service Deploy task has a check-box called XML Transformation, which displays the following help text:
The config transforms will be run for *.Release.config and
*.<EnvironmentName>.config on the *.config file. Config transforms will be run prior to the Variable Substitution. XML transformations
are supported only for Windows platform.
At the moment I'm trying to set up a release into a test environment, but the Web.Release.config is being applied, rather than the Web.Test.config. I've searched everywhere I can find for a place to define the environment to make the release use the test config, but I can't find anywhere.
There's a similar question on GitHub which shows the following screenshot:
Unfortunately mine doesn't look like that:
Am I trying to do the right thing? If so, where do I set the environment?
You need to make sure your stage name is just 'Test' and not 'Deploy EMS to Test', reference here.
Essentially, I need a way to inject environment variables for my app since I don't want to check a .env file into my repo. I was hoping to run something like API_URL=api.example.com npm run build, but it looks like you can't prepend things before the task command.
Is there a way to do this or is there a better way to create environment variables for a node app hosted in Azure?
You can add some build variables to the build definition and then reference those in your build steps somewhere. For example, for your API_URL add a build variable with the same name and value. If you need the variable to be secret for any reason (passwords, etc.) just click the lock icon next to the value field.
Then add a new cmd task to your build and move it to the top to set your environment variables before you start your build. The way you reference the build variables is like this...
set API_URL=$(Build.API_URL)
In the UI it will look like this:
I added two cmd tasks to a test build just to show that it is working. The first one is used to set the environment variable and then I used the second to dump all the environment variables so I could see them. You can see in the build logs that it worked.
If you want to reference the build variables using something other than the command line you can find examples of the different ways to reference build variables here. There are also examples of how to use secret variables on that page.
EDIT:
Since you have some concerns about it not being available in your node app I tested it out in the console real quick to show that it will definitely work.
Using yaml config you can switch to script task and then do something like this:
- script: 'npm run'
workingDirectory: 'src'
displayName: 'npm run'
env:
{ API_URL: 'api.example.com' }
tehbeardedone's answer is great for the build machine, and the answer is similar for when running your app in an Azure app service, which I'm thinking is what you really need. This is for if you need your access to these .env files just previous to or after your app has started (post build).
To the Azure portal we go.
Navigate to your app service.
Click "Configuration" under the "Settings" heading.
For each of the variables you'll need during a production run of your app you'll need to:
Click "New application setting"
Paste the name of the variable (exactly as it appears in your .env) into the "Name" field.
Similarly, paste the value of the variable into the next field.
Check the "deployment slot setting" checkbox. As nebulous as this field is, it's necessary. When I didn't do this, the app didn't have access to this variable after running the start command.
Restart your app. If you deployment when well, then your app should have access to the variables add in the manner specified above.
I'm deploying a web app to Azure using Visual Studio Team Services to build and then release.
During release I'm replacing tokens within the web.config file with environment variables I have setup within the release definition for each environment, staging, production etc.
I have set the Release Name Format to include the build number and release number using the following:
$(Build.BuildNumber).$(rev:r)
I know Release.ReleaseName is provided as a pre-defined global release variable, but how can I access this Release.ReleaseName variable within my app to display it in a footer?
Usually, you can use Tokenization build / release task to transfer the value of a variable into web.config file.
But since the variable you want to use is filled with the value of another variable, you need to add an additional powershell step to do this.
Following are the steps for you reference:
Create a new variable.
Change the content that you want to replace in web.config to __Eddie__.
Add a powershell step to set the value of "Eddie" to "Release.ReleaseName".
Add a Tokenization step to replace the __Eddie__ in config.
Now, when you start the release, the content in web.config will be replaced by the value of Release.ReleaseName.