Public/Start-DeploymentStage.ps1

Function Start-DeploymentStage {
    <#
.SYNOPSIS
Starts a deployment between stages in a Microsoft Fabric deployment pipeline.
 
.DESCRIPTION
The Start-DeploymentStage function initiates a deployment between two stages in a Microsoft Fabric deployment pipeline.
It locates the specified deployment pipeline by name, identifies the source and target stages,
and initiates the deployment process between them. After deploying, it retrieves the list of
deployment items in the target stage and returns combined information about the deployment.
 
.PARAMETER deploymentPipelineName
The display name of the deployment pipeline to use.
 
.PARAMETER sourceStageName
The display name of the source stage where the deployment will come from.
 
.PARAMETER targetStageName
The display name of the target stage where items will be deployed to.
 
.PARAMETER note
An optional note to include with the deployment operation.
 
.EXAMPLE
Start-DeploymentStage -deploymentPipelineName "Main Pipeline" -sourceStageName "Development" -targetStageName "Test"
 
This example starts a deployment from the "Development" stage to the "Test" stage in the "Main Pipeline".
 
.EXAMPLE
Start-DeploymentStage -deploymentPipelineName "Finance Pipeline" -sourceStageName "Test" -targetStageName "Production" -note "Monthly financial reports update"
 
This example deploys from "Test" to "Production" with a deployment note.
 
.OUTPUTS
Returns a custom object containing both the deployment result and the list of deployment items.
 
.NOTES
Requires the Invoke-FabricApiRequest function to be available and configured for accessing the Microsoft Fabric API.
#>

    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory)]
        [string]$deploymentPipelineName,

        [Parameter(Mandatory)]
        [string]$sourceStageName,

        [Parameter(Mandatory)]
        [string]$targetStageName,

        [string]$note

    )
      
    
    $result = Invoke-FabricApiRequest -Uri "deploymentPipelines" -Method Get

    
    $deploymentpipeline = $result | Where-Object { $_.displayName -ieq $deploymentPipelineName }

    if (!$deploymentpipeline) {
        throw "Cannot find Deployment pipeline '$deploymentPipelineName'"
    }
    
    $result = Invoke-FabricApiRequest -Uri "deploymentPipelines/$($deploymentpipeline.id)/stages" -Method Get
    $sourceStage = $result | Where-Object { $_.displayName -ieq $sourceStageName }
    $targetStage = $result | Where-Object { $_.displayName -ieq $targetStageName }

    if (!$sourceStage) {
        throw "Cannot find source stage '$sourceStageName' in deployment pipeline '$deploymentPipelineName'"
    }
    
    if (!$targetStage) {
        throw "Cannot find target stage '$targetStageName' in deployment pipeline '$deploymentPipelineName'"
    }

    $deployBody = @{
        sourceStageId = $sourceStage.id
        targetStageId = $targetStage.id
        note = $note
    } | ConvertTo-Json

    $deployResult = Invoke-FabricApiRequest -Uri "deploymentPipelines/$($deploymentpipeline.id)/deploy" -Method Post -Body $deployBody


    $listDeploymentItems = Invoke-FabricApiRequest -Uri "deploymentPipelines/$($deploymentpipeline.id)/stages/$($targetStage.id)/items" -Method Get

    $combinedResult = [PSCustomObject]@{
        DeploymentResult = $deployResult
        DeploymentItems = $listDeploymentItems
    }
    Write-Output $combinedResult

}