Using yq version 4 to modify a .yaml to add & and * characters not in quotes - yq

Ive got a requirement to update the bitbucket pipelines yaml file by adding a task definition to the pipelines.branches.develop set of steps (adding it to be the first step, while keeping any existing steps) and if the develop branch pipeline doesn't exist, create it with the specified task definition as a step.
yq command im using the add to the develop pipeline is below:
yq '.pipelines.branches.develop = [{"step":"*My-Step"}] + .pipelines.branches.develop' bitbucket-pipelines.yml
This works but its enclosing the '*My-Step' in quotes and I cant work out how to remove them without causing the command to fail.
I have the same problem with the '&' character for my addition to the definition steps.
Could someone provide some guidance, or documentation.

Related

How can I define env variable value during the release phase in Azure?

I have an Nx monorepo project set up. I want to set some environment variables to use throughout my app. I'm looking for a solution where I can define a value for these variables during the release process to each environment (QA, Test, Dev).
Example:
NX_API_URL is my environment variable in my .env file.
If I release to QA env the variable should be NX_API_URL=api-url-qa.com.
If I release to Test env the variable should be NX_API_URL=api-url-test.com
I have found solutions during the build process but that's not going to work, it needs to be at the release phase. How can I accomplish this?
If the file that you'd like to modify during the deployment process is either XML or JSON you can define the URLs as tokens and use standard 'FileTransform' task to replace them: https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/reference/file-transform-v2?view=azure-pipelines
For more details regarding file transformation please refer to documentation: https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/transforms-variable-substitution?view=azure-devops&tabs=Classic
If none of the links above will help you with this, then I'd suggest to create PS script to do whatever modifications are required, like adding a line after specific line definition: https://social.technet.microsoft.com/Forums/office/en-US/fabc9790-0ffe-43b2-9d6b-bc483cd28bb6/add-line-to-a-text-file-just-after-a-specific-line-with-powershell?forum=winserverpowershell

What is the recommended way to update the web.config values using release pipeline variables?

I am planning to use azure devops release pipeline to deploy a .NET web app into test, uat and prod environments.
I have previously worked on projects which had the appsettings.json file and used variable substitution approach as shown in screenshot below:
When doing the variable replacement for a .NET web app containing the web.config, I am exploring the following options:
Generate web.config parameters section which is shown above the JSON section marked above. But the description says that it is meant for Python, Node.js, Go and Java apps. It doesn't mention .NET.
In the web.config file, in the value I can add something like XXMARKERXX and then via Powershell script maybe replace the above with the contents from pipeline variable?
The web.config file is XML format type file. So you couldn't use JSON variable substitution option in task to update the value.
Here are the methods to update the value in Web.config file:
1.You can use the XML variable substitution option in the Azure App service deploy task or IIS deploy task.
For more detailed steps, refer to this doc: XML variable substitution
2.You can use PowerShell script to modify the value. But you don't need to add any additional characters in web.config file.
Here is the PowerShell script sample:
$myConnectionString = "test";
$webConfig = '$(build.sourcesdirectory)\Web.config'
Function updateConfig($config)
{
$doc = (Get-Content $config) -as [Xml]
$root = $doc.get_DocumentElement();
$activeConnection = $root.connectionStrings.SelectNodes("add");
$activeConnection.SetAttribute("connectionString", $myConnectionString);
$doc.Save($config)
}
updateConfig($webConfig)
3.When you add the mark: #{..}# in web.config file, you can try to use the Replace Token task from Replace Tokens Extension.
Refer to the example in this ticket:How to perform XML Element substitution in web.config using Replace Tokens?

AzCopy ignore if source file is older

Is there an option to handle the next situation:
I have a pipeline and Copy Files task implemented in it, it is used to upload some static html file from git to blob. Everything works perfect. But sometimes I need this file to be changed in the blob storage (using hosted application tools). So, the question is: can I "detect" if my git file is older than target blob file and ignore this file for the copy task to leave it untouched. My initial idea was to use Azure file copy and use an "Optional Arguments" textbox. However, I couldn't find required option in the documentation. Does it allow such things? Or should this case be handled some other way?
I think you're looking for the isSourceNewer value for the --overwrite option.
--overwrite string Overwrite the conflicting files and blobs at the destination if this flag is set to true. (default true) Possible values include true, false, prompt, and ifSourceNewer.
More info: azcopy copy - Options
Agree with ickvdbosch. The isSourceNewer value for the --overwrite option could meet your requirements.
error: couldn't parse "ifSourceNewer" into a "OverwriteOption"
Based on my test, I could reproduce this issue in Azure file copy task.
It seems that the isSourceNewer value couldn't be set to Overwrite option in Azure File copy task.
Workaround: you could use PowerShell task to run the azcopy script to upload the files with --overwrite=ifSourceNewer
For example:
azcopy copy "filepath" "BlobURLwithSASToken" --overwrite=ifSourceNewer --recursive
For more detailed info, you could refer to this doc.
For the issue about the Azure File copy task, I suggest that you could submit a feedback ticket in the following link: Report task issues.

Sanitize branch name to avoid problems with website name

I am using Azure Devops Release pipeline to automatically publish Pull Requests to test domain branchname.blablabla.com
Everything works fine, but sometimes branch name contains dot . and deployment task fails due to 4th level domain name like branch.name.blablabla.com
Is there a way to remove illegal characters from $(Build.SourceBranchName) before it binds to PARAMETERS_WEBSITENAME?
You can add one additional task before using it in website name, to remove dot from the branch name. For example, you can add Bash task to run one shell script:
a="$(echo $(Build.SourceBranchName) | tr -d .)"
echo "##vso[task.setvariable variable=BranchName]$a"
In this script, it firstly remove dot . from $(Build.SourceBranchName) value. Then create one new variable BranchName and assign the modified value to it.
Now, in your next task, you can call this modified value by using $(BranchName).

Azure DevOps Release Pipelines - Using env parms with a period . in

I am finding using AZDO Release pipeline variables maddening in Powershell steps.
I am running an Azure PowerShell step to return a primary key value. It is 2 lines…
$primarykey = (Get-AzRelayKey -ResourceGroupName ${env:az-resourcegroupname} -Namespace ${env:az-relaynamespace} -HybridConnection ${env:serviceBus.primaryRelay.ConnectionName} -Name ${env:serviceBus.primaryRelay.KeyName} | Select-Object -ExpandProperty PrimaryKey)
Write-Host "##vso[task.setvariable variable=serviceBus.primaryRelay.Key]$primarykey"
In my pipeline I have a mix of variable names, some I have complete control over (the az- prefixed ones) and others I don’t (the ones starting serviceBus.)
The reason I have no control over the latter is that they are used for a later File Transform step that navigates an appsettings.json file to find/replace values, and its unable to be changed (for example serviceBus.primaryRelay.ConnectionName is a value that is changed in the JSON and the file transform step specifies to navigate the JSON structure, it has to be separated with a period . )
When this script runs it always complains about the -HybridConnection value being empty. This is because the variable has a period in it.
I’ve tried everything I can think of to retrieve that value in the code.
Are they suggesting here that a variable with a period isn’t workable in Powershell in AZDO release pipelines? I’m completely lost.
I have found the answer by looking under the Release Pipelines "Initialize Job" log. It appears to substitute the period . with a dash -
The log revealed this...
[SERVICEBUS_PRIMARYRELAY_CONNECTIONNAME] --> [dev-sbrelay]