Azure Devops - Domain name is invalid - azure-devops

I am trying to create a virtual machine using ARM template.
I have added ARM template in my visual studio and have checked-in into the Azure Repos.
On running release pipeline I am receiving below error:
InvalidDomainNameLabel: The domain name label $(dns) is invalid. It must conform to the following regular expression: ^[a-z][a-z0-9-]{1,61}[a-z0-9]$. []
Below is the parameter.json file:
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"adminUsername": {
"value": "Myadmin"
},
"dnsNameForPublicIP": {
"value": "mynewdevvmbox003"
}
}
}
dns name "mynewdevvmbox003" is matching the regular expression requirement but still, I am getting this error.

InvalidDomainNameLabel: The domain name label $(dns) is invalid.
According to this error message, the value of domain name that this task server got is $(dns), not mynewdevvmbox003.
I think you should specified the override parameter value in Override template parameters of Azure resource group deployment task, such as -dnsNameForPublicIP $(dns).
If you indeed specify like this, the value in parameter.json will not be parsed any more. Because the override content has higher priority than parameter.json. Here, dns is a pipeline varibale.
Please ensure select the parameter.json file in Template parameters:
And, do not specify adminUsername and dnsNameForPublicIP again in Override template parameters of the task.

Related

Read DevOps Variable as bool in ARM paramters

I am trying to read in a pipeline variable as a bool. I have the following parameters defined in json:
"parameters": {
"disableFunkyFunction": {
"value": "#{myVariables.disableFunkyFunction}#",
"type": "bool"
}...
}
In my Azure DevOps release pipeline, I have a pipeline variable called myVariables.disableFunkyFunction and the value for it is set to true. However, whenever I try and run the pipeline, it fails on the "Azure resource group deployment" step: Template parameter 'disableFunkyFunction' was provided an invalid value. Expected a value of type 'Boolean', but received a value of type 'String'. I have tried using a value of 1 instead, but to no avail.
The following works, but ideally I want to read the value from the DevOps pipeline variable, not hard code it in the parameters file:
"parameters": {
"disableFunkyFunction": {
"value": true,
"type": "bool"
}...
}
Any suggestions?
From the error message, it seems that the pipeline variable value hasn't been passed to ARM template file.
Since you have set the parameter value as #{myVariables.disableFunkyFunction}#, you can try to use the Replace Token task from Replace Tokens Extension.
Here are the steps:
Step1: Keep the parameter value as #{myVariables.disableFunkyFunction}#.
Step2: Use Replace token task to set the value.
Step3: Deploy the ARM template.
For example:
Note: You need to put the Replace token task before the ARM template deployment step.

Unexpected error while passing variable group variables (Azure DevOps) to YAML pipeline

