【问题标题】:How do I write an azure function to trigger a azure devops pipeline? Is this even possible?如何编写 azure 函数来触发 azure devops 管道?这甚至可能吗?
【发布时间】:2019-08-15 17:22:02
【问题描述】:

我正在尝试设置一个可以触发 Azure devops 构建管道的 Azure 函数。但我不确定如何进行,甚至是否有可能这样做。

我已经准备好可以使用的构建管道,但无法使用 Azure devops 已经提供的常规触发器。我无法透露原因,但我需要使用 Azure 函数来触发它。任何有关这方面的帮助都会非常有帮助。

谢谢。

【问题讨论】:

    标签: azure azure-devops azure-functions


    【解决方案1】:

    您可以简单地使用 Azure DevOps 的 REST API 并从您的函数中调用它们。见这里:API for automating Azure DevOps Pipelines?

    【讨论】:

      【解决方案2】:

      如何进行以及是否可以这样做。

      当然,它可以通过 Azure 功能触发 Azure Devops 管道。

      要实现这一点,您还将使用 Visual Studio,最好使用 2017 或更高版本,并安装 .Net Core SDK 和 Develop Azure Functions using Visual Studio

      1. 在您创建的 Azure Function App 中,打开 HttpTrigger1,然后 点击获取函数网址。这是一个关键点,所以最好保存 它在txt中。因为作为这次执行,Azure Function 只是充当 切换代理或机制。
      2. 在 Visual Studio 中创建一个 Azure Function 项目。在模板中, 选择 HttpTrigger 模板,Azure Functions v1 (.NET 框架) 从框架下拉菜单中。

      3. 打开.cs文件并在其中输入以下脚本:

              using System;
              using System.Linq;
              using System.Net;
              using System.Net.Http;
              using System.Threading.Tasks;
              using Microsoft.Azure.WebJobs;
              using Microsoft.Azure.WebJobs.Extensions.Http;
              using Microsoft.Azure.WebJobs.Host;
              namespace PartsUnlimited.AzureFunction
             {
             public static class Function1
             {
                 [FunctionName("HttpTriggerCSharp1")]
                 public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route =
        null)]HttpRequestMessage req, TraceWriter log)
                 {
                     var userIdKey = req.GetQueryNameValuePairs().FirstOrDefault(q =>
        string.Equals(q.Key, "UserId", StringComparison.OrdinalIgnoreCase));
                     var userId = string.IsNullOrEmpty(userIdKey.Value) ? int.MaxValue : Convert.ToInt64(userIdKey.Value);
                     var url = $"https://<<APIAppServiceUrl>>/api/{(userId > 10 ? "v1" : "v2")}/specials/GetSpecialsByUserId?id={userId}";
                     using (HttpClient httpClient = new HttpClient())
                     {
                         return await httpClient.GetAsync(url);
                     }
                 }
             }
            }
        
      4. &lt;&lt;APIAppServiceUrl&gt;&gt; 替换为您的 AzureWebsite.net URL。

      5. 转到xxxxWebsite->Controllers->StoreController.cs,替换url 第 46 行 中的变量与您在步骤 1 中复制的 Function url

      6. 单击 Commit and Push All 将更改推送到 Azure Devops 存储库。

      7. 创建构建管道并启用 CI。此外,创建发布管道,添加 deloy Azure App 任务并启用 CD。

      这就是您需要创建的所有步骤。有一个blog是我们Azure DevOps Labs写的,里面有详细的步骤,可以参考一下。

      【讨论】:

        【解决方案3】:

        可以从 azure 函数轻松调用 REST API。

        您几乎可以部署整个基础架构,也可以仅基于使用 azure DevOps 的函数调用部署代码。

        更多信息请参考:https://docs.microsoft.com/en-us/rest/api/azure/devops/

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-04-13
          • 1970-01-01
          • 2020-04-24
          • 2022-07-01
          • 1970-01-01
          • 2021-01-13
          • 1970-01-01
          相关资源
          最近更新 更多