Running an AppVeyor Build After Creating an Azure Web App Using Terraform

Written by Ken Dale

We’re in the process of scaffolding out our Microsoft Azure environments using Terraform. While Terraform does an excellent job creating Azure Web Apps, it still needs code to be deployed before the application can be used.

We currently manage our deployments using AppVeyor. Here’s how to get Terraform to invoke a fresh build of master after the resource is created:

resource "azurerm_app_service" "app-service" {
  name                    = "${var.name}"
  location                = "${var.location}"
  resource_group_name     = "${var.resource_group_name}"
  app_service_plan_id     = "${var.service_plan_id}"

  provisioner "local-exec" {
    # Invoke AppVeyor build
    command = <<EOF
$headers = @{
  "Authorization" = "Bearer ${var.appveyor_api_key}"
  "Content-Type" = "application/json"
}

$body = @{
  "accountName" = "YOUR_ACCOUNT_NAME_HERE" # Or use ${var.repository_user} or similar
  "projectSlug" = "${var.repository_name}"
  "branch" = "master"
} | ConvertTo-Json

Invoke-RestMethod -Uri https://ci.appveyor.com/api/builds -Method Post -Headers $headers -Body $body
EOF
    interpreter = ["PowerShell", "-Command"]
  }
}

The PowerShell script requires the following (there are other Terraform variables in the above, too):

  • ${var.repository_name} (just the repository name, not organization/repository-name)
  • ${var.appveyor_api_key} (can be obtained at https://ci.appveyor.com)

The local-exec block runs on the machine running terraform apply, so you’ll also need PowerShell to be available.

Enjoy using this script or modifying it to suit your needs!

Published January 10, 2019 by

undefined avatar
Ken Dale Senior Application Developer (Former)

Suggested Reading