Module/DevOps/New-BCSDevOpsPipeline.ps1

<#
.SYNOPSIS
  Setup DevOps Pipelines
 
.DESCRIPTION
  Create DevOps CI and CD Pipelines
 
.PARAMETER organisation
  DevOps Organisation Name, Default BrightComSolutions
  
.PARAMETER projectName
  DevOps Project Name
  
.PARAMETER repositoryName
  DevOps repository name where the pipeline yaml file is stored, if empty this will be the same as projectName
  
.PARAMETER pipelineFolderName
  Will save pipelines in a subfolder with this name
 
.PARAMETER pipelineType
  For QA pipeline specify CI, for Release pipeline specify CD.
 
.PARAMETER sourcePat
  DevOps Pwesonal Access Token.
 
.EXAMPLE
  New-BCSDevOpsPipeline -projectName "MyProjectName" -repositoryName "MyRepositoryName" -pipelineType CI -sourcePat (Get-BCSSecureString -InputString "MyDevOpsPat")
.NOTES
    Author: Mathias Stjernfelt
    Website: http://www.brightcom.se
#>


function New-BCSDevOpsPipeline {
  [CmdletBinding(SupportsShouldProcess)]
  Param (
    [Parameter(Mandatory = $false)]
    [string]$organisation = "BrightComSolutions",
    [Parameter(Mandatory = $true)]
    [string]$projectName,
    [Parameter(Mandatory = $false)]
    [string]$repositoryName,
    [Parameter(Mandatory = $false)]
    [switch]$pipelineFolderName,
    [Parameter(Mandatory = $true)]
    [ValidateSet('CI', 'CD')]
    [string] $pipelineType,
    [Parameter(Mandatory = $true)]
    [securestring]$sourcePat
  )

  try {
    $fullOrgUrl = "https://dev.azure.com/$organisation";

    $Pat = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($sourcePat))
    $Pat | az devops login --org $fullOrgUrl
    Write-host "Logged in to '$fullOrgUrl'"

    switch ($pipelineType) {
      "CI" { 
        $ymlPath = ".azureDevOps\pte-qa-current.yml" 
        $pipelineName = "$projectName - CI";
      }
      "CD" { 
        $ymlPath = ".azureDevOps\pte-prod-release.yml" 
        $pipelineName = "$projectName - Release";
      }
    }

    if ([string]::IsNullOrEmpty($repositoryName)) {
        $repositoryName = $projectName
    } 

    #Setups Pipelines
    if (-not [string]::IsNullOrEmpty($pipelineFolderName) -And ($pipelineFolderName.IsPresent -eq $true)) {
      $folder = az pipelines folder list --path $pipelineFolderName --org $fullOrgUrl --project $projectName

      if ($folder -eq '[]') {
        if ($PSCmdlet.ShouldProcess("")) {
          $folder = az pipelines folder create --path $projectName --project $projectName --org $fullOrgUrl  | convertfrom-json
          }
      }

      $result = az pipelines create --name $pipelineName --folder $pipelineFolderName --project $projectName --repository $repositoryName --repository-type tfsgit --branch main --org $fullOrgUrl --yml-path $ymlPath --skip-first-run true | convertfrom-json
    }
    else {
      if ($PSCmdlet.ShouldProcess("")) {
        $result = az pipelines create --name $pipelineName --project $projectName --repository $repositoryName --repository-type tfsgit --branch main --org $fullOrgUrl --yml-path $ymlPath --skip-first-run true | convertfrom-json
      }
    }
  }
  catch {
    throw "An error occured: $_.Exception";
  }
}

Export-ModuleMember -Function New-BCSDevOpsPipeline