How to get the value of $(Build.SourceVersionMessage) variable in Release Pipeline? - azure-devops

In the Release Pipeline under Release Notes, I am passing $(Build.SourceVersionMessage) but in my app center I am getting "$(Build.SourceVersionMessage)" as a string instead of the actual value. This variable works in my build pipeline under -task. How can I use this variable or any alternative way to fetch the commit message in the Release pipeline.

How to get the value of $(Build.SourceVersionMessage) variable in Release Pipeline?
As we know the value of predefined variable $(Build.SourceVersionMessage) is the build pipeline, we could not get it from release pipeline directly.
To resolve this issue, there are three ways to do it:
Just as GeralexGR comment, writing $(Build.SourceVersionMessage) on a text file and publish this text file as artifact and parse the value in the release pipeline.
Using the REST API Definitions - Update to update the variable with the value of $(Build.SourceVersionMessage) in the build pipeline. Or using az pipelines variable-group variable to update the referenced variable group.
You could check my previous thread for some more details.
You could combine your classic release pipeline and build YAML pipeline to one pipeline with multiple stages, then we could use output variables from tasks:
dependencies.JOB.outputs['TASK.VARIABLE']

Related

Auto Increment of JAR file version in the Azure DevOPS pipeline

I have a task to increase the JAR version automatically in Azure DevOPS, once the changes are made to the particular branch.
SCM used here is Bit Bucket. Repository storage is Azure DevOPS Artifact section.
I need to pick the version from the artifact section in Azure DevOPS and increment in the azure pipeline before building the code.
Is there any way to do that?
You can try to set a version variable in pipeline, and then set conditions to increment the variable (variable auto increment once the changes are made to the particular branch), then use the variable in the subsequent tasks.
To do that, please install Build Updating Tasks extension, and use the Set variable on a build definition task to update a variable in a Build Definition with conditions.
For example:
Create a variable in your pipeline (count here):
Then, add the Set variable on a build definition task in your pipeline:
Set the conditions based on your scenario, in the following YAML sample we set a variable isMain and the value is true when the branch is main. Set the condition in Set variable on a build definition task, if the value is true, then the count variable will Autoincrement.
variables:
isMain: $[eq(variables['Build.SourceBranch'], 'refs/heads/main')]
steps:
- task: BuildVariableTask#2
displayName: Update the variable
inputs:
buildmode: 'Prime'
variable: 'count'
mode: 'Autoincrement'
usePSCore: False
condition: and(succeeded(), eq(variables.isMain, 'true'))
In the subsequent tasks you can use the "count" variable as the JAR version.

Passing variables between release pipelines in azure (trigger azure pipeline extension)

I have a requirement to pass data between 2 release pipelines (to trigger 2nd pipeline on completion of 1st pipeline).
Can we pass variables dynamically between azure RELEASE pipelines using trigger an Azure DevOps pipeline extension?
I tried this blog but unable to find/understand if we can use "output variables" to pass data between azure release pipelines.
https://msftplayground.com/2019/02/trigger-a-pipeline-from-an-azure-devops-pipeline/
Thank you in advance!
Output variables are created by the pipeline and referenced by the other tasks in the pipeline, it means they are dynamic and refers to the result of a particular task.
These cannot be defined statically.
After running the task in the pipeline, output variables value can be known.
There are two different ways to create output variables :
By building support for the variable in the task itself
Setting the value ad-hoc in a script
Below example is defining a task with the name SomeTask that natively creates an output variable called out.
In a task within that same job, you can reference that variable using $(SomeTask.out).
steps:
- task: MyTask#1
name: SomeTask
- script: echo $(SomeTask.out)
For the detailed information regarding how to create output variables and pass between the pipelines, please refer azure devops output variables.

Only start an Azure DevOps release stage if a specific artifact exists

In Azure DevOps, I have one build pipeline that runs which could produce 1 or 2 artifacts I want to release. Lets call them Artifact1 and Artifact2.
Is it possible to have one release pipeline with multiple stages that only start if a specific artifact exists? So if Artifact1 was produced, run the Stage Artifact1Stage, but not Artifact2Stage.
I see there are branch filters in DevOps, but that doesn't get me what I want. I want to filter on the artifact produced.
In the Deployment group job (in a stage) there is an Artifact download option which allows me to select the specific artifact I want, but this doesn't prevent the stage from running (and then failing if the specific artifact wasn't produced).
EDIT: If it matters, I am not using yaml syntax for the release pipeline.
I would do something like the following (assuming this is classic pipeline as you suggested):
Add a Powershell task whose script inspects the contents of $(System.DefaultWorkingDirectory) (including whatever subfolder contains your artifact) and then uses Write-Host "##vso[task.setvariable variable=whichArtifact;isoutput=true]some value that tells you if it's artifact 1 or 2 that's present"
Create 2 jobs in a single stage, one for each artifact (or two stages with one job each, your call). For the Run on agent job additional option Run this Job, choose Custom condition using variable expressions, and the value would be something like eq(variables['whichArtifact'], 'value that tells you which artifact this is')
The variable value you set and condition you choose should be mutually exclusive so that you can clearly select the stage/job you wish to run.

