Use Build.Repository.Uri in Release Pipeline (Azure DevOps Server) - azure-devops

I need to use Build.Repository.Uri in a release pipeline. (to pass it to a PowerShell script)
In a buildpipeline:
Write-Host $(Build.Repository.Uri)
> 2019-07-15T08:30:51.8695425Z http://138.202.18.216:8070/Samples/Framework%20A/_git/Framework%20A
In a releasepipeline:
Write-Host $(Build.Repository.Uri)
> The name Build.Repository.Uri was not recognized as the name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if the path is correct (if included), and try again.
Why that inconsistency?
I also try Write-Host $(env:BUILD_REPOSITORY_URI) because of that: How to read directory path of the Artifact in Release pipeline in Azure DevOps? (I also don't understand the logic behind . to _)
Is there a way to get Build.Repository.Uri in a releasepipeline?
EDIT: Solution
"$env:SYSTEM_TASKDEFINITIONSURI$env:BUILD_PROJECTNAME/_git/$env:BUILD_REPOSITORY_NAME" -> http://136.202.18.216:8070/Samples/Framework A/_git/Framework A
If you set system.debug variable to true, you can find all predefined variables inside of the Job Initialize (Auftrag initialisieren) Report after a build.
If your project or repository name contains spaces, make sure that you replace them in your script with %20:
$Uri = $Uri.Replace(" ", "%20")

To access the Build URI in the Release Pipeline you need to use the release variable:
Release.Artifacts.{alias}.BuildURI
{alias} is the the alias of the artifact source you have in the release
If you accessing variables within PowerShell scripts you need to replace any dots with underscores i.e. $env:RELEASE_ARTIFACTS_{alias}_BUILDURI
Source: https://learn.microsoft.com/en-us/azure/devops/pipelines/release/variables?view=azure-devops&tabs=batch

The variable Build.Repository.Uri is agent-scoped. It can be used as an environment variable in a script and as a parameter in a build task. When you add variable System.Debug with value true in the pipeline, the init job will log all the available environment variables, which includes the REPOSITORY_URI.
You can try with following variables:
Write-Host $env:BUILD_REPOSITORY_URI
Or
Write-Host $env:RELEASE_ARTIFACTS_{alias}_REPOSITORY_URI
Please note that the {alias} is the uppercase of the Artifact source alias.

Related

How to send value from powershell script to the jenkinsfile

I have to send buildId value from powershell script to the jenkinsfile pipeline. I am trying something like below. Is this right approach?
powershell script
if($out -match "buildId-") {
$splitline = $out.Split("-")
echo "splitline: " $splitline
$buildId= $splitline[1]
echo "buildId: " $buildId
$buildIds= $env:$buildId.Value
}
then want to use that build to pass as a parameter to trigger the build job.
Jenkinsfile
build job: 'build_Test', parameters: [validatingString(name: 'buildId', value: '$buildIds'), string(name: 'TASK', value: 'build')]
You can give it a shot, but I'm not sure simply adding it to an environment variable will allow you to access the value from the Jenkinsfile. You can find more about this problem in this question. However, you maybe able to achieve your goal via the EnvInject plugin, or simply store the value in a file in your workspace and access it via Jenkins later.

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]

Can we override Azure DevOps variable groups values from a file input

I have a complete CI/CD pipeline in Azure DevOps and its working perfectly. now i have a JSON file including the version number for the release.
I need to get this version number as a global variable. How to assign file value to a global variable. I need to use this release no as my build pipeline id, docker tag and release pipeline id.
The way to set a global variable is:
##vso[task.setvariable variable=name;]value
So you can write a PowerShell script that read the release version from the JSON file and set the variable, for example:
$jsonFile = Get-Content path/to/json
$json = $jsonFile | ConvertFrom-Json
$version = $json.release.version
Write-Host "##vso[task.setvariable variable=releaseVersion;]$version"
Now you can use the variable $(version) in your docker tag, etc.
just to add to existing answer, here's how you set build id to the calculater value (because you cannot set it before the build starts, as it is only calculated during the build):
- pwsh: Write-Host "##vso[build.updatebuildnumber]${env:VERSION}"
assuming version is how you called your variable.

VSTS : Can I access the Build variables from Release definition?

In VSTS CI/CD , I am setting some variable's value in a Powershell task in CI.
During CD I want to access that variable's value to do something , lets say echo.
Is this possible? If so, how?
You could write it out to a json/xml file and include that file in your published artifacts of your build defintion. Then read in that file via PowerShell in your release definition.
ConvertTo-Json | Out-File "file.json"
Get-Content "file.json" | ConvertFrom-Json
For VSTS itself, it can not persists variables from build to release.
An workaround is store the variable’s value in Variable Group and link the variable group into your release definition. Detail steps as below:
During build, you can Add a variable group with the name group-$(Build.BuildId), and store the variable you want to transfer in the variable group.
During release, you can get variable groups firstly, and filter the variable under the variable group-$(Build.BuildId). And delete the group group at the end of the release.
Besides, if artifact type is build for your release definition, you can also store the variable value in a file and then publish the file as build artifacts (as Calidus says).
Check out the Azure DevOps extension Variable Tools for Azure DevOps Services.
In the "build pipeline" you can create a JSON file using "save variables". This file needs to be published as artifact or with existing artifact.
In the "release pipeline" you can restore the variables using "load variables" from the JSON file.

Unable to read VSTS Online release variables

I'm working with VSTS environment variables and stuck with variables of a secret type.
I'm using POSH script (file) to generate a variable (in fact, to obtain the value from Azure Key Vault and the set this value to the variable):
# Add as a script parameter during the release step
-ResourceGroupNameArg "$(ResourceGroupName)" -KeyVaultNameArg "$(KeyVaultName)" -KeyVaultSecretNameArg "$(KeyVaultSecretName)"
# The script itself
Param(
[string]$ResourceGroupNameArg,
[string]$KeyVaultNameArg,
[string]$KeyVaultSecretNameArg
)
<...>
$secret = Get-AzureKeyVaultSecret -VaultName $KeyVaultNameArg -Name $KeyVaultSecretNameArg
$secretValue = $secret.SecretValueText
Write-Host "##vso[task.setvariable variable=SQLAdministratorPassword;issecret=true]$secretValue"
Here I can pass to the script different KeyVault names (according to my needs) - by substituting the $KeyVaultNameArg and $KeyVaultSecretNameArg variables.
For any other variables configured using ##vso[task.setvariable variable= I am able to retrieve them using the construction $env:DatabaseName (for example in another POSH script) or $(DatabaseName) in agent phase step (using Hosted 2017 agent).
However, for the issecret=true variable or even for a manually created variable I'm unable to retrieve its values during the release deployment process.
According to this article,
The values of hidden (secret) variables are stored securely on the
server and cannot be viewed by users after they are saved. During a
deployment, the Release Management service decrypts these values when
referenced by the tasks and passes them to the agent over a secure
HTTPS channel.
So IMO the variables should be accessible for the script (or even agent phase step) despite they are secret.
Refer to these steps to do it:
Click Library tab
Click + Variable group
Specify variable group name
Enable Link secrets for an Azure Key vault as variables and link Azure key vault
Click +Add to add necessary secret(s)
Edit release definition
Choose Variables tab
Select Variable groups
Click Link variable group to link that variable group
Using the related variable directly in release task ($(variable name))