The AppVeyor API and PowerShell: Getting Started

choubertsprojects

The Best WordPress plugins!

1. WP Reset

2. WP 301 Redirects

3. WP Force SSL

AppVeyor is a continuous integration platform that provides developers with the tools they need to create and deploy apps faster. It’s built on top of Microsoft Azure, so users can focus on coding while it takes care of all the infrastructure requirements like configuration management, build processes, release pipelines etc., saving time for app developers. PowerShell scripts are often used by AppVeyor users because they’re easy to write and help automate scripting tasks including building packages and performing deployments.

“The AppVeyor API and PowerShell: Getting Started” is a blog post by the author of this article. The article goes over how to download from appveyor and what you can do with it.

The AppVeyor API and PowerShell: Getting Started

AppVeyor, a Windows and Linux continuous integration solution, is an excellent tool for automating CI/CD pipelines. We can create some nice automation around this using its built-in REST API with both the cloud and on-prem versions, an AppVeyor API key, and a little PowerShell!

AppVeyor is the sponsor of this article. I definitely suggest them if you’re seeking for a full continuous integration platform that can operate both in the cloud and on-premise.

AppVeyor is a CI/CD solution that helps developers to create automated software pipelines. I’m a PowerShell developer, so combining AppVeyor with PowerShell is a no-brainer for me.

I just got the chance to begin administering AppVeyor via their API, and thus far everything has gone well. I’ll show you how to get started using the AppVeyor API and PowerShell in this post.

Versions of AppVeyor API

AppVeyor has two API versions in use as of 07/06/19: v1 and v2. The way you make API requests distinguishes these two API versions. These changes are detailed here, along with some language used in the documentation to refer to each API version.

Version Suffix for Endpoints Label
1 /api/ Classic/Account-Level
2 /api/account/<account-name>/ N/A

Versions of AppVeyor API

Also, if you navigate appveyor.com while investigating REST calls with Fiddler or any HTTP inspection tool, you’ll see v2 references even though the documentation is still v1. Keep an eye out for this!

Wrangling REST APIs and JSON with PowerShell is related.

Prerequisites

You’ll need an AppVeyor account to get started using PowerShell with AppVeyor (appveyor.com). Because all of my projects are public, I utilize the free account, however they also provide a premium subscription for private GitHub repos.

After you’ve created an account, you’ll need to create a project (appveyor.com) if you don’t already have one. Because I keep everything on GitHub, all I have to do is create an AppVeyor project that points to a GitHub repository.

Using PowerShell to connect to the AppVeyor API

Assuming you’ve got a project created, you now need to find your AppVeyor API token. You’ll find this by clicking in the upper-right corner of the page and then going to My Profile. Once there, click on API keys on the left and then on the dropdown in the middle. This dropdown will display All accounts and <account-name>.

The All accounts options references the v2 API while the <account-name> option references the v1 API. The v2 option has complete control over all accounts while the v1 option only has control over a single account.

In this lesson, I’ll be utilizing the v2 Appveyor API token, but if you’re worried about security, you should definitely use the v1 API key instead.

Make sure the All accounts dropdown is selected.

The API key will then be shown as a Bearer token, as illustrated below. That should be copied to your clipboard.

API Key for AppVeyor v2API Key for AppVeyor v2

The next step is to open your preferred PowerShell editor. I keep all of the common variables I’ll be dealing with at the top of the page. I’ve included the following:

  • My AppVeyor account name ($accountName), which may change depending on whether you’re on-prem or in the cloud.
  • All calls will refer to the $apiUrl, which represents the API endpoint. As you can see, I’ve created both cloud and on-premise tokens since they’ll change based on the platform you choose.
  • The Appveyor API token ($authToken) is copied from above, with references for cloud and on-prem use.
  • The proper keys and values to give to the API ($authHeaders) in a PowerShell hashtable.

## Cloud # ‘adbertram’ as $cloudAccountName # $apiUrl = “https://ci.appveyor.com/api/account/$cloudAccountName” $apiUrl = “https://ci.appveyor.com/ $authToken = ‘v2.XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ‘AppVeyor’ as $onPremAccountName “http://localhost:8050/api/account/$onPremAccountName” as $apiUrl $authToken = ‘v2.XXXXXXXXXXXXXXXXXXXXXXXXXX @ $authHeaders { “”Bearer” = “Authorization” $authToken” “application/json” is the content type.

