如何在 App Center 中安排重复构建

How to Schedule Recurring Builds in App Center

我在 App Center 有一个 Android 应用程序,我想安排夜间构建。

有什么办法吗?

回答

今天,应用中心构建 doesn't yet allow us to schedule recurring builds

幸运的是,App Center 在云端有一个 complete suite of APIs we can leverage to schedule an Azure Timer Function (essentially a cron job)来触发每晚构建。

有关完整的解决方案,请参阅此存储库中的 UITestSampleApp.Functions 项目:https://github.com/brminnick/UITestSampleApp

演练

要获得完整的演练,请遵循此 post:https://www.codetraveler.io/2019/06/06/scheduling-app-center-builds/

这展示了如何从 App Center 收集所需的元数据和 API 令牌,然后如何构建将通过 App Center APIs 触发构建的 Azure 计时器函数。

1。获取应用中心元数据

首先,获取我们需要的 App Center 元数据 Post 到 App Center API

2。生成 App Center API 令牌

然后生成一个应用中心API令牌

3。创建 Azure 计时器函数

此 Azure 计时器函数使用 cron 计划 0 0 9 * * * 在每天 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。将 App Center API 令牌添加到 Azure Function

在 Azure Function Application Settings 中,添加在步骤 2 中生成的 App Center API Token,其名称使用 AppCenterApiToken