DevOpsHandling/Publish-DevOpsArtifactsToFeed.ps1

<#
 .Synopsis
  Publishes artifacts to DevOps feed
 .Description
  Publishes artifacts to DevOps feed
 .Parameter devOpsOrganization
  Name of the DevOps organization containing the feed
  .Parameter devOpsFeed
  Name of the feed
  .Parameter devOpsArtifact
  Name of the artifact. If empty, the name will be built from the app name
  .Parameter SourcePath
  Path to the current project
  .Parameter version
  Version number to be published. If the version is empty, it will be read from the app.json
  .Example
  Publish-DevOpsArtifactsToFeed -devOpsOrganization "test" -devOpsFeed "feed" -devOpsArtifact "artifact" -sourcePath "C:\Install" -version "1.0.0"
#>

function Publish-DevOpsArtifactsToFeed {
    Param(
        [Parameter(Mandatory=$false)]
        [string]$devOpsOrganization = "",
        [Parameter(Mandatory=$false)]
        [string]$devOpsFeed = "",
        [Parameter(Mandatory=$false)]
        [string]$devOpsArtifact = "",
        [Parameter(Mandatory=$true)]
        [string]$sourcePath,
        [Parameter(Mandatory=$false)]
        [string]$version = ""
    )

    if ($devOpsOrganization -eq "") {
        $devOpsOrganization = Get-EnvironmentKeyValue -KeyName "Organization"
    }
    if ($devOpsArtifact -eq "") {
        $devOpsArtifact = (((Get-AppKeyValue -KeyName 'name') -replace ' ', '_').ToLower())
    }
    if ($version -eq "") {
        $version = ((Get-AppKeyValue -KeyName 'version') -replace '(.*)\.(.*)','$1')
    }
    if ($devOpsFeed -eq "") {
        $dependencies = Get-EnvironmentKeyValue -KeyName "dependencies"
        if ($null -ne $dependencies) {
            if ($dependencies -ne "") {
                $devOpsFeed = $dependencies[0].devOpsFeed
            }
        }
    }

    if ($devOpsFeed -eq "") {
        $devOpsFeed = (Get-EnvironmentKeyValue -KeyName "devOpsFeed")
    }

    # publishing app
    Write-Output "Publishing $($devOpsArtifact)_app, version $version, in feed $devOpsFeed, organization $devOpsOrganization"

    foreach ($app in (Get-ChildItem (Join-Path $sourcePath "app"))) {
        $output = az artifacts universal publish --organization ("https://dev.azure.com/" + $devOpsOrganization) --feed "$devOpsFeed" --name "$($devOpsArtifact)_app" --version "$version" --path $app.FullName
        if (!$output) {
            throw "Could not publish package $($devOpsArtifact)_app to $devOpsOrganization feed $devOpsFeed"
        }
        else {
            Write-Output "Published $($devOpsArtifact)_app version $($version)"
        }
    }

    # publishing runtime app
    Write-Output "Publishing $($devOpsArtifact)_runtime, version $version, in feed $devOpsFeed, organization $devOpsOrganization"

    foreach ($app in (Get-ChildItem (Join-Path $sourcePath "runtimeapp"))) {
        $output = az artifacts universal publish --organization ("https://dev.azure.com/" + $devOpsOrganization) --feed "$devOpsFeed" --name "$($devOpsArtifact)_runtime" --version "$version" --path $app.FullName
        if (!$output) {
            throw "Could not publish package $($devOpsArtifact)_runtime to $devOpsOrganization feed $devOpsFeed"
        }
        else {
            Write-Output "Published $($devOpsArtifact)_runtimeapp version $($version)"
        }
    }

    # publishing tests app
    Write-Output "Publishing $($devOpsArtifact)_tests, version $version, in feed $devOpsFeed, organization $devOpsOrganization"

    foreach ($app in (Get-ChildItem (Join-Path $sourcePath "testapp"))) {
        $output = az artifacts universal publish --organization ("https://dev.azure.com/" + $devOpsOrganization) --feed "$devOpsFeed" --name "$($devOpsArtifact)_tests" --version "$version" --path $app.FullName
        if (!$output) {
            throw "Could not publish package $($devOpsArtifact)_tests to $devOpsOrganization feed $devOpsFeed"
        }
        else {
            Write-Output "Published $($devOpsArtifact)_tests version $($version)"
        }
    }

    # publishing base objects
    if (Test-Path (Join-Path $sourcePAth "baseobjects")) {
        Write-Output "Publishing $($devOpsArtifact)_baseobjects, version $version, in feed $devOpsFeed, organization $devOpsOrganization"

        foreach ($app in (Get-ChildItem (Join-Path $sourcePath "baseobjects"))) {
            $output = az artifacts universal publish --organization ("https://dev.azure.com/" + $devOpsOrganization) --feed "$devOpsFeed" --name "$($devOpsArtifact)_baseobjects" --version "$version" --path $app.FullName
            if (!$output) {
                throw "Could not publish package $($devOpsArtifact)_baseobjects to $devOpsOrganization feed $devOpsFeed"
            }
            else {
                Write-Output "Published $($devOpsArtifact)_baseobjects version $($version)"
            }
        }
    }
}