I'm a newbie to both Azure DevOps and Terraform but, I'm trying to deploy a pipeline using a YAML file.
I have tried to run a terraform plan using a YAML file and passing variables (from AZ DevOps) but, I got the following error:
2021-11-24T18:39:46.4604561Z Error: "name" may only contain alphanumeric characters, dash, underscores, parentheses and periods
2021-11-24T18:39:46.4604832Z
2021-11-24T18:39:46.4605940Z on modules/aks/main.tf line 2, in resource "azurerm_resource_group" "aks-resource-group":
2021-11-24T18:39:46.4606436Z 2: name = var.resource_group_name
2021-11-24T18:39:46.4606609Z
2021-11-24T18:39:46.4606722Z
2021-11-24T18:39:46.4606818Z
2021-11-24T18:39:46.4607525Z Error: Error: Subnet: (Name "#{vnet_subnet_name}#" / Virtual Network Name "#{vnet_name}#" / Resource Group "RG-XX-XXXX-XXXXX-001") was not found
2021-11-24T18:39:46.4608006Z
2021-11-24T18:39:46.4608580Z on modules/aks/main.tf line 16, in data "azurerm_subnet" "subnet-project":
2021-11-24T18:39:46.4609335Z 16: data "azurerm_subnet" "subnet-project" {
The 'name' has the following format at the Variable group in the Azure DevOps UI:
RG-XX-XXXX-XXXXX-001
This is the snippet of where I included the replace token at the YAML file:
displayName: 'Replace Secrets'
inputs:
targetFiles: |
variables.tfvars
encoding: 'utf-8'
actionOnMissing: fail
tokenPattern: #{MyVar}#
And this is a sample of the variables I have in a variable group:
variable-group-sample
Also, I replace the terraform.tfvars file with something like this:
resource_group_name = "#{resource_group_name}#"
I have checked the name inserted at the UI several times but I feel the error is pointing to something else I cannot see.
Have anyone experienced something related to this error?
Thank you in advance!
tokenPattern: #{MyVar}#
It is looking for the pattern #{MyVar}# to replace. Not "something contained between #{ and }#, but the actual value #{MyVar}#. I'm guessing it's expecting a regular expression, but I'm not familiar with that task.
So the end result is that your #{token values}# aren't getting replaced.
Assuming you're using https://marketplace.visualstudio.com/items?itemName=qetza.replacetokens, you probably want to specify tokenPrefix: #{ and tokenSuffix: }# instead of using tokenPattern.
Now, having said that...
There is no reason for you to be using token replacement on a tfvars file. You should create different tfvars files for each environment, then pass in a tfvars file via the -var-file argument to Terraform. Secrets can be passed in on the command line via -var 'foo=bar'
Storing variables that represent application or deployment configuration in Azure DevOps (or GitHub, or any other CI system) is a big, big anti-pattern, because it's tightly coupling your deployment process to a particular platform. If you're sourcing all of your variables from Azure DevOps, you can't easily test locally or migrate to a different CI/CD provider like GitHub Actions in the future.
For values that shouldn't be in source control, such a secrets, you should use a secret provider like Azure KeyVault and integrate it with your application (or, in this case, use a data resource in Terraform to pull the necessary secrets automatically at deployment time).

Create Azure Devops/Pipeline release including all meta data like buildNumber via REST

I'am aware of https://learn.microsoft.com/en-us/rest/api/azure/devops/release/releases/create?view=azure-devops-rest-6.0#uri-parameters and i can create releases via REST, but ...
Problem:
The issue with those releases is, that a release triggered via the REST lacks some predefined variables like Build.BuildNumber - or at least, they are not available at all scopes.
It seems like Build.BuildNumber is available in the pipeline stage, but is missing in when the release name format is computed. This means, a release name format Release-$(Build.BuildNumber)($(Release.ReleaseId)) will end up with a blank Build.BuildNumber when created using the payload below.
Details:
My json payload
{
"definitionId": 9,
"description": "Test Release",
"artifacts": [
{
"alias": "My Build Artifact",
"instanceReference": {
"id": "6989",
"name": null
}
}
],
"isDraft": false,
"reason": "none",
"manualEnvironments": null
}
While artifacts.instanceReference.id references a valid build (which of course has a buildNumber) - so that during the release stage Build.BuildNumber is properly populated.
I send the payload via
curl -X POST -u username:redactedPAT -H "Content-Type: application/json" -d #payload.json https://vsrm.dev.azure.com/redactedCompany/redactedProjectId/_apis/release/releases\?api-version\=6.0
Question:
What is the right way to create a release as if this would be created via the GUI including all the META-Data?
Do i miss use the API somehow or do i need to manually set the environment variables for this to work (or even somehow via variables).
Since the buildNumber is available in the stage, could that be rather/even a bug?
The release name is evaluated at the compile time, which means it is populated before the pipeline tasks are executed. You can check out below workarounds to populated the release name.
1, The $(Build.BuildNumber) variable you defined in the release name format will retrieve the name property you pass to the artifacts instanceReference in the request body. So you can pass the BuildNumber value to the name property under instanceReference in the request body. See here.
{
...
"artifacts": [
{
"alias": "My Build Artifact",
"instanceReference": {
"id": "6989",
"name": BuildNumber #set the buildNumber here.
}
}
],
...
}
2, Another workaround is to use logging commands to update the release name in a script task.
You can add a script task in your release pipeline stage. And run below logging command to update the release name during the release pipeline execution.
echo "##vso[release.updatereleasename]Release-$(Build.BuildNumber)($(Release.ReleaseId))"
See below example:
When the task is executed. it will update the release name with your desired format.

How to use Environment Variables in CI/CD Pipeline in Azure

I am creating the CI/CD pipeline in Azure DevOps for .NetCore API, and I have multiple deployment stages like QA, UAT and Production. Every stages has its own setting like database connectionstrings and many other, and I have one appsettings.json file in which I declared these settings, so its very hard work to change appsettings.json file each time in deployment, so my question is that how I can use environment variables in CI/CD pipelines to avoid all this stuff,
As I Just need to define the variables values in variables section and CD pipeline can automatically pick it from my appsetting.json file.
appsetting.json File
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"SubscriptionClientName": "api",
"MessageBrokerRetryCount": 5,
"ConnectionStrings": {
"CaseDBConnectionString": "Server=db-server;Database=api_db;User ID=user;pwd =Password;"
},
//For Dev
"MessageBrokerConnection": "ip",
"MessageBrokerUserName": "john",
"MessageBrokerPassword": "john"
}
We typically use the "Replace Tokens" task in our releases, and then set the variables in our release pipeline for each environment that will replace the tokens in our appsettings.json file. Very simple to use and set up.
UPDATE:
In your appsettings.json file, you would do something like:
"Username": "#{AccountUser}#"
Then in your pipeline, you would go into your release and create a variable called "AccountUser". Then you use the Replace Tokens task in your release as the first step, and it will replace #{AccountUser}# in your appsettings.json file with whatever the value of the variable is. Set different variables for each Scope (dev, stage, production or whatever you call your environments in your release) so that your appsettings.json file gets the appropriate value per environment.
a more reasonable approach would be to use application settings of you App Service, they would override values in appsettings.json\web.config
https://learn.microsoft.com/en-us/azure/app-service/configure-common
this might require slight code changes on your part:
https://learn.microsoft.com/en-us/azure/app-service/containers/configure-language-dotnetcore#access-environment-variables

