<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by John Lokerse on Medium]]></title>
        <description><![CDATA[Stories by John Lokerse on Medium]]></description>
        <link>https://medium.com/@johnlokersedev?source=rss-67e87a81e455------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*6UQAKf9vlVSylGY_IsP9mQ@2x.jpeg</url>
            <title>Stories by John Lokerse on Medium</title>
            <link>https://medium.com/@johnlokersedev?source=rss-67e87a81e455------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 12:30:11 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@johnlokersedev/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Subnetting in Azure Bicep]]></title>
            <link>https://medium.com/azure-bicep/subnetting-in-azure-bicep-94b4e41bc23d?source=rss-67e87a81e455------2</link>
            <guid isPermaLink="false">https://medium.com/p/94b4e41bc23d</guid>
            <category><![CDATA[azure-bicep]]></category>
            <category><![CDATA[infrastructure-as-code]]></category>
            <category><![CDATA[azure]]></category>
            <category><![CDATA[networking]]></category>
            <dc:creator><![CDATA[John Lokerse]]></dc:creator>
            <pubDate>Mon, 21 Aug 2023 08:16:10 GMT</pubDate>
            <atom:updated>2026-03-27T21:47:34.675Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*DfbWPKaO8t-CAVu1" /></figure><p>In this blog, I am writing about Bicep CIDR functions and will show a practical example on how to use this. With these CIDR functions subnetting in Bicep has been made easier. We can now create subnets programmatically.</p><h3>What is CIDR?</h3><p>CIDR stands for Classless Inter-Domain Routing and it is a method of allocating IP addresses and routing internet protocol packets. CIDR uses prefix a notation to represent IP address blocks. In this notation, an IP address is followed by a forward slash and a number that indicates the length of the network prefix. For example, 192.168.1.0/24 represents a block of IP addresses where the first 24 bits are the network prefix, and the remaining 8 bits are available for host addresses.</p><h3>Bicep CIDR functions</h3><p>There are three CIDR functions available:</p><p><strong>parseCidr()</strong></p><p>Let’s take a look at the first function: parseCidr(). This function parses an IP address range to get various properties of the specified address range. This function expects one parameter, namely: parseCidr(<strong>network</strong>). The network parameter is the IP address range to parse.</p><p>For example, to get information about the following IP address range 192.168.1.0/24 call the parseCidr function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2bc28439d71fb9bd1a3ead667c56a759/href">https://medium.com/media/2bc28439d71fb9bd1a3ead667c56a759/href</a></iframe><p>Outputs:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d0378f4c3560889b1f9d20804afeafec/href">https://medium.com/media/d0378f4c3560889b1f9d20804afeafec/href</a></iframe><p><strong>cidrSubnet()</strong></p><p>The cidrSubnet() function splits the specified IP address range into subnets with a new CIDR value and returns the IP address range of the subnet with the specified index. This function expects three parameters: cidrSubnet(<strong>network</strong>, <strong>cidr</strong>, <strong>subnetIndex</strong>).</p><p>The first parameter is the IP address space to split, the second parameter is the CIDR of the subnet and the last parameter is used to output the desired subnet.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7fb76f62bf0c1a9c0f07bfcf8bba0efd/href">https://medium.com/media/7fb76f62bf0c1a9c0f07bfcf8bba0efd/href</a></iframe><p>Outputs:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cf3c8fc7ec0c3fd1cf7a97987bd9f5ec/href">https://medium.com/media/cf3c8fc7ec0c3fd1cf7a97987bd9f5ec/href</a></iframe><p>The output is the first subnet with CIDR /25 created from the address space. If we changed the subnetIndex from 0 to 1 it would have outputted the second subnet 192.168.1.128/25.</p><p><strong>cidrHost()</strong></p><p>The cidrHost() function calculates the usable IP addresses of the given address space. of the host with the specified index on the specified IP address range in CIDR notation. This function expects two parameters: cidrHost(<strong>network</strong>, <strong>hostIndex</strong>).</p><p>The first parameter is the IP address space and the second parameter is to output the desired host. For example, if cidrHost is called with host index 0 using address space 192.168.1.0/24 the output will be 192.168.1.1. If host index 1 is used the output will be 192.168.1.2 and so on.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/761b291ab6c24ebc1618dec248ecb012/href">https://medium.com/media/761b291ab6c24ebc1618dec248ecb012/href</a></iframe><p>Outputs:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1c92436bfcff163f6466720599284e0b/href">https://medium.com/media/1c92436bfcff163f6466720599284e0b/href</a></iframe><p>This function can be valuable, for example, allowing the creation of programmatic whitelists for specific IP address ranges within a network security group or firewall. Notably, it excludes reserved IP addresses from its results.</p><h3>Subnetting a virtual network with cidrSubnet()</h3><p>Let’s put the function cidrSubnet() to the test. We aim to create a virtual network with address space 192.168.1.0/24. As we have observed in the parseCidr range output, the /24 prefix can accommodate 256 IP addresses.</p><p>To begin, we need to determine the number of subnets required. In this scenario, we’ll be working with 2 subnets, each capable of accommodating 128 addresses, denoted by a CIDR notation of /25. You can use parseCidr to calculate the amount hosts for the subnet, or you can calculate it manually using the following equation: 2^(32-25) = 2^7, which equals 128. There are various methods to calculate subnets, but this is the approach I find most straightforward.</p><p>Now let’s create the Bicep file to deploy the virtual network and two subnets with the cidrSubnet() function.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/44ee2d1cfd0b990ab176efb50b35dd0f/href">https://medium.com/media/44ee2d1cfd0b990ab176efb50b35dd0f/href</a></iframe><p>Let’s walk through the provided Bicep code. First, I’ve defined several parameters: parAddressSpace, parSubnetCidr, and parAmountOfSubnets. These parameters serve as inputs that can be customised when deploying the virtual network. For instance, parAddressSpace allows you to specify the preferred IP address space in CIDR notation, while parSubnetCidr lets you define the subnet CIDR prefix length, and parAmountOfSubnets sets the number of subnets to be created.</p><p>Following the parameter definitions, I’ve created a variable called varSubnetCalculations. This variable utilises the map function to generate an array of objects, each representing the properties of a subnet expected by the virtual network resource. This approach enhances code readability and simplifies the configuration of multiple subnets within the virtual network. It iterates through the specified number of subnets and computes the corresponding address prefixes using the cidrSubnet function. These subnet property objects are then used in the subnets property of the resVirtualNetwork resource, facilitating the creation of the virtual network with the desired subnets.</p><p>For clarity, this is the output of varSubnetCalculations:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/64f58babdd2c08117c3172baee3a918b/href">https://medium.com/media/64f58babdd2c08117c3172baee3a918b/href</a></iframe><p>If you want to know more about the map() function or other lambda’s check out my “ <a href="https://johnlokerse.dev/2022/09/26/azure-bicep-lambda-expressions">Azure Bicep Lambda Expressions</a> “ blog post.</p><p>When we deploy the Bicep code to Azure it creates the virtual network including the subnets:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1018/0*x9a7lUQmm6vA9h2L" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*fjqky802iZh_AX3F" /></figure><h3>Conclusion</h3><p>This is how to leverage the Bicep CIDR functions to create subnets for your Azure networks. In this blog, I used IPv4 addresses, but the CIDR functions also work with IPv6. Subnetting has been made easier with these functions, but you will still need to think about the CIDR for the subnets.</p><p><em>Originally published at </em><a href="https://johnlokerse.dev/2023/08/21/subnetting-in-azure-bicep/"><em>http://johnlokerse.dev</em></a><em> on August 21, 2023.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=94b4e41bc23d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/azure-bicep/subnetting-in-azure-bicep-94b4e41bc23d">Subnetting in Azure Bicep</a> was originally published in <a href="https://medium.com/azure-bicep">Azure Bicep</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The “Awesome Azure DevOps” community repository]]></title>
            <link>https://medium.com/@johnlokersedev/the-awesome-azure-devops-community-repository-10f605e2262e?source=rss-67e87a81e455------2</link>
            <guid isPermaLink="false">https://medium.com/p/10f605e2262e</guid>
            <category><![CDATA[github]]></category>
            <category><![CDATA[azure]]></category>
            <category><![CDATA[microsoft]]></category>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[azure-devops]]></category>
            <dc:creator><![CDATA[John Lokerse]]></dc:creator>
            <pubDate>Sun, 16 Jul 2023 12:57:50 GMT</pubDate>
            <atom:updated>2023-07-16T12:58:00.170Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/560/1*vgUp27gR1OBXu8-9ofCM0g.png" /></figure><p>Are you looking for a curated list of Azure DevOps blogs, videos, or podcast content? Then this post is for you. I have bundled helpful Microsoft official and community-created Azure DevOps content and listed them on GitHub.</p><h3>Repository</h3><p>The goal of this repository is to have a central place with awesome Azure DevOps content. So if you have an interesting blog, video, or podcast that you want to share with the community, open a pull request and add your content to the list! This repository is community driven!</p><p>Have a look at the <strong>awesome-azure-devops</strong> repository:</p><p><a href="https://github.com/johnlokerse/awesome-azure-devops">GitHub - johnlokerse/awesome-azure-devops: An awesome list of Azure DevOps content created by Microsoft or the Community!</a></p><p>Feel free to share this repository, give feedback or add content. If you want to stay up-to-date with the progress of the repository please give the repository a star.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=10f605e2262e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Run PowerShell scripts with Azure Bicep]]></title>
            <link>https://medium.com/@johnlokersedev/run-powershell-scripts-with-azure-bicep-f36b26926021?source=rss-67e87a81e455------2</link>
            <guid isPermaLink="false">https://medium.com/p/f36b26926021</guid>
            <category><![CDATA[azure]]></category>
            <category><![CDATA[bicep]]></category>
            <category><![CDATA[azure-bicep]]></category>
            <category><![CDATA[azure-devops]]></category>
            <category><![CDATA[powershell]]></category>
            <dc:creator><![CDATA[John Lokerse]]></dc:creator>
            <pubDate>Thu, 19 Jan 2023 17:26:03 GMT</pubDate>
            <atom:updated>2023-01-19T17:26:03.947Z</atom:updated>
            <content:encoded><![CDATA[<p>Did you know you can run Azure PowerShell or CLI scripts with Bicep? With Microsoft.Resources/deploymentScripts you can execute scripts in a Bicep deployment. In addition, you can work with the outputs of the script that you ran. This opens loads of automation possibilities and flexibility.</p><p><strong>What are Deployment Scripts?</strong></p><p>The deploymentScript resource can run PowerShell or Bash scripts that are run inside a temporary container. The given scripts run during an ARM deployment, so besides infrastructure deployments, it is possible to call internal or external APIs or gather resource information before deploying infrastructure.</p><p>DeploymentScript uses User-Assigned Managed Identities, or Connect-AzAccount/az login. This identity connects to Azure and runs the scripts inside the container. A managed identity can get an RBAC (or Azure AD) role, this helps to scope what a deployment scripts can control, deploy or delete.</p><p>Once the deployment script is finished, it is possible to get the outputs of a script. These outputs can be used to populate properties in other resource deployments.</p><p><strong>How does it work?</strong></p><p>When the resource type Microsoft.Resources/deploymentScripts is deployed the Azure Resource Manager deploys the following resources in the given resource group:</p><ul><li>Azure Container Instance — is used for running scripts.</li><li>Azure Storage Account — is used to store script outputs in a file share.</li><li>Deployment Script — is used for debugging or logging.</li></ul><p>The Container Instance and the Storage Account are temporary resources. The ARM engine will automatically clean up these resources once the deployment script has been completed. Billing is minimal when you let the ARM engine clean up the resources. It is also possible to use existing container instances and storage accounts however, these do not clean up automatically and have to be managed by yourself.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/679/0*onUYmme1SZYilURU.png" /><figcaption>How the DeploymentScript is triggered</figcaption></figure><p><strong>How does this work in practice?</strong></p><p>To demonstrate the working of deployment scripts, we want to deploy the following resources:</p><ul><li>User-Assigned Managed Identity</li><li>Deployment Script (and indirectly also a container instance and storage account)</li><li>Azure Key Vault</li></ul><p>The goal of this Bicep deployment is to create an Azure Key Vault, along with a service principal that is granted access to the policy of the key vault.</p><pre>param parLocation string = &#39;westeurope&#39;<br><br>var varTenantId = tenant().tenantId<br><br>resource resManagedIdentity &#39;Microsoft.ManagedIdentity/userAssignedIdentities@2022-01-31-preview&#39; = {<br>  name: &#39;john-managed-identity&#39;<br>  location: parLocation<br>}<br><br>resource resDeploymentScript &#39;Microsoft.Resources/deploymentScripts@2020-10-01&#39; = {<br>  name: &#39;create-spn-for-kv&#39;<br>  location: parLocation<br>  kind: &#39;AzurePowerShell&#39;<br>  identity: {<br>    type: &#39;UserAssigned&#39;<br>    userAssignedIdentities: {<br>      &#39;${resManagedIdentity.id}&#39; : {}<br>    }<br>  }<br>  properties: {<br>    azPowerShellVersion: &#39;9.0&#39;<br>    retentionInterval: &#39;P1D&#39;<br>    scriptContent: &#39;&#39;&#39;<br>      $spnAppId = New-AzADServicePrincipal -DisplayName &quot;my-keyvault-spn&quot; | Select-Object -ExpandProperty AppId<br>      $DeploymentScriptOutputs = @{}<br>      $DeploymentScriptOutputs[&#39;appId&#39;] = $spnAppId<br>    &#39;&#39;&#39;<br>  }<br>}<br><br>resource resKeyVault &#39;Microsoft.KeyVault/vaults@2019-09-01&#39; = {<br>  name: &#39;my-ds-key-vault&#39;<br>  location: parLocation<br>  properties: {<br>    enabledForDeployment: true<br>    enabledForTemplateDeployment: true<br>    enabledForDiskEncryption: true<br>    tenantId: varTenantId<br>    accessPolicies: [<br>      {<br>        tenantId: varTenantId<br>        objectId: resDeploymentScript.properties.outputs.appId<br>        permissions: {<br>          keys: [<br>            &#39;get&#39;<br>          ]<br>          secrets: [<br>            &#39;list&#39;<br>            &#39;get&#39;<br>          ]<br>        }<br>      }<br>    ]<br>    sku: {<br>      name: &#39;standard&#39;<br>      family: &#39;A&#39;<br>    }<br>  }<br>}</pre><p><strong>Deployment Scripts resource</strong></p><pre>resource resDeploymentScript &#39;Microsoft.Resources/deploymentScripts@2020-10-01&#39; = {<br>  name: &#39;create-spn-for-kv&#39;<br>  location: parLocation<br>  kind: &#39;AzurePowerShell&#39;<br>  identity: {<br>    type: &#39;UserAssigned&#39;<br>    userAssignedIdentities: {<br>      &#39;${resManagedIdentity.id}&#39; : {}<br>    }<br>  }<br>  properties: {<br>    azPowerShellVersion: &#39;9.0&#39;<br>    retentionInterval: &#39;P1D&#39;<br>    scriptContent: &#39;&#39;&#39;<br>      $spnAppId = New-AzADServicePrincipal -DisplayName &quot;my-keyvault-spn&quot; | Select-Object -ExpandProperty AppId<br>      $DeploymentScriptOutputs = @{}<br>      $DeploymentScriptOutputs[&#39;appId&#39;] = $spnAppId<br>    &#39;&#39;&#39;<br>  }<br>}</pre><p>Let’s take a look at the deployment script resource Microsoft.Resources/deploymentScripts. In this Bicep resource several properties are important:</p><ul><li><strong>kind</strong></li></ul><p>This specifies the type of the script. Only Azure PowerShell or Azure CLI scripts are supported, but not limited to Azure PowerShell or Azure CLI commands. Values are: AzurePowerShell or AzureCLI.</p><ul><li><strong>identity</strong></li></ul><p>Not required, but an identity is needed in some way. The identity property uses a user-assigned managed identity that is created in an earlier stage in the Bicep deployment resManagedIdentity.id. Currently, only user-assigned managed identities can be used. If no identity is given, the deployment script has to call Connect-AzAccount in PowerShell or az login in AzureCLI.</p><ul><li><strong>azPowerShellVersion / azCliVersion</strong></li></ul><p>Azure PowerShell or Azure CLI version to be used when running script. Not all versions are supported, but Microsoft shared a list of supported versions for Azure PowerShell <a href="https://mcr.microsoft.com/v2/azuredeploymentscripts-powershell/tags/list">here</a> or Azure CLI <a href="https://mcr.microsoft.com/v2/azure-cli/tags/list">here</a>.</p><ul><li><strong>retentionInterval</strong></li></ul><p>The retention interval is used to determine how long the script results should be stored. By default, the results are erased after running the script. In the example, I used P1D, which stands for “one day”. The script refers to the deployment script resource.</p><ul><li><strong>scriptContent</strong></li></ul><p>The script to run. This can be a multi-line &#39;&#39;&#39; inline script or the loadTextContent() Bicep function can be used load a script from a file.</p><p><strong>The output of the example</strong></p><p>If we run the demonstration example a few resources are being created:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/953/0*EtUo6ffVQRlQCOkM" /></figure><p>The resources defined in the example are key vault, managed identity and deployment scripts. The storage account and container instances are created by the deployment script and the names are randomly generated.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/879/0*zmfg0BhFt0EvyJgp" /></figure><p>In the deployment script resource we can see that the container instance and storage account are linked and that they are removed after the provision state is succeeded.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*uK_LSn4TodyCwuZI" /></figure><p>The script created a service principal and used the appId as an output. This output can be found in the outputs of the deployment script.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*RmXxSjueNacu5lSc" /></figure><p>The deployment script output is used in the access policies of the key vault. The created service principal has permission to the created key vault.</p><p><strong>Deployment Script outputs</strong></p><p>In the script contents, outputs can be defined that can be utilized elsewhere in the Bicep template. The definition of deployment script outputs is different between Azure PowerShell and Azure CLI:</p><p><strong>Azure PowerShell</strong></p><pre><br>$DeploymentScriptOutputs = @{}<br>$DeploymentScriptOutputs[&#39;appId&#39;] = $spnAppId</pre><p>For Azure PowerShell, you have to define a hashtable with the name DeploymentScriptOutputs.</p><p><strong>Azure CLI</strong></p><pre>$spnAppId &gt; $AZ_SCRIPTS_OUTPUT_PATH</pre><p>For Azure CLI, you have to output the value to the $AZ_SCRIPTS_OUTPUT_PATH variable.</p><p><strong>Use the output in Bicep</strong></p><pre>resource resDeploymentScript &#39;Microsoft.Resources/deploymentScripts@2020-10-01&#39; = {<br>  ...<br>}<br><br>output outScriptOutput string = resDeploymentScript.properties.outputs.appId</pre><p>To use the outputs generated by the Azure PowerShell or CLI script refer to the symbolic name of the resource. This resource contains the property .properties.outputs. which has the value of $spnAppId stored in appId.</p><p><strong>Deployment Script input parameters</strong></p><p>As you can see, we are creating a service principal with a predefined name. We can make this name adjustable by populating it with parameters supplied by Bicep.</p><pre>param parSpnName string = &#39;my-input-parameter&#39;<br><br>### hidden managed identity resource ###<br><br>resource resDeploymentScript &#39;Microsoft.Resources/deploymentScripts@2020-10-01&#39; = {<br>  name: &#39;create-spn-for-kv&#39;<br>  location: parLocation<br>  kind: &#39;AzurePowerShell&#39;<br>  identity: {<br>    type: &#39;UserAssigned&#39;<br>    userAssignedIdentities: {<br>      &#39;${resManagedIdentity.id}&#39; : {}<br>    }<br>  }<br>  properties: {<br>    azPowerShellVersion: &#39;9.0&#39;<br>    retentionInterval: &#39;P1D&#39;<br>    arguments: &#39;-SpnName ${parSpnName}&#39;<br>    scriptContent: &#39;&#39;&#39;<br>      param (<br>        [string] $SpnName<br>      )<br>      $spnAppId = New-AzADServicePrincipal -DisplayName $SpnName | Select-Object -ExpandProperty AppId<br>      $DeploymentScriptOutputs = @{}<br>      $DeploymentScriptOutputs[&#39;appId&#39;] = $spnAppId<br>    &#39;&#39;&#39;<br>  }<br>}</pre><p>In the above Bicep we added the parameter parSpnName which is used as input for the PowerShell script. Also, the script is modified to support parameters. We added the parameter block that contains $SpnName to supply the script with the given service principal name. To pass through the value of parSpnName the arguments property was added. This property corresponds with the parameter required by the PowerShell script.</p><p>If we check the Azure Portal we see our deployment script resource create-spn-for-kv. Under content and inputs we see the property arguments. We can see that the dynamic value (${parSpnName}) has been changed to the given parameter value. In this case the hardcoded value my-input-parameter.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1023/0*SXiuhTlCjhg9v8eP" /></figure><p><strong>Conclusion</strong></p><p>This is how you can use deployment scripts in your Bicep templates. It offers new possibilities and flexibility regarding infrastructure automation. In this blog post we talked about creating a service principal, but it can also be used to look up IP-address data, for creating certificates and saving it in a key vault and so on.</p><p>If you want to have a deployment script Bicep snippet check out my GitHub repository: <a href="https://github.com/johnlokerse/bicep-snippets">bicep-snippets</a>.</p><p><a href="https://www.johnlokerse.dev/">Azure Cloud | John Lokerse</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f36b26926021" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Validating YAML using Azure DevOps or CLI]]></title>
            <link>https://medium.com/@johnlokersedev/validating-yaml-using-azure-devops-or-cli-556b228a3ef1?source=rss-67e87a81e455------2</link>
            <guid isPermaLink="false">https://medium.com/p/556b228a3ef1</guid>
            <category><![CDATA[azure]]></category>
            <category><![CDATA[microsoft]]></category>
            <category><![CDATA[yaml]]></category>
            <category><![CDATA[azure-devops]]></category>
            <category><![CDATA[powershell]]></category>
            <dc:creator><![CDATA[John Lokerse]]></dc:creator>
            <pubDate>Fri, 02 Dec 2022 09:25:06 GMT</pubDate>
            <atom:updated>2022-12-04T11:51:52.633Z</atom:updated>
            <content:encoded><![CDATA[<p>Working with YAML can be a struggle. It does not matter how easy or complex the task is, an error is easily made. Errors might be an illegal character in a job name or incorrect formatting.</p><p>I work with YAML frequently to create or maintain CI/CD pipelines and for this post, I want to share how I validate my YAML files before these are added to the main branch. The validation can be done through the Azure DevOps web interface or via the CLI. The CLI can be a useful tool to automate this process in build validation.</p><h3><strong>Validation using the GUI</strong></h3><p>This is the YAML input we are going to work with:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/30e99fea522683987d67c24dd4e80873/href">https://medium.com/media/30e99fea522683987d67c24dd4e80873/href</a></iframe><p>This input contains an error in the job name. It contains an <a href="https://docs.microsoft.com/en-us/azure/devops/pipelines/yaml-schema?view=azure-devops&amp;tabs=schema%2Cparameter-schema#job">illegal character</a>, which is the “space” character. Job names only allow [A-Z, a-z, 0-9, and underscores] characters.</p><p>Azure DevOps has a built-in validator that checks your YAML file for errors and invalidations. This validator also works with YAML templates.</p><p>To validate a pipeline follow these steps:</p><ol><li>Go to the pipeline you want to validate.</li><li>Press on the <strong><em>Edit </em></strong>button on the top right.</li><li>On the edit-page click on the <strong><em>three dots</em></strong> on the top right.</li><li>(Optional) When using <a href="https://trunkbaseddevelopment.com/">trunk based development</a> you can choose the feature branch you are working in.</li><li>In this menu, click on <strong><em>validate </em></strong>and a message will appear with either OK if the pipeline is validated correctly or an error message if the pipeline is invalid.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*JuHFxpJnWQcFoIBc" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1021/0*6OboeVKLOFyWRH6B" /></figure><p>The example input we are working with is invalid so after doing step 5 we get an error message containing the location of the error:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*o6mrBxSUxr_iZ8tK" /></figure><p>If we replace the spaces and the dash (-) with underscores (_) in the job name the pipeline will throw a success message:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/220/0*PdWB1PIk0bn7hnq_" /></figure><h3>Validation using the CLI</h3><p>There is a way to validate through the CLI. This is useful because it is the perfect way to automate these validations. The YAML must be validated against an <em>existing pipeline</em>. Microsoft provides a “hidden and undocumented” API to validate YAML via a REST call:</p><blockquote><a href="https://dev.azure.com/$OrganizationName/$ProjectName/_apis/pipelines/$PipelineId/runs?api-version=5.1-preview">https://dev.azure.com/<strong>$OrganizationName</strong>/<strong>$ProjectName</strong>/_apis/pipelines/<strong>$PipelineId</strong>/runs?api-version=5.1-preview</a></blockquote><p>This REST call requires some given segments to find your Azure Pipeline:</p><ul><li>OrganizationName (e.g. Contoso)</li><li>ProjectName (e.g. ContosoTeamProject)</li><li>PipelineId, also known as BuildDefinitionId (e.g. 150)</li></ul><p>The BuildDefinitionId can be found in the URL of the pipeline: <a href="https://dev.azure.com/contoso/ContosoTeamProject/_build?definitionId=150">https://dev.azure.com/<strong>contoso</strong>/<strong>ContosoTeamProject</strong>/_build?definitionId=<strong>150</strong></a></p><p>This REST call is a <strong>POST</strong>, so it also requires a <em>body</em>:</p><pre>$Body = @{<br>  &quot;PreviewRun&quot;   = &quot;true&quot;<br>  &quot;YamlOverride&quot; = &lt;INSERT_YAML&gt;<br>}</pre><p>The success output is a bit vague because there is no OK statement. The output of the error message is clear and similar to the message shown in the GUI.</p><p>When the output is validated correctly this will be the output:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1023/0*f2iIRrwEzsrSB-Ic" /></figure><p>The full script with the YAML input provided from the last section:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/63d3f3c19906f679f15f175a3a001003/href">https://medium.com/media/63d3f3c19906f679f15f175a3a001003/href</a></iframe><h3>Conclusion</h3><p>This is how you can validate your YAML files using Azure DevOps and the CLI. This feature is useful when you are working with large YAML files or when you are using templated YAML files.</p><p>I have written a PowerShell script that makes it easier to validate your YAML file. Also, I added an Azure Pipeline you can use as build validation. Check it out at my GitHub repo: <a href="https://github.com/johnlokerse/ado-yaml-validation">https://github.com/johnlokerse/ado-yaml-validation</a></p><p><a href="https://johnlokerse.dev/">Azure Cloud | John Lokerse</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=556b228a3ef1" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>