Question:
Using a PowerShell script to customize your pipeline

In the world of automation and DevOps, pipelines have become a crucial component for building, testing, and deploying software applications in Azure. A pipeline is a sequence of tasks or stages that automate the process of software delivery. While most popular pipeline tools offer default functionality, there are times when you need to customize the pipeline to fit your specific requirements. In this blog, we'll explore how you can customize your pipeline using a PowerShell script, taking advantage of its powerful features and flexibility.


  1. Understanding PowerShell:

PowerShell script is a versatile scripting language and automation framework developed by Microsoft. It comes with a rich set of features and commands to interact with various components of Windows and other operating systems. Scripts written in PowerShell can be used to automate tasks, configure systems, and efficiently manage pipelines.


  1. Analyzing Your Pipeline Requirements:

Before diving into customization, it's important to identify the specific areas of your pipeline that require modification. This could include adding new stages, integrating with external tools, manipulating data, or implementing conditional logic. Assess your pipeline's current structure and determine the changes needed to align it with your goals.


Extending Pipelines with PowerShell:

There are various methods to extend Pipelines with PowerShell. However, you still have to run PowerShell scripts as inline scripts in your YAML files, either as a single line or multi-line


Run this command for single line code: 

- Powershell: "PowerShell single line"


Run this command for multi-line code: 


- Powershell: |

      This is script line 1

        This is script line 2


PowerShell can be integrated into existing pipelines or used to create entirely new ones. Here are some ways you can customize your pipeline using PowerShell:


  1. Customize pipeline using powershell.exe:

For those who are unaware, the legacy Windows PowerShell edition (v5.1-), which is based on the Windows-only.NET Framework (v4.8-), has the executable name powershell.exe. It only works with Windows agents and does not support execution across platforms.


Use the below code to customize your pipeline using powershell.exe


stages:

  - stage: Initialize

    displayName: 'Initialize Stage'

    pool: "windows-server-pool"

    jobs:

    - job: Install

      displayName: 'Install Dependencies (PowerShell)'  

      steps:

      - script: |

         @"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "Install-PackageProvider NuGet -Scope CurrentUser -Force"

        displayName: 'Powershell Install NuGet'

      - script: |

            @"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "Install-Module -Name SqlServer -AllowClobber -Scope CurrentUser -Force"

        displayName: 'Powershell Install SqlServer Module'



  1. Customize your pipeline using pwsh.exe:

This is a completely different way to Customize azure devops pipeline. In this method, we will use pwsh[.exe], which is an executable file name of PowerShell [Core] (v6+). 


stages:

- stage: CustomStage

  jobs:

  - job: CustomJob

    displayName: 'Custom Job'

    pool:

      vmImage: 'ubuntu-latest'

    steps:

    - task: PowerShell@2

      displayName: 'Run Custom PowerShell Script'

      inputs:

        targetType: 'filePath'

        filePath: 'path/to/your/script.ps1'

        pwsh: true



Pwsh[.exe] is the cross-platform edition of PowerShell built on .NET Core / .NET 5+. It requires PowerShell Core on the Linux agent or container. 


Run the below command to install pwsh in your Linux devices.


sudo apt-get install -y PowerShell

pwsh --version


  1. PowerShell Task

The pwsh keyword is a shorthand for PowerShell Core's PowerShell task. The PowerShell keyword is another PowerShell task abbreviation.


- powershell: |

     Install-Module -Name Az -AllowClobber -Scope CurrentUser -Force

     displayName: 'Install Azure Powershell'


# for PowerShell Core

steps:

- pwsh: ./my-script.ps1


# for Windows PowerShell

steps:

- powershell: .\my-script.ps1



This is another way to Customize your pipeline. In this method, we are using PowerShell and the pwsh shortcut to customize the pipeline.


- task: PowerShell@2

  displayName: 'Run Smoke Test'

  inputs:

    filePath: '$(Pipeline.Workspace)/DeployScripts/SmokeTest.ps1'

    arguments: '-url $(WebAppDeploy.AppServiceApplicationUrl)'

    failOnStderr: true

    pwsh: true


Best Practices for PowerShell Scripts in Azure Pipelines:

When using PowerShell scripts in Azure Pipelines, keep the following best practices in mind:


Version Control: To track changes and collaborate effectively with your team members, save your PowerShell scripts in a version control system such as Azure Repos or GitHub.


Parameterization: Use parameters to make your scripts more customizable and reusable. This allows you to easily modify the behavior of your scripts in response to changing environments or requirements.


Error Handling and Logging: Implement proper error handling and logging within your scripts in order to capture and report any problems that may occur during execution. This facilitates troubleshooting and the identification of potential issues.


Security Considerations: Ensure that your PowerShell scripts follow security best practices, such as using secure credentials and protecting sensitive information. Also, grant appropriate permissions to the Azure service principal or user running the pipeline.


Final thoughts: 

Using a PowerShell script to customize your pipeline empowers you to tailor your CI/CD process to meet your specific requirements. By leveraging PowerShell's extensive capabilities, you can install dependencies, incorporate code quality checks, perform custom testing, and execute unique deployment steps. Remember to document your customization process and ensure proper version control to maintain a reliable and reproducible pipeline. With PowerShell scripting, you can optimize your CI/CD pipeline and streamline your software delivery process.


Submit
0 Answers