Starting Runbook via Azure API, Portal, or ISE Add-On yields "input parameter type mismatch" error

Given a simple runbook:
workflow test
{
[CmdletBinding()]
param([string] $NumericString)
write-output $NumericString
}
When starting it with a numeric value (ie: 5) via the Azure Portal as a new Job (published), via the Test Pane, or using the Azure Automation PowerShell ISE Add-On, the following error is returned and the execution Fails.
[edit] Just out of curiosity I tried some other values. 'true' or 'false' (without quotes in ise/the ui) will also cause the error (and are sent to the API inside quotes).[\edit]
The values provided for the root activity's arguments did not satisfy
the root activity's requirements: 'DynamicActivity': Expected an input
parameter value of type 'System.String' for parameter named 'Numeric'.
Parameter name: rootArgumentValues
AFAIK, this is not a factor when I've executed via a parent runbook, webhook, etc.
The PowerShell ISE Add-On issues this PUT request to the API:
(https://management.azure.com/subscriptions/<id>/resourceGroups/<rg>/providers/Microsoft.Automation/automationAccounts/<aa>/runbooks/<rb>/draft/testJob?api-version=2015-10-31)
{
"parameters": {
"Numeric": "5" <-- notice it's a string
}
}
This is a bug in the Automation portal and ISE add on. For portal, this should be fixed in a week or so. For ISE add on, can you please file a bug on this here: https://github.com/azureautomation/azure-automation-ise-addon/issues
This would appear to be a bug, similar to Azure Automation Error 'DynamicActivity': Expected an input parameter value of type
To mitigate the issue, just wrap your numeric value in quotes in the Portal or ISE Add-On
If you are starting runbooks outside of the formal SDKs, it appears you need to know about required extra escaping for sending Numeric or Boolean values for string parameters (in the least).
This is what the subsequent PUT request should look like (from ISE Add-On)
{
"parameters": {
"Numeric": "\"5\""
}
}