1. Нам важно Ваше мнение!
    и обсуждайте вместе! Нажимайте "мне нравится" и рекомендуйте друзьям ;)
    Понравилось тема? Напишите в ней свой отзыв.
    Считаете что тема не достаточно полно раскрыта? Расскажите свою точку зрения!
    С Вашей помощью сделаем форум еще лучше!

Torrance : azure devops gated check in - Eduard Kabrinskiy

Тема в разделе 'Горячие новости для тех кто следит за собой', создана пользователем SHALOMEn, 19 май 2021.

  1. SHALOMEn

    SHALOMEn Member

    Эдуард Кабринский - Git artifact variable in vsts - Eduard Kabrinskiy


    <h1>Git artifact variable in vsts</h1>
    <p>[youtube]</p>
    Git artifact variable in vsts <a href="http://remmont.com">Today's big news</a> Git artifact variable in vsts
    <h1>Scott Hanselman</h1>
    <h2>Adding a git commit hash and Azure DevOps Build Number and Build ID to an ASP.NET website</h2>
    <p style="clear: both"><img style="float: left; margin: 0 10px 5px 0;" src="https://hanselmanblogcontent.azuree...mage_4ad31585-2276-45c6-9355-9e0a0f7b81c9.png" />A few months back I moved my CI/CD (Continuous Integration/Continuous Development) to Azure DevOps for free. You get 1800 build minutes a month FREE and I'm not even close to using it with three occasionally-updated sites building on it. Earlier this week I wrote about making a cleaner and more intentional azure-pipelines.yml for an ASP.NET Core Web App</p>
    <p>I was working/pairing with Damian today because <strong>I wanted to get my git commit hashes and build ids embedded into the actual website so I could see exactly what commit is in production</strong>.</p>
    <p>That's live on hanselminutes.com righ tnow and looks like this</p>
    <p>There's a few things here and it's all in my ASP.NET Web App's main layout page called _layout.cshtml. You can look all about ASP.NET Core 101, .NET and C# over at https://dot.net/videos if you'd like. They've lovely videos.</p>
    <p style="clear: both"> <img src="https://hanselmanblogcontent.azuree...mage_6785bcf2-3283-4f14-9e7b-80f169698c1b.png" /></p>
    <p>So let's take this footer apart, shall we?</p>
    <p>First, the obvious floating copyright year. Then a few credits that are hard coded.</p>
    <p>Next, a call to @System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription which gives me this string "<strong>.NET Core 3.1.2</strong>" Note that there was a time for a while where that Property was somewhat goofy, but no longer.</p>
    <p>I have two kinds of things I want to store along with my build artifact and output.</p>
    <p><ul>
    <li>I want the the Git commit hash of the code that was deployed. <ul>
    <li>Then I want to link it back to my source control. Note that my site is a private repo so you'll get a 404</li>
    </ul>
    </li>
    <li>I want the Build Number and the Build ID <ul>
    <li>This way I can link back to my Azure DevOps site</li>
    </ul>
    </li>
    </ul>
    </p>
    <h3>Adding a Git Commit Hash to your .NET assembly</h3>
    <p>There's lots of Assembly-level attributes you can add to your .NET assembly. One lovely one is AssemblyInformationalVersion and if you pass in SourceRevisionId on the dotnet build command line, it shows up in there automatically. Here's an example:</p>
    <p>From this command line:</p>
    <p>But where does that hash come from? Well, Azure Dev Ops includes it in an environment variable so you can make a YAML task like this:</p>
    <p>Sweet. That will put in VERSION+HASH, so we'll pull that out of a utility class Damian made like this (full class will be shown later)</p>
    <p>Displaying it is then trivial given the helper class we'll see in a minute. Note that hardcoded paths for my private repo. No need to make things complex.</p>
    <h3>Getting and Displaying Azure DevOps Build Number and Build ID</h3>
    <p>This one is a little more complex. We could theoretically tunnel this info into an assembly as well but it's just as easy, if not easier to put it into a text file and make sure it's part of the ContentRootPath (meaning it's just in the root of the website's folder).</p>
    <p><strong>To be clear, an option:</strong> There are ways to put this info in an Attribute but not without messing around with your csproj using some not-well-documented stuff. I like a clean csproj so I like this. Ideally there'd be another thing like SourceRevisionID to carry this metadata.</p>
    <p>You'd need to do something like this, and then pull it out with reflection. Meh.</p>
    <p>Those $(BuildNumber) and $(BuildId) dealies are build variables. Again, this csproj messing around is not for me.</p>
    <p>Instead, a simple text file, coming along for the ride.</p>
    <p>I'm cheating a little as I gave it the .json extension, only because JSON files are copying and brought along as "Content." If it didn't have an extension I would need to copy it manually, again, with my csproj:</p>
    <p>So, to be clear, two build variables inside a little text file. Then make a little helper class from Damian. Again, that file is in ContentRootPath and was zipped up and deployed with our web app.</p>
    <p>How do we access this class? Simple! It's a Singleton added in one line in Startup.cs's ConfigureServices():</p>
    <p>Then injected in one line in our _layout.cshtml!</p>
    <p>Then I can use it and it's easy. I could put an environment tag around it to make it only show up in staging:</p>
    <p>I could also wrap it all in a cache tag like this. Worst case for a few days/weeks at the start of a new year the Year is off.</p>
    <p>Thoughts on this technique?</p>
    <p><strong>Sponsor:</strong> This week's sponsor is. me! This blog and my podcast has been a labor of love for over 18 years. Your sponsorship pays my hosting bills for both AND allows me to buy gadgets to review AND the occasional taco. Join me!</p>
    <h4>About Scott</h4>
    <p>Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.</p>
    <h2>Git artifact variable in vsts</h2>

    <h3>Git artifact variable in vsts</h3>
    <p>[youtube]</p>
    Git artifact variable in vsts <a href="http://remmont.com">Breaking news headlines</a> Git artifact variable in vsts
    <h4>Git artifact variable in vsts</h4>
    A few months back I moved my CI/CD (Continuous Integration/Continuous ...
    <h5>Git artifact variable in vsts</h5>
    Git artifact variable in vsts <a href="http://remmont.com">Git artifact variable in vsts</a> Git artifact variable in vsts
    SOURCE: <h6>Git artifact variable in vsts</h6> <a href="https://dev-ops.engineer/">Git artifact variable in vsts</a> Git artifact variable in vsts
    #tags#[replace: -,-Git artifact variable in vsts] Git artifact variable in vsts#tags#

    Кабринский Эдуард
    current news

Поделиться этой страницей