How to Schedule Recurring Builds in App Center

799 Views Asked by At

I have an Android app in App Center and I'd like to schedule nightly builds.

Is there any way to do this?

1

There are 1 best solutions below

1
On BEST ANSWER

Answer

Today, App Center Build doesn't yet allow us to schedule recurring builds.

Luckily, App Center has a complete suite of APIs we can leverage to schedule an Azure Timer Function (essentially a cron job in the cloud) to trigger a build every night.

For a complete solution, see the UITestSampleApp.Functions project in this repo: https://github.com/brminnick/UITestSampleApp

Walkthrough

For a complete walkthrough, follow this post: https://www.codetraveler.io/2019/06/06/scheduling-app-center-builds/

This shows how to gather the required metadata and API Token from App Center, then how to build an Azure Timer Function that will trigger a build via the App Center APIs.

1. Get App Center Metadata

First, get the App Center metadata we'll need to Post to the App Center API

App Center Build Metadata

2. Generate App Center API Token

Then generate an App Center API Token

Generate App Center API Token

3. Create an Azure Timer Function

This Azure Timer Function uses the cron schedule 0 0 9 * * * to trigger every day at 0900 UTC.

using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;

const string owner = "[Your App Owner]"; //change to your app owner
const string appName = "[Your App Name]"; // change to your app name
const string branch = "[Your Repo's Branch]"; //change to your repo's branch

readonly static Lazy<HttpClient> clientHolder = new Lazy<HttpClient>(() =>
{
    var client = new HttpClient();
    client.DefaultRequestHeaders.Add("X-API-Token", Environment.GetEnvironmentVariable("AppCenterApiToken"));

    return client;
});

static HttpClient Client => clientHolder.Value;

[FunctionName("AppCenterScheduledBuildFunction")]
public static async Task Run([TimerTrigger("0 0 9 * * *")]TimerInfo myTimer, ILogger log)
{    
    var httpContent = new StringContent("{ \"debug\": true }", System.Text.Encoding.UTF8, "application/json");

    var result = await Client.PostAsync($"https://api.appcenter.ms/v0.1/apps/{owner}/{appName}/branches/{branch}/builds", httpContent);

    result.EnsureSuccessStatusCode();
}

4. Add App Center API Token to Azure Function

In the Azure Function Application Settings, add the App Center API Token, generated in step 2, using AppCenterApiToken for its name.

Azure Function Application Settings

Azure Function API Token