Resolving Branch Specific Azure Template Links in Github Repositories--解决GitHub Azure REAEDME.MD的URL定向问题

转自《https://blogs.msdn.microsoft.com/mihansen/2017/12/13/resolving-branch-specific-azure-template-links-in-github-repositories/》

The Problem

The Azure Resource Manager is the primary tool or orchestrating deployments in Azure. Using Azure Resource Manager Templates it is possible to describe and manage complicated deployments. Templates are also used to share example deployments and reference architectures. The Azure Quickstart Templates is one of the largest collection of such templates. I will often share architectures and example deployments with colleagues and customers via GitHub repositories and one of the features that I use a lot is to add a deploy button directly in the README.md file in the repository. An example would be my Team Foundation Server (TFS) in Azure deployment template, which you can find on GitHub with an associated  blog post.  The relevant section of the README.md file renders as seen below on GitHub. People interested in using this deployment can simply click the button.

The Quickstart Templates use the same icons and links to enable easy deployment. An issue with those links (the buttons) is that the URL for referring the Azure portal to the template is fixed. In the specific example above, the link that produces the "Deploy to Azure" button is:


1

2

3

<a href="https://portal.azure.com/#create/Microsoft.Template/uri/https%3A%2F%2Fraw.githubusercontent.com%2Fhansenms%2Fiac%2Fmaster%2Fdevnet-tfs%2Fazuredeploy.json" target="_blank">

    <img src="http://azuredeploy.net/deploybutton.png"/>

</a>

Cosmetically this is pretty bad in itself. The lack of readability makes it hard to manage the links, but the real problems start when you are making edits to the repository and you push those edits into a different branch for testing. Since the link explicitly references the master branch, you then have to change the link, push it and when you merge back into master, you have to change it back. It is an awkward workflow, which is error prone. Another problem scenario is when somebody clones the repository to make changes. They then have to accommodate the new repository name (and/or branches) while editing and if they would like to submit a pull request to merge back, they then manually have to change links. This is a major hassle when working with the Azure Quickstart Templates repo and one reason why some edits never get merged back in.

The Solution - Transmogrify

To solve this problem, I have developed a small .NET core 2.0 Web App. It is tentatively called "transmogrify" and you can find the source code on GitHub. This web app is designed to be called from one of the README.md files in a GitHub repository. It will then determine which repository and branch it is being called from by analyzing the referring URI. It will then transform the requested template URL into a correct link for the Azure portal (Azure Commercial or Azure Government) and redirect to the portal. I have deployed this Web App to an Azure Web App at the address https://transmogrify.azurewebsites.net. By using this endpoint instead of the fixed URL, the link above is achieved with:


1

2

3

<a href="https://transmogrify.azurewebsites.net/devnet-tfs/azuredeploy.json" target="_blank">

    <img src="http://azuredeploy.net/deploybutton.png"/>

</a>

Or for Azure Government:


1

2

3

<a href="https://transmogrify.azurewebsites.net/devnet-tfs/azuredeploy.json?environment=gov" target="_blank">

<img src="https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/1-CONTRIBUTION-GUIDE/images/deploytoazuregov.png"

</a>

Not only is this link much simpler to read and write (without the URL encoded link to the template), it also works from any clone of the repository and any branch within the repository.

The code for the Web App is so simple, we can repeat the entire thing here without overwhelming anybody:


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

using System;

using System.Collections.Generic;

using System.Linq;

using System.Threading.Tasks;

using Microsoft.AspNetCore.Builder;

using Microsoft.AspNetCore.Hosting;

using Microsoft.AspNetCore.Http;

using Microsoft.Extensions.DependencyInjection;

namespace transmogrify

{

    public class Startup

    {

        // This method gets called by the runtime. Use this method to add services to the container.

        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940

        public void ConfigureServices(IServiceCollection services)

        {

        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)

        {

            if (env.IsDevelopment())

            {

                app.UseDeveloperExceptionPage();

            }

            app.Run(async (context) =>

            {

                if (context.Request.Headers["Referer"].ToString().Length == 0)

                {

                    await context.Response.WriteAsync("Please call from referring URI at github.com");

                    return;

                }

                Uri rUri = new Uri(context.Request.Headers["Referer"].ToString());

                string org = rUri.Segments[1].Replace("/","");

                string project = rUri.Segments[2].Replace("/", "");

                string branch = "master";

                if (rUri.Segments.Length > 3)

                {

                    branch = rUri.Segments[4].Replace("/", "");

                }

                string portal = "https://portal.azure.com";

                if (context.Request.Query["environment"] == "gov")

                {

                    portal = "https://portal.azure.us";

                }

                string templateUri = "https://raw.githubusercontent.com/" + org + "/" + project + "/" + branch + context.Request.Path;

                templateUri = System.Web.HttpUtility.UrlEncode(templateUri);

                string redirectUri = portal + "/#create/Microsoft.Template/uri/" + templateUri;

                if (context.Request.Path != "/") {

                    context.Response.Redirect(redirectUri);

                }

                await context.Response.WriteAsync("Please provide redirect path.");

            });

        }

    }

}

Related work

I have been made aware that there is a related project hosted at https://deploy.azure.com. I am not sure if it has been updated lately or if deployment to Azure Government is directly supported. A newer implementation of that project is the Slingshot project. Do check that one out too. In both cases, these projects aim to deploy without using the Azure portal which has matured considerably since the inception of those projects. The solution in this blog post leverages the Azure portal and simply redirects.

Conclusions

