Resolving Branch specific Azure Template Links in GitHub repositories--resolve GitHub Azure Reaedme. The URL orientation problem of MD

Source: Internet
Author: User

Transferred from the 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 is also used to share example deployments and reference architectures. The Azure Quickstart Templates is one of the largest collection of such Templates. I'll often share architectures and example deployments with colleagues and customers via GitHub repositories and one of The features that I use a lot are to add a deploy button directly in the Readme.md file in the repository. An example would is my Team Foundation Server (TFS) in Azure Deployment template, which you can find on GitHub with a  Ociated 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, 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:

123 < 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" > &NBSP;&NBSP;&NBSP;&NBSP; < img src = "Http://azuredeploy.net/deploybutton.png" /> </ a >

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

The Solution-transmogrify

To solve the 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 was designed to being called from one of the readme.md files in a GitHub repository. It would then determine which repository and branch it was being called from by analyzing the referring URI. It'll then transform the requested template URLs into a correct link for the Azure Portal (Azure commercial or Azure Gove rnment) and redirect to the portal. I have deployed this web app for Azure Web app at the Address https://transmogrify.azurewebsites.net. By using this endpoint instead of the fixed URL, the link above was achieved with:

123 <ahref="https://transmogrify.azurewebsites.net/devnet-tfs/azuredeploy.json" target="_blank">    <imgsrc="http://azuredeploy.net/deploybutton.png"/></a>

Or for Azure Government:

123 < a href = target = "_blank" > < img src = "https://raw.githubusercontent.com/azure/azure-quickstart-templates/master/1- Contribution-guide/images/deploytoazuregov.png " </A>

not = This link much simpler to read and write (without the URL encoded link to the template), it also works from an Y clone of the repository and any branch within the repository.

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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 6667 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 are a related project hosted at https://deploy.azure.com. I am not sure if it had been updated lately or if deployment to Azure government is directly supported. A Newer implementation of this project is the Slingshot project. Do check this one out too. In both cases, these projects aim to deploy without using the Azure portal which have matured considerably since the incept Ion 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 practice s when authoring Azure template libraries. In this blog, I has demonstrated that the problem can is 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 is supports Azure commercial and azure US government clouds, but that could easily is exp ANDed. For long term reliability, the Transmogrify app should probably is deployed and managed as a central service, Feel free to use it in your templates.

Let me know if you have questions/comments.

Resolving Branch specific Azure Template Links in GitHub repositories--resolve GitHub Azure Reaedme. The URL orientation problem of MD

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.