如何使用Rest API在Azure Devops中自动执行流水线操作

时间:2019-05-31 17:59:23

标签: azure azure-devops azure-devops-rest-api

我希望能够自动化创建构建过程,并以天蓝色devops发布。我确实知道rest api的存在。但是他们会帮助实现流程自动化,并且可以在node.js中完成吗?

2 个答案:

答案 0 :(得分:0)

是的。 Azure Devops确实具有API。我没有使用node.js的经验。

Docu for Azure DevOps REST API:

https://docs.microsoft.com/en-us/rest/api/azure/devops/?view=azure-devops-rest-5.0

您可以在这里找到node.js库:

https://github.com/microsoft/azure-devops-node-api

答案 1 :(得分:0)

您能否提供有关所需自动化过程的更多详细信息?

如果您还想创建定义,那么我还将介绍YAML。将来,将会有更多的更新,尤其是针对发行版定义的更新: https://docs.microsoft.com/en-us/azure/devops/pipelines/yaml-schema?view=azure-devops&tabs=schema

我为Azure DevOps Api实现了Powershell(但没有使用Node.js)包装。以下是一些用于创建新版本的代码段:

设置连接

<#
    .SYNOPSIS
    Sets the environment parameter for the current session so that the commandlets can access Azure DevOps.
#>
function Set-AzureDevOpsEnvironment {
    Param(
        <# The account name of the Azure DevOps tenant to access. If not set, this is set to "vanstelematics" #>
        [Parameter(Mandatory=$true)]
        $AzureDevOpsAccountName,

        <# The project name of Azure DevOps to work with. If not set, this is set to "ScaledPilotPlatform" #>
        [Parameter(Mandatory=$true)]
        $AzureDevOpsProjectName,

        <# The PAT to access the Azure DevOps REST API. If not set, the function tries to read the PAT from a
        textfile called "AzureDevOpsPat.user" either in the current working directory or in the profile
        directory of the current user. The textfile must contain only that PAT as plain string. #>
        [Parameter(Mandatory=$false)]
        $AzureDevOpsPat
    ) 

    if ($PSCmdlet.MyInvocation.BoundParameters["Verbose"].IsPresent) {
        $Script:Verbose = $true
    } else {
        $Script:Verbose = $false
    }

    if (!$AzureDevOpsPat) {
        $paths = @("AzureDevOpsPat.user", (JOin-Path $env:USERPROFILE "AzureDevOpsPat.user"))

        foreach ($path in $paths) {
            if (Test-Path $path -ErrorAction SilentlyContinue) {
                $AzureDevOpsPat = Get-Content $path
                break
            }
        }

        if (!$AzureDevOpsPat) {
            Write-Host "AzureDevOpsPat is empty and file AzureDevOpsPat.user not found." -ForegroundColor Red
            return  
        }
    }

    Write-Host "The Azure DevOps project '$($AzureDevOpsProjectName)' inside the Azure DevOps account '$($AzureDevOpsAccountName)' will be used."
    $Script:AzureDevOpsAccountName = $AzureDevOpsAccountName
    $Script:AzureDevOpsProjectName = $AzureDevOpsProjectName
    $Script:AzureDevOpsPat = $AzureDevOpsPat
}

REST CALLER

function Call-AzureDevOpsApi($Url, $JsonBody, [ValidateSet("GET", "DELETE", "POST", "PUT", "PATCH")]$Method, $ContentType) {
    if ($Script:Verbose) {
        Write-Host "Calling $($Method) $($Url)"
    }

    if (!$ContentType) {
        $ContentType = "application/json"
    }

    $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes(("{0}:{1}" -f "", $Script:AzureDevOpsPat)))

    $parameters = @{
        Headers = @{Authorization = ("Basic {0}" -f $base64AuthInfo)};
        Method = $Method;
        Uri = $Url;
    }

    if ($Method -in @("POST", "PUT", "PATCH")) {
        if (!$JsonBody) {
            Write-Error "A JsonBody is required for method $($Method)."
            return
        }

        $JsonBodyUtf8 = [Text.Encoding]::UTF8.GetBytes($JsonBody)
        $parameters["Body"] = $JsonBodyUtf8
        $parameters["ContentType"] = $ContentType
    } 

    $result = Invoke-RestMethod @parameters
    return $result
}

function Call-AzureDevOpsApiPost($Url, $JsonBody, [Parameter(Mandatory=$False)][ValidateSet("application/json", "application/json-patch+json")]$ContentType) {
    return Call-AzureDevOpsApi -Url $Url -JsonBody $JsonBody -ContentType $ContentType -Method POST
}

function Call-AzureDevOpsApiPut($Url, $JsonBody, [Parameter(Mandatory=$False)][ValidateSet("application/json", "application/json-patch+json")]$ContentType) {
    return Call-AzureDevOpsApi -Url $Url -JsonBody $JsonBody -Method PUT
}

function Call-AzureDevOpsApiPatch($Url, $JsonBody, [Parameter(Mandatory=$False)][ValidateSet("application/json", "application/json-patch+json")]$ContentType) {
    return Call-AzureDevOpsApi -Url $Url -JsonBody $JsonBody -Method PATCH
}

function Call-AzureDevOpsApiGet($Url, [Parameter(Mandatory=$False)][ValidateSet("application/json", "application/json-patch+json")]$ContentType) {
    return Call-AzureDevOpsApi -Url $Url -Method GET
}

function Call-AzureDevOpsApiDelete($Url, [ValidateSet("application/json", "application/json-patch+json")]$ContentType) {
    return Call-AzureDevOpsApi -Url $Url -Method DELETE
}

新发布

<#
    .SYNOPSIS
    Creates a new release for a given Release Definition and artifact (p.e. build)
#>
function New-Release {
    Param(
        <# The id of the release definition to create the release for. #>
        [Parameter(Mandatory=$true)]
        $ReleaseDefinition, 

        <# The alias of the artifact of the release definition to create the release for #>
        [Parameter(Mandatory=$true)]
        $ArtifactAlias, 

        <# The version of the artifact (p.e. the id of the build)#>
        [Parameter(Mandatory=$true)]
        $ArtifactVersion,

        <# The description/name of the release #>
        [Parameter(Mandatory=$true)]
        $Description
    )

    $url = "https://vsrm.dev.azure.com/$($Script:AzureDevOpsAccountName)/$($Script:AzureDevOpsProjectName)/_apis/release/releases?api-version=4.1-preview.6"

    $releaseData = @{
        "definitionId"       = $ReleaseDefinition.id;
        "description"        = $Description;
        "artifacts"          = @(
            @{
                "alias"             = $ArtifactAlias;
                "instanceReference" = $ArtifactVersion
            }
        );
        "isDraft"            = $false;
        "reason"             = "none";
        "manualEnvironments" = $ReleaseDefinition.environments | select -ExpandProperty name
    }

    $result = Call-AzureDevOpsApiPost -Url $url -JsonBody ($releaseData | ConvertTo-Json -Depth 100)
    return $result
}

希望这给出了如何使用它的想法。