How to update a pipeline variable after the task is succeeded, through `output variable` section or some other method in Azure devops

I want to update a pipeline variable once my task is finished. Can I do it through output variable section provided in azure DevOps?
The main objective is to let downstream tasks know that the list of specific previous tasks have succeeded, which I can check by adding custom conditions.
If your tasks are in an agent job, there is no need to use the output variable section, just add a Powershell task after the successful task, you can overwrite/update the value of the variables by using the logging command to set the variables again in Azure Devops pipeline.
Write-Host "##vso[task.setvariable variable={variableName}]{updateValue}"
To set a variable from a script, you use the task.setvariable logging command. This doesn't update the environment variables, but it does make the new variable available to downstream steps within the same job. In addition ,here is a reference.

Pipeline parameter overwrites variable value

I have a pipeline in Azure DevOps somewhat like this:
parameters:
- name: Scenario
displayName: Scenario suite
type: string
default: 'Default'
variables:
Scenario: ${{ parameters.Scenario }}
...
steps:
- script: echo Scenario is $(Scenario)
And I'm executing the pipeline via the VSTS CLI like this:
vsts build queue ... --variables Scenario=Test
When I run my pipeline, it seems that the parameter default value overwrites my cmd line specified variable value and I get the step output Scenario is Default. I tried something like Scenario: $[coalesce(variables['Scenario'], ${{ parameters.Scenario }})] but I think I got the syntax wrong because that caused a parsing issue.
What would be the best way to only use the parameter value if the Scenario variable has not already been set?
What would be the best way to only use the parameter value if the
Scenario variable has not already been set?
Sorry but as I know your scenario is not supported by design. The Note here has stated that:
When you set a variable in the YAML file, don't define it in the web editor as settable at queue time. You can't currently change variables that are set in the YAML file at queue time. If you need a variable to be settable at queue time, don't set it in the YAML file.
The --variables switch in command can only be used to overwrite the variables which are marked as Settable at queue time. Since yaml pipeline doesn't support Settable variables by design, your --variables Scenario=Test won't actually be passed when queuing the yaml pipeline.
Here're my several tests to prove that:
1.Yaml pipeline which doesn't support Settable variable at Queue time:
pool:
vmImage: 'windows-latest'
variables:
Scenario: Test
steps:
- script: echo Scenario is $(Scenario)
I ran the command vsts build queue ... --variables Scenario=Test123, the pipeline run started but the output log would always be Scenario is Test instead of expected Scenario is Test123. It proves that it's not Pipeline parameter overwrites variable value, instead the --variables Scenario=xxx doesn't get passed cause yaml pipeline doesn't support Settable variables.
2.Create Classic UI build pipeline with pipeline variable Scenario:
Queuing it via command az pipelines build queue ... --variables Scenario=Test12345(It has the same function like vsts build queue ... --variables Scenario=Test) only gives this error:
Could not queue the build because there were validation errors or warnings.
3.Then enable the Settable at queue time option of this variable:
Run the same command again and now it works to queue the build. Also it succeeds to overwrite the original pipeline variable with the new value set in command-line.
You can do similar tests like what I did to figure out the cause of the behavior you met.
In addition:
VSTS CLI has been deprecated and replaced by Azure CLI with the Azure DevOps extension for a long time. So now it's more recommend to use az pipelines build queue
instead.
Lance had a great suggestion, but here is how I ended up solving it:
- name: Scenario
displayName: Scenario suite
type: string
default: 'Default'
variables:
ScenarioFinal: $[coalesce(variables['Scenario'], '${{ parameters.Scenario }}')]
...
steps:
- script: echo Scenario is $(ScenarioFinal)
In this case we use the coalesce expression to assign the value of a new variable, ScenarioFinal. This way we can still use --variables Scenario=Test via the CLI or use the parameter via the pipeline UI. coalesce will take the first non-null value and effectively "reorder" the precedence Lance linked to here: https://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#expansion-of-variables
(Note that there need to be single quotes around the parameter reference '${{}}' because the ${{}} is simply converted to to the value, but then the coalesce expression doesn't know how to interpret the raw value unless it has the single quotes around it to denote it as a string)
Note that the ability to set parameters via the CLI is a current feature suggestion here: https://github.com/Azure/azure-devops-cli-extension/issues/972