06 Sep 2020

Azure Devops Tidbits

Categories: Programming
Tags: AzDO

I was recently working on a project that invovled doing a lot of work inside Azure DevOps (AzDO). We had some unique challenges to solve and I continually ran into trouble looking for ways to treat the AzDO Git repository as I would if I were going to download files from a Git repository (using the raw endpoints). Since I struggled finding a solution, and was only able to solve with a steer from a collegue, I thought I would publish an article so others could find.

AzDO Git and curl

This was the one that had me spinning for a while. If you take a look at the docs for the REST endpoints you would think you could grab the curl example and rock and roll. My experimenting proved that to be wrong. Take a look at the example below:

curl -u {username}[:{personalaccesstoken}] https://dev.azure.com/{organization}/_apis/projects?api-version=2.0

So, how do I use this? Are the ‘{}’ supposed to stay? Are they supposed to go? What about the ‘[]’? The only thing I found that worked for this was simply to use:

username:pat

Once this was done. I could get into most of the REST API end points.

Single File downloads

This one was a bit more interesting. The first thing you have to try to suss out is where is the endpoint to use. This was not as simple. With a steer from a collegue I found it here (replace all caps with your information):

https://dev.azure.com/ORGNAME/PROJECT/_apis/git/repositories/REPOSITORY/items/PATH-TO-FILE?versionType=Branch&versionOptions=None

Using the same curl -u format as above will get you any file you need form the repository. As an interesting side note, you could also do the curl command like so to the same effect. I have not tested this everywhere and have only used this format to get files (replace all caps with your information):

curl -o FILE -s https://PAT@dev.azure.com/ORGNAME/PROJECTNAME/_apis/git/repositories/REPOSITORY/items/PATH-TO-FILE?versionType=Branch&versionOptions=None

An interesting trick to execute code in a pipeline

We also found ourselves in a situation where we needed to do something in a build pipeline that was difficult to do in bash while running a build pipeline. This may be simply that I am not a bash God but we foind updating a json file in the build was difficult. Below is the dirty hack we used to get this done. :-)

We decided to use a little C# script (CSX) to solve our problem. The first thing we need to do was place a CSX file in the repository we were looking at. In this case the script was simply going to update some JSON:

#r "nuget: Newtonsoft.Json, 12.0.3"

using System;
using System.Collections.Generic;
using Newtonsoft.Json;

public class Extension
{
    public string name { get; set; }
    public string description { get; set; }
    public string type { get; set; }
    public string version { get; set; }
    public string filePath { get; set; }
}

public class Catalog
{
    public List<Extension> extensions { get; set; }
}

if (Args.Count != 3)
{    
    Console.WriteLine("Three arguments must be passed in: <index.json> <name> <version>");
    return -1;
}

Catalog catalog = JsonConvert.DeserializeObject<Catalog>(File.ReadAllText(Args[0]));
var extension = catalog.extensions.FirstOrDefault(x=> x.name.Equals(Args[1]));
extension.version = Args[2];
File.WriteAllText(Args[0], JsonConvert.SerializeObject(catalog, Formatting.Indented));
Console.WriteLine("Updated");

Once this was in the repository we needed to two more things: 1) get the tool to run the scripts and 2) run it. To install the NET Core SDK tool in your pipeline simply run:

_curl -s https://raw.githubusercontent.com/filipw/dotnet-script/master/install/install.sh sudo bash_

From there you can simply execute the above script (in this case called ) like so:

sudo dotnet script ./update-version.csx extensions/index.json “core” $(Build.BuildNumber)

Wrap Up

I had a heck of a time trying to track down how to do this. I hope any of this is helpful to someone or at least finding it is easier.

Thank You For Reading
David White

Resident old man programmer. Get off my lawn!!!

comments powered by Disqus