.Net, Azure and occasionally gamedev

Azure DevOps status badges

2018/10/06

Another quick Azure DevOps powershell script. Click here for the script hosted as a gist.

Here's a quick onelineer that you can run locally:

Get-Badges.ps1 -Token $BadgePAT -AccountName $accountName | Out-File status.md

Status badge support for both builds and releases has been available for quite some time. With the wiki it is also super easy to create a dedicated page that shows the status of the entire project CI pipeline by embedding the status badges into a markdown document.

While one could certainly manually go to each build and release, turn on the status badge support and then manually add it to the wiki, this task is cumbersome and screams "automation".

So I wrote a script to automate it. The big plus here is the script itself can run as a (scheduled) build/release and thus updates the wiki automatically to include all current pipelines every day.

The script simply iterates all projects, builds and releases and outputs them into a nice format. It lists builds and releases in separate tables alphabetically:

DevOps status DevOps status

It has the same filters as my previous script to filter projects, builds and releases.

The script also has two modes: Readonly or read/write. The first will merely get all the badges where possible, whereas the second mode needs write access to enable badges.

By default any new release definition has its badges disabled and they must be manually enabled (per environment). If you grant the script write access, it will automatically enable those by updating the release definition for your.

With read-only access, the script will only display builds/releases where the badges are enabled and ignores all other pipelines.

Usage

Since I wanted it fully automated I have setup a scheduled build that runs once per day and regenerates the markdown file and uploads it to the internal project wiki.

The build is "slightly complicated" since I didn't want to save the wiki and the script in the same repository (you are of course free to do so)

I create two repositories: "DevOps" and "wiki". The first contains the script(s), the second is used for the wiki (duh).

The pipeline works based on the DevOps repo and is scheduled to execute these 3 simple steps daily:

  1. Download wiki repo into temporary folder using git commandline
  2. Run script from actual DevOps repo to regenerate markdown file and save output into wiki repo
  3. Commit and push wiki repo using git commandline

The last step will only create a new commit if there are actually any changes in the file! And if it does the Azure DevOps team wiki is automatically updated.

In case you want to use the build definition, here's the yaml:

resources:
- repo: DevOps
  clean: true
queue:
  name: 'Hosted VS2017'
variables:
  WikiRepo: 'wiki'
  BadgePAT: 'save it as secret or store in keyvault, needs build/release read (and write) access as per the script documentation'
  CodePAT: 'second PAT that has access to read and commit code, used to download/update the separate wiki repository'
steps:
- powershell: |
   $vstsRepoUri = "$env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI".Substring(8) + "$(system.teamProject)/_git/$(WikiRepo)"
   git clone "https://$(CodePAT)@$vstsRepoUri" 
  workingDirectory: '$(build.artifactstagingdirectory)'

  displayName: 'Checkout wiki'

- powershell: |
   # format is https://your-name.visualstudio.com, we just need "your-name"
   $accountName = "$env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI".Substring(8)
   $accountName = $accountName.Substring(0, $accountName.IndexOf("."))
   Scripts\Get-Badges.ps1 -Token $(BadgePAT) -AccountName $accountName | Out-File $(build.artifactstagingdirectory)/$(WikiRepo)/index.md 

  displayName: 'Get badges'

- powershell: |
   git config user.name CI
   git config user.email "CI@devops.azure.com"
   git add .
   git commit -m "current pipelines"
   
   $vstsRepoUri = "$env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI".Substring(8) + "$(system.teamProject)/_git/$(WikiRepo)"
   
   git remote add wiki "https://$(CodePAT)@$vstsRepoUri"
   git push --set-upstream wiki master 
  workingDirectory: '$(build.artifactstagingdirectory)/$(WikiRepo)'

  displayName: 'Commit & push'

I now have an up to date overview of all my pipelines and can identify broken pipelines very quickly!

Here's the link to the script gist again.

tagged as Visual Studio Team Services, VSTS and Azure DevOps