Azure Devops CLI Task - show output - azure-devops

I am using the Azure CLI task Version 2. The only problem, i need some output so that it´s showed as part of the pipeline run. The task however wraps all inline scripts in a script file so that i can´t see the output. How can I achieve that?

Thank you. It works now, you are right, I used the Bash#3 task instead. I use the output from the AzCLI task to define a pipeline variable and the bash task to print it.
service=$(kubectl get service --namespace $(aks-namespace) -o jsonpath='{.items[0].status.loadBalancer.ingress[0]}')
echo "##vso[task.setvariable variable=serviceip]$service"

Related

Azure DevOps: How to set a Pipeline Variable on a Release?

I am trying to create a simple Release Pipeline that:
sets a value via a script
asks a user to manually check the value, and modify it if needed
goes on with the pipeline, using the value
What is the easiest way to achieve this functionality in Azure DevOps?
I have tried to define a Pipeline Variable called TestVariable, with initial value of 1, where I will store the value.
A Pipeline Variable seems a good option to me, because its value can be easily modified by the user, during manual check.
The problem is, I cannot set its value via a script (as per point 1. above).
I have tried to set its value to 2 via a Bash Script, but it does not work as you can see.
Here is the code, if you want to try yourself:
echo $(TestVariable)
echo "##vso[task.setvariable variable=TestVariable;]2"
echo $(TestVariable)
I searched thoroughly the Azure documentation but I could not find any solution. Is there any way to set a Pipeline Variable through a script, or to achieve the 3 above mentioned points with another strategy?
The method you used to update the value is correct.
Refer to this doc: Set variables in scripts
A script in your pipeline can define a variable so that it can be consumed by one of the subsequent steps in the pipeline.
It will not work on the current task, but the updated value can be used in the next tasks.
Here is an example:
steps:
- bash: |
echo $(TestVariable)
echo "##vso[task.setvariable variable=TestVariable;]2"
displayName: 'Bash Script'
- bash: |
echo $(TestVariable)
Result:

Azure Devops pipelines set bash shell options for entire pipeline?

Is there a way to set default shell options for the entire pipelines file?
In github actions you could do it at the workflow level using the 'defaults > shell' value e.g. bash --noprofile --norc -eo pipefail {0}
and this would affect all shell steps in the entire workflow. As far as I can tell, there is no equivalent option in azure devops and I would be forced to copy/paste for each 'bash' task.
You can add the SHELLOPTS variable for the entire pipeline, which would at least get you the -eo pipefail part:
variables:
SHELLOPTS: errexit:pipefail
It is currently not supported to set default shell options in Azure DevOps. Every bash task represents a new session in one Azure Pipeline workflow.
You could create a new Azure DevOps feature request via : https://developercommunity.visualstudio.com/report?space=21&entry=suggestion

Azure DevOps Pipeline - Using YAML, how can I have the build number set to dynamically change, based on the version defined in a different file? [duplicate]

