Module/DevOps/New-BCSDevOpsPipeline.ps1

<#
.SYNOPSIS
  Update Azure Pipeline files from Template Project
 
.DESCRIPTION
  Update current.yml and build-settings.json from the project template repository and copy the files to your local project folder.
 
.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 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]$userPipelineSubfolder,
    [Parameter(Mandatory = $true)]
    [ValidateSet('CI', 'CD')]
    [string] $pipelineType,
    [Parameter(Mandatory = $true)]
    [string]$sourcePat
  )

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

    $sourcePat | 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)) {
      $folder = az pipelines folder list --path $pipelineFolderName --org $fullOrgUrl --project $projectName  --skip-first-run true

      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