Posted on 2 Comments

Update variable group using Azure DevOps rest API – POSTMAN

I was struggling to update a variable group using the Azure DevOps Rest API. In this article I will document the procedure using POSTMAN.

First things first you should create a PAT in order to interact with the API. If you do not know how to create such a thing you should read my previous article about running a build through a REST api on which I documented also the creation of a PAT.

Then you will need to add the access token under authorization tab of POSTMAN using Type Basic Auth. The PAT should be added as plain text.

Then you will need to add Content-Type as application/json under Headers.

Then you will have to create your URL. This should be of the format:

https://dev.azure.com/Organization/project/_apis/distributedtask/variablegroups/groupVariableID?api-version=5.1-preview.1

Important: You should use the version=5.1-preview.1. If you use the latest version you will notice an error on the call. This is a bug that has not been fixed as I found online.

In my example I wanted to update the variable group with the ID 5 and add a variable named new-var. The body of your request should be like below. Keep in mind that we use the PUT HTTP verb to update the variable group. This means everything that is inside the variable group will be discarded. If you followed all the steps correctly you will notice the below output JSON. This should indicate success on the procedure.

Lastly you can locate your new variable inside the variable group.

Variablegroups – Update – REST API (Azure DevOps Task Agent) | Microsoft Docs

Video tutorial on YouTube:

Posted on Leave a comment

Powershell options for tasks – Azure Devops

Powershell is used very often on Azure DevOps tasks, as with it you can implement functionality that is not supported out of the box.

In this article I will explain some options that you may need during your executions.

You can fail a task on Azure Devops with powershell checks. For instance you can integrate your logic and if you get a response that is not the requested you can fail the task.

Fail a powershell task on Azure DevOps

$json = WebServiceCall ConvertFrom-Json
if ($json.value-eq "correct") { Write-Host success }  else { exit 1  }

Continue a failed task on Azure DevOps.

Under control options you should enable continue on error

This will result on a partial success.


Retry a failed task on failure

Run the task under certain conditions

Change working directory of powershell

When running a file path and not an inline script you can also define arguments

Posted on 1 Comment

Powershell handle Invoke-WebRequest 404 error

In some cases you may need to use Invoke-WebRequest of powershell as a correct result with status code of 404. A use case for this scenario would be a smoke test for a particular service or URL. In newer powershell versions you can use SkipHttpErrorCheck in order to stop powershell from failing the script.

The below example is a simple Web request that will fail on Powershell <7 with an exception if a 404 response is returned from the webpage.

$req = Invoke-WebRequest -Uri $url -Method GET

The parameter that you could use with Powershell 7++ and you would not have the powershell task failed would be.

$req = Invoke-WebRequest -Uri $url -Method GET -SkipHttpErrorCheck

In order to bypass this issue you could handle the exception on Powershell <7 and add your logic inside the catch block. For example

try {
    $resultApi = MakeWebRequest("http://localhost:1234/api/console") 
}
catch {
    if( $_.Exception.Response.StatusCode.Value__ -eq 404 ) 
    {
        Write-Host "API console Test 404 , proceeding ..."
    }
    else {
        Write-Host "False response..."
    }
}
Posted on Leave a comment

Automatically update your GitHub repositories with a powershell script

Developers often have a lot of repositories stored on their local machines. These repositories get updated from other developers and they stay outdated. In many cases developers forget to fetch and pull the latest changes on those repositories and when they commit code, the IDE will notify of the new changes. When this is the case, the commit will get an non explanatory message as the latest of the commit and you will have to navigate on the actual commit to verify the changes and commit message.

Commit message
Merge branch test/v3.0.0 of https://github.com/org/repository

In order to resolve this issue, you can create a powershell script that can automatically fetch the latest changes of your local repositories. You will need to change your repositories base location.

#change your github location
$github_directory = "C:\Users\galexiou\Documents\GitHub"
Get-ChildItem $github_directory | ForEach-Object {
if($_.Attributes -eq "Directory")
{
Write-Host $_.FullName
Set-Location $_.FullName
git fetch
git pull
}
}

As you can see from the output below this script will go and fetch the latest changes on the repositories that have been updated.

You can also create a cron job or an automated windows task in order to run this job automatically on computer startup or on your work schedule start. For example

On task scheduler press create task

and select your triggers (when this task will run) along with the action. This will be the run of the powershell script. On the argument you must specify the -File location (where you stored your powershell script).