In Azure DevOps, I created a Build. In that Build I created a ProjectBuildNumber Pipeline variable that is Settable at queue time. That variable is then used under Options -> Build number format to set my build number displayed in Azure.
However, I am trying to make that ProjectBuildNumber variable settable in the code I am building/deploying. Is there a way I can have a Task in my Build to update that ProjectBuildNumber and update the Build number in Azure DevOps?
Is there a way I can have a Task in my Build to update that ProjectBuildNumber and update the Build number in Azure DevOps?
The answer is yes.
You could add a Inline Power-Shell task in your build definition to update the value of ProjectBuildNumber and then update the build number base on the it:
Write-Host "##vso[task.setvariable variable=ProjectBuildNumber;]YourUpdateValue"
Write-Host "##vso[build.updatebuildnumber]xxx.$(ProjectBuildNumber).xxx.xxx"
Check the Logging Command during the build for some more details:
Besides, if you want to update the value of a Pipeline Variable on the UI/web portal, you need the REST API (Definitions - Update) to update the value of the build pipeline definition variable from a build task.
There is a very similar thread, you can check the answer for the details:
How to modify Azure DevOps release definition variable from a release task?
Note:Change the API to the build definitions:
PUT https://dev.azure.com/{organization}/{project}/_apis/build/definitions/{definitionId}?api-version=5.0
Hope this helps.
We can update Build Number in Azure Devops via two ways .
One from Option Section/Tab and 2nd Via PowerShell Scripts.
To update the build number from Power shell Script.. We need to add following script..
Write-Host "##vso[build.updatebuildnumber]$(VersionNumber).$(VersionRevision)"
Here we have used 2 variables : VersionNumber and VersionRevision.
We need to add 2 variables in PipeLine Configurations..
VersionNumber will be the desired number and VersionRevision is the counter number that will be updated every time, when ever we will create a new build.
Please check the complete demonstration from You tube video
https://youtu.be/WBmFTmzopiQ
Have created power shell task for that
# replace existing Build.BuildNumber with
# NAME_2.1.2.54_20211220.16_345
- task: PowerShell#2
displayName: 'Update Version Number'
inputs:
targetType: 'inline'
script: |
$lines = Get-ChildItem ".\Project\My Project\AssemblyInfo.vb"
$match = $lines | Select-String -Pattern "\<Assembly\:\s+AssemblyVersion\(""(\d+\.\d+\.\d+\.\d+)""\)\>"
$version = $match.Matches[0].Groups[1].Value
[Version]::Parse($version) # validate
$tag = "NAME_$($version)_$(Build.BuildNumber)_$(Build.BuildId)"
Write-Host "##vso[build.updatebuildnumber]$tag"
Check out the Microsoft documentation on this: Variables
Depending on your operating system you can add a Powershell/Batch/Bash Task and change the variable.
Edit: After some research it appears that the change of the variable will show up in the following task. Take a look at this issue Update environment variables using task.setvariable in a bash script does not work

Run CURL GET commands in Azure Pipeline Task

I am looking to run some CURL commands (GET mainly) in an Azure Pipeline Task to list/download some artifacts from a few sources. Would appreciate some help with any examples of how I can achieve this through a Pipeline Task using CURL, Powershell, Windows Command or any other appropriate method.
Thanks
The easiest way to do this is to use the Command Line task. For a YAML pipeline, this would look like
- task: CmdLine#2
displayName: Curl Example
inputs:
script: 'curl google.com'
For a "Classic UI" pipeline, it would look like
Note that for either of these options you must be using a Linux build agent. If you are using a Windows build agent, you should use Invoke-WebRequest.
curl in PowerShell uses Invoke-WebRequest. From PowerShell 3.03.0 and above, we could use Invoke-WebRequest, which is equivalent to curl. You could refer to this doc for more details
Sample script in the power shell
Invoke-WebRequest -URI https://www.educative.io/
In addition, in the Azure DevOps Artifacts, the URL contain special characters, we need use "" contain it. Such as
https://dev.azure.com/{Org name}/{project name}/_packaging?_a=feed+"&"+feed={feed name}

How to use output variable of Deploy to Kubernetes task in Azure pipeline (VSTS)

I am new to VSTS and Azure Kubernetes. I am building a VSTS CD pipeline. I have added a Deploy to Kubernetes task in my pipeline. I am executing the get command and trying to store the output in the output variables (which is available at the bottom of the Deploy to kubernetes task). I have set the variable name.
I am trying to fetch the value of the above output variable. I have used command line task to set a value to the variable as mentioned below
echo '##vso[task.setvariable variable=myStatusVar;isSecret=false;]$(myvar)'
where myvar is the variable, which is set in the Deploy to kubernetes task as output variable.
After that in another command line task, I am trying to access the myStatusVar variable value, but when I execute the release pipeline, it shows the message:
myvar command not found
Can anyone let me know, how to use the output variable of the Deploy to kuberentes task of VSTS pipeline?
As stated in the comments your variable is 'exposed' as 'myvar.KubectlOutput'
the way you are able to use it in scripts differs based on type of scripting you are doing:
Batch script: %MYVAR_KUBECTLOUTPUT%
PowerShell script: $env:MYVAR_KUBECTLOUTPUT
Bash script: $MYVAR_KUBECTLOUTPUT
Azure Devops 'designer view': $(myvar.KubectlOutput)
For more details on this see the documentation on using variables in Azure DevOps: https://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch