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}
Related
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
For the reasons that are beyond the scope of this discussion, I have the following Azure DevOps setup which is being used to build some .NET library:
An Azure DevOps Powershell task:
- task: PowerShell#2
displayName: 'My task'
inputs:
targetType: filePath
filePath: 'build_cake.ps1'
arguments: '<my arguments>'
The build_cake.ps1 script, executed by the DevOps task, which invokes Cake passing it the build.cake script:
try {
Invoke-Expression "& <invoke Cake with build.cake>"
}
catch {
exit 1;
}
The build.cakescript which sets up some parameters and calls Build.RunDotNetCore:
Build.SetParameters(...);
Build.RunDotNetCore();
The RunDotNetCore call internally executes the dotnet build command. When that build fails for whatever reason, the error is not captured and/or propagated all the way back, so the Azure DevOps task (1) still happily reports successful execution.
I'm looking for a way to fix this.
At this point I'm not even sure whether the issue/solution is on the Cake side or on the Powershell side.
As my example shows, I have tried to wrap Invoke-Expression in a try/catch block but that didn't fix anything. I have also tried checking the $LASTEXITCODE code right after the Invoke-Expression call. (I guess $LASTEXITCODE is not supposed to be set anyway, since I'm invoking an external application and not a commandlet.)
To be clear, I'm not looking for an alternative solution (for example, to replace the existing setup with an equivalent Cake Azure DevOps task). I need to fix this particular setup with minimal changes, if possible.
The boostrapper I'm using looks like this and is confirmed working with Azure DevOps
$ErrorActionPreference = 'Stop'
dotnet tool restore
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
dotnet cake #args
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
Above assumes your using a .NET tool manifest to version Cake, if you have Cake installed already on the agent you can skip the dotnet tool restore part.
Versioning Cake with a tool-manifest is preferred though as that ensures same version of Cake used locally and remote.
Installing Cake via tool manifest is done using dotnet cli in your repository root.
dotnet new tool-manifest
dotnet tool install Cake.Tool
and then add & commit in the .config/dotnet-tools.json file created.
Then when cloning the repo it's
dotnet tool restore
dotnet cake
And it'll fetch and execute the right version of Cake.
I want to tag my sonarqube projects automatically, via azure Devops.
I tried, but without success, the pipeline runs but doesn't tag the project.
- task: CmdLine#2
displayName: Add Tag Sonarqube
inputs:
script: 'curl -X POST urlsonarqube/api/project_tags/set?project=NameProject&tags=NameTags'
Is any token missing?
Sonarqube Community Edition Version 8.2
This method requires POST method according to documentation
https://sonarqube.inria.fr/sonarqube/web_api/api/project_tags
Yout command uses GET method.
Try to use 'curl -X POST urlsonarqube/api/project_tags/set?project=NameProject&tags=NameTags'
After much researching, we found the right solution.
- task: CmdLine#2
inputs:
script: 'curl -u tokenSonarqube: -X POST ''https://urlsonarqube/api/project_tags/set?project=ProjectName&tags=Tag1,Tag2,Tag3'''
There is no limitation on the number of tags
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"
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