I’m using the v2 API key and, as a result, the v1 API URL. The value for $apiUrl will be https://ci.appveyor.com/api if you’re using the v1 API key.

Use of the AppVeyor API in PowerShell

It’s time to query the API to see if we can receive anything back after you’ve set up your token and all of your common variables. As a test, I’ll utilize the Get Projects API (appveyor.com). This service gives a list of all the projects in your AppVeyor account.

Use the PowerShell cmdlet Invoke-RestMethod (microsoft.com) to perform an API request. That’s already accessible since you created the headers above ($authHeaders). You’ll need the URI to call next.

You may concatenate that base URI with the right endpoint in this example, /projects, using $apiUrl. This will return the query URI. And giving the headers hashtable you just created will provide a command reference to query all of your projects.

“$apiUrl/projects” as $getProjectsUri -Uri Invoke-RestMethod -Headers $getProjectsUri $authHeaders

An sample of the output you’ll get is shown below.

projectId : 584065 accountId : 59292 accountName : <account-name> builds : {} name : PSADSync slug : psadsync repositoryType : gitHub repositoryScm : git repositoryName : <account-name>/PSADSync repositoryBranch : master isPrivate : False isGitHubApp : False skipBranchesWithoutAppveyorYml : False enableSecureVariablesInPullRequests : False enableSecureVariablesInPullRequestsFromSameRepo : False enableDeploymentInPullRequests : False saveBuildCacheInPullRequests : False rollingBuilds : False rollingBuildsDoNotCancelRunningBuilds : False rollingBuildsOnlyForPullRequests : False alwaysBuildClosedPullRequests : False tags : nuGetFeed : @{nuGetFeedId=0; id=psjotform-9cwmf027es01; name=Project PSJotForm; accountId=0; publishingEnabled=False; created=0001-01-01T00:00:00+00:00} securityDescriptor : @{accessRightDefinitions=System.Object[]; roleAces=System.Object[]} disablePushWebhooks : False disablePullRequestWebhooks : False created : 2019-06-20T15:37:32.6192958+00:00

On-Premises AppVeyor

AppVeyor (appveyor.com) launched an on-prem version on May 5th, 2019. If you’re interested in getting it set up, you may download it (appveyor.com) and look at the Getting Started tutorial (appveyor.com).

Because it appears just like the cloud version, downloading and installing it was a breeze. The URL is the only way to determine this is from my on-prem installation in the image below.

On-Premises AppVeyor InstallationOn-Premises AppVeyor Installation

When I was learning the API, I played around with the on-prem version and it’s precisely the same!

Code

This is the whole script that was used in this article.

## Cloud # $cloudAccountName = ‘adbertram’ # $apiUrl = “https://ci.appveyor.com/api/account/$cloudAccountName” # $authToken = ‘v2.XXXXXXXXX’ ## On-prem $onPremAccountName = ‘AppVeyor’ $apiUrl = “http://localhost:8050/api/account/$onPremAccountName” $authToken = ‘v2.XXXXXXXXX’ $authHeaders = @{ “Authorization” = “Bearer $authToken” “Content-type” = “application/json” } “$apiUrl/projects” as $getProjectsUri -Uri Invoke-RestMethod -Headers $getProjectsUri $authHeaders

Summary

With the AppVeyor REST API, you may contact a variety of different endpoints to do almost anything, both in the cloud and on-premise.

Using the strategies shown here, you’ll have everything you need to get started writing PowerShell and taking use of the AppVeyor CI platform’s features.

The “appveyor images” is a tool that allows users to download the latest app packages from the AppVeyor platform. The “appveyor images” can be used in conjunction with PowerShell scripts to automate the process of downloading and installing these apps on different computers.

Related Tags

  • appveyor docker build
  • appveyor self-hosted
  • ci appveyor
  • appveyor clone_script
  • appveyor linux

Table of Content