Fixed URLs with direct references to specific repositories and branches make it difficult to use good development practices when authoring Azure template libraries. In this blog, I have demonstrated that the problem can be solved by using a Web App to transform URLs based on repository and branch names and redirect to the appropriate Azure portal. The current version of the app only supports Azure Commercial and Azure US Government clouds, but that could easily be expanded. For long term reliability, the transmogrify app should probably be deployed and managed as a central service, but for now, feel free to use it in your templates.

Let me know if you have questions/comments.

原文地址:https://www.cnblogs.com/junjiany/p/9508894.html

时间: 2024-11-08 00:56:23

Resolving Branch Specific Azure Template Links in Github Repositories--解决GitHub Azure REAEDME.MD的URL定向问题的相关文章

Azure Stack技术深入浅出系列5:在Azure Stack上使用Web App PaaS服务及其背后原理窥探(开发案例)

App Service 是微软Azure的PaaS产品. 为任何平台或设备创建Web App PaaS服务和mobile App PaaS服务. 将应用与SaaS解决方案集成.与本地应用程序进行连接,以实现业务流程的自动化.在我们日常开发中,经常会使用Web App PaaS服务来承载企业的业务. 本文试图通过一个案例来分别详细说明Azure Web App业务的下列几大特点: 应用服务计划 多种语言和框架 持续集成和部署 连接数据库服务 可用性全局缩放 就在本文撰写过程中,Azure Stac

[SQL in Azure] Provisioning a SQL Server Virtual Machine on Azure

http://azure.microsoft.com/en-us/documentation/articles/virtual-machines-provision-sql-server/ Provisioning a SQL Server Virtual Machine on Azure The Azure virtual machine gallery includes several images that contain Microsoft SQL Server. You can sel

设置SQL Azure防火墙规则来允许SSMS管理SQL Azure

SQL Server Management Studio可以用来管理SQL Azure 但SQL Azure是云端的关系型数据库,也就是说任何一台连接到Internet的机器都能够连接到SQL Azure数据库.但是在某些情况下这是不安全的,作为用户来说,我只希望我自己企业内部能够连接到我创建的SQL Azure数据库:除我企业之外的其他人都无法连接到这台SQL Azure数据库.这个功能可以功过设置SQL Azure的防火墙规则来进行. 我这里设置任何地址都可以访问 设置SQL Azure防火

如何解决GitHub冲突&lt;一&gt;:GitHubDesktop同步你的分支

原文地址:https://help.github.com/desktop/guides/contributing/syncing-your-branch/ 当一个代码提交被推送到你的github项目时,你可以通过同步远程代码仓库的方式来使你本地的代码复件保持最新. 你必须让你的本地分支和远程仓库保持同步,只有这样,任何被增加到上游分支的新增代码提交才会被正确的更新. 1.更新你本地的上游分支 为了找到哪个分支是上游分支,请看对比图. ·上游分支会出现在最顶部 ·你的项目分支会出现在底部 (1)在

如何解决GitHub冲突&lt;二&gt;:使用命令行解决合并冲突

如何解决GitHub冲突<二>:使用命令行解决合并冲突 原文地址:https://help.github.com/desktop/guides/contributing/syncing-your-branch/ 你可以使用命令行和文本编辑器来解决"合并冲突". 合并冲突往往会发生在以下情况: (1)多个代码更改发生在同一行代码上 (2)一个提交删除了某一个文件而另一个提交尝试去编辑该文件 1.解决同行代码竞争引起的合并冲突 为了解决一个由更改同行代码引起的合并冲突,你必须决

转: 解决Github访问超慢问题

转自:http://zengrong.net/post/2092.htm 解决Github访问超慢问题 Github is so slowly. 这段时间访问 github 都非常慢,google了一下发现是github某个CDN被伟大的墙屏蔽所致. 出问题的应该是这个CDN: github.global.ssl.fastly.net,有图为证: 解决方法就是使用万能的host文件,将这个域名映射到它所在的ip. 访问 IPAddress.com 使用 IP Lookup 工具获得这个域名的ip

解决Github访问超慢问题[自己留档]

解决Github访问超慢问题 Github is so slowly. 这段时间访问 github 都非常慢,google了一下发现是github某个CDN被伟大的墙屏蔽所致. 出问题的应该是这个CDN: github.global.ssl.fastly.net,有图为证: 解决方法就是使用万能的host文件,将这个域名映射到它所在的ip. 访问 IPAddress.com 使用 IP Lookup 工具获得这个域名的ip地址(注意,该网站可能需要梯子): 也可以直接点击 这个地址 查看,结果如

【Github教程】史上最全github用法:github入门到精通

原文 http://www.eoeandroid.com/thread-274556-1-1.html [初识Github] 首先让我们大家一起喊一句"Hello Github".YEAH!就是这样. Git是一个分布式的版本号控制系统,最初由Linus Torvalds编写,用作Linux内核代码的管理.在推出后,Git在其他项目中也取得了非常大成功,尤其是在Ruby社区中.眼下,包含Rubinius和Merb在内的非常多知名项目都使用了Git.Git相同能够被诸如Capistran

如何解决Github上的 xxx.h文件not found

1. 此方法可基本解决github上的xxx.h文件缺失报红 以DTCoreText示例 一开始我直接Download ZIP,下载打开运行工程,本想来个一气呵成的,然后发现DTHTMLParser.h not found我擦,这不科学啊!这是为毛! 然后我试试换个方式下载,直接用git命令.打开终端,输入 git clone --recursive git项目url "指定路径",记住recursive不可缺 git clone --recursive https://github.