Skip to content

Instantly share code, notes, and snippets.

Created November 18, 2018 23:25
What would you like to do?
Azure Pipelines Powershell Build Script
Sends a request to the Github Pages API to trigger a Pages build for the repository.
Uses Invoke-WebRequest method to send a POST request to the Github Pages API. This request is directed at the repository
API URL, and requires an OAuth token with appropriate permissions to be specified in order for the request to be
accepted by the API.
Specify the OAuth API Token with which to query the Github API. If a token is not provided, or the provided token does
not have sufficient permissions, the request will typically respond with a 404 error for any non-publicly-available
information, such as Pages build statuses and build requests.
Specify the username of the github account the pages repo is attached to.
.PARAMETER Repository
Specify the repository name as it appears in the Github URL.
Invoke-PagesBuild.ps1 -Token $ApiToken -Username 'GithubUser' -Repository ''
A 404 response may be **either** a malformed URI, caused by specifying the wrong username or repository name,
**OR** it can also be a rejected token. Github's API opts to send a 404 response rather than a flat out rejection if the
authentication token is rejected.
[Parameter(Position = 0, Mandatory)]
[Parameter(Position = 1)]
$Username = $env:SYSTEM_TEAMPROJECT -replace '\..+$',
[Parameter(Position = 2)]
$Repository = $env:SYSTEM_TEAMPROJECT
$ApiParams = @{
Uri = "$Username/$Repository/pages/builds"
Method = 'POST'
Headers = @{
Authorization = 'token {0}' -f $Token
Accept = 'application/vnd.github.mister-fantastic-preview+json'
'Query URI: {0}' -f $ApiParams['Uri'] | Write-Host -ForegroundColor Blue
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 #required on Windows containers, not on PSCore builds
Invoke-RestMethod @ApiParams
#region Update TwitterTitle variable for the tweet.
$Date = Get-Date -Format yyyy-MM-dd
$BlogPostFile = Get-ChildItem -Path $PSScriptRoot\_posts |
Where-Object {$_.Name -match "$Date"} |
Select-Object -ExpandProperty Name
$BlogPostFile -match "\d-([a-z-]+)\.md"
$UrlEnding = $matches[1]
Write-Host "##vso[task.setvariable variable=BlogPostTitle]$UrlEnding"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment