【问题标题】:How to set Release Definition artifacts using Azure DevOps REST API如何使用 Azure DevOps REST API 设置发布定义工件
【发布时间】:2020-08-03 05:09:03
【问题描述】:

我正在尝试为其中一个发布定义设置一长串工件。首先,我只添加一个工件。但是,即使这样也不会更新。以下是我到目前为止所做的。我究竟做错了什么?

$patUser = 'patUser'
$patToken = 'patToken'
$base64AuthInfo = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $($patUser), $($patToken))))
$header = @{Authorization=("Basic {0}" -f $base64AuthInfo)}

$relDefsUrl = 'https://vsrm.dev.azure.com/myOrg/myProj/_apis/release/definitions?api-version=5.1'

try {
    $result = Invoke-RestMethod -Uri $relDefsUrl -Method Get -ContentType "application/json" -Headers $header
    $relDefs = $result.value

    if ($relDefs.count -gt 0) {
        $relDefs | ForEach-Object {
            if ($_.name -eq 'ThisReleaseDefinition') {
                $url = $_.url + '?api-version=5.1'
                $relDefExpanded = Invoke-RestMethod -Uri $url -Method Get -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}

                $sourceId = "1234:1234"
                $type = "Build"
                $alias = "_thisCIBuild"
                $defRef = ""
                $isRetained = $false

                $artifacts = @{
                    sourceId = $sourceId
                    type = $type
                    alias = $alias
                    definitionReference = $defRef
                    isRetained = $isRetained
                }

                $relDefExpanded.artifacts = $artifacts
                $body = $relDefExpanded | ConvertTo-Json -Depth 100
                $body = [System.Text.Encoding]::UTF8.GetBytes($body)

                $updateResult = Invoke-RestMethod $url -Method Put -ContentType "application/json" -body $body -Headers $header
                Write-Host "The value of 'artifacts' is updated to" $updateResult.artifacts
            }
        }
    }
    else {
        throw ("Cannot continue because there is zero release definition.")
    }
}
catch {
    throw ("Failed to call ADO REST API: " + $_.Exception.Message)
}

【问题讨论】:

    标签: powershell azure-pipelines-release-pipeline azure-devops-rest-api


    【解决方案1】:

    您是否替换了脚本中的revision 参数?更新release定义需要设置当前的revision替换旧的,否则会提示:You are using an old copy of the release pipeline. Refresh your copy and try again.这会导致更新失败。

    在您的脚本中,您使用get 方法获取发布定义的响应正文,但revision 没有更新,仅修改了artifacts 部分。

    我在 Postman 中测试过,效果很好:

    示例请求正文:

    {
        "source": "userInterface",
        "revision": 46,
        "description": null,
        "createdBy": {
            "displayName": "hughl01",
            "url": "https://spsprodea1.vssps.visualstudio.com/xxx-8b1e-4b09-aaf7-291/_apis/Identities/xxx-a332-64e5-aea9",
            "_links": {
                "avatar": {
                    "href": "https://dev.azure.com/{org}/_apis/GraphProfile/MemberAvatars/msa.OWY3MjMwMTMtYTMzMi03NGU1LWFlYTktNmUzZW"
                }
            },
            "id": "9f723013-a332-64e5-aea9-xxf9d353",
            "uniqueName": "xx@hotmail.com",
            "imageUrl": "https://dev.azure.com/{org}/_apis/GraphProfile/MemberAvatars/msa.OWY3MjMwMTMtYTMzMi03NGU1L",
            "descriptor": "msa.OWY3MjMwMTMtY"
        },
        "createdOn": "2019-10-29T03:11:22.673Z",
        "modifiedBy": {
            "displayName": "hughl01",
            "url": "https://spsprodea1.vssps.visualstudio.com/Ac36xx7c99faca4/_apis/Identities/9f723013-a332-64e5-aea9-6e3",
            "_links": {
                "avatar": {
                    "href": "https://dev.azure.com/{org}/_apis/GraphProfile/MemberAvatars/msa.OWY3MjMwMTMtYTMzMi03NGU1LWFlYTk"
                }
            },
            "id": "9f723013-a332-64e5",
            "uniqueName": "xx@hotmail.com",
            "imageUrl": "https://dev.azure.com/{org}/_apis/GraphProfile/MemberAvatars/msa.OWY3MjMwMTMtYT",
            "descriptor": "msa.OWY3MjMwMTMtYTMzMi03NGU1LWFlYTk"
        },
        "modifiedOn": "2020-04-21T06:04:04.877Z",
        "isDeleted": false,
        
        "variables": {
            "test": {
                "value": "true"
            }
        },
        "variableGroups": [],
        "environments": [
            {
                "id": 5,
                "name": "Stage 1",
                "rank": 1,
                "owner": {
                    "displayName": "hughl01",
                 ......  
                
        ],
        "artifacts": [
            {
                "sourceId": "59b994e8-4a77-46a1-8b5d-6a25f10-9df2-4cc7-9f2f-944cf7280d1c",
                "type": "Git",
                "alias": "_111",
                "definitionReference": {
                    "branches": {
                        "id": "master",
                        "name": "master"
                    },
                    "checkoutNestedSubmodules": {
                        "id": "True",
                        "name": "Any nested submodules within"
                    },
                    "checkoutSubmodules": {
                        "id": "",
                        "name": ""
                    },
                    "defaultVersionSpecific": {
                        "id": "",
                        "name": ""
                    },
                    "defaultVersionType": {
                        "id": "latestFromBranchType",
                        "name": "Latest from the default branch"
                    },
                    "definition": {
                        "id": "46f31875-9df2-4cc7-9f2f-9441c",
                        "name": "111"
                    },
                    "fetchDepth": {
                        "id": "",
                        "name": ""
                    },
                    "gitLfsSupport": {
                        "id": "",
                        "name": ""
                    },
                    "project": {
                        "id": "59b994e8-4a77-46a1-8b5d-6a26",
                        "name": "xx"
                    }
                },
                "isRetained": false
            },
            {
                "sourceId": "59b994e8-4a77-46a1-8b5d-6a24b6:30",
                "type": "Build",
                "alias": "_dev02",
                "definitionReference": {
                    "artifactSourceDefinitionUrl": {
                        "id": "https://dev.azure.com/{org}/_permalink/_build/index?collectionId=0933e8b2-f504-4b7e-9ea59&projectId=59b994e8-4a77-4a1-8b5d-6a4b6&definitionId=30",
                        "name": ""
                    },
                    "defaultVersionBranch": {
                        "id": "",
                        "name": ""
                    },
                    "defaultVersionSpecific": {
                        "id": "2279",
                        "name": "2279"
                    },
                    "defaultVersionTags": {
                        "id": "",
                        "name": ""
                    },
                    "defaultVersionType": {
                        "id": "specificVersionType",
                        "name": "Specific version"
                    },
                    "definition": {
                        "id": "30",
                        "name": "dev02"
                    },
                    "definitions": {
                        "id": "",
                        "name": ""
                    },
                    "IsMultiDefinitionType": {
                        "id": "False",
                        "name": "False"
                    },
                    "project": {
                        "id": "59b994e8-4a77-4xxx10d24b6",
                        "name": "xx"
                    },
                    "repository": {
                        "id": "",
                        "name": ""
                    }
                },
                "isPrimary": true,
                "isRetained": false
            },
            {
                "type": "Build",
                "alias": "_UnitTest",
                "definitionReference": {
                    "defaultVersionType": {
                        "id": "latestType",
                        "name": "Latest"
                    },
                    "definition": {
                        "id": "27",
                        "name": "UnitTest"
                    },
                    "project": {
                        "id": "59b994e8-4a77xxx25f10d24b6",
                        "name": "xx"
                    }
                },
                "isPrimary": false,
                "isRetained": false
            }
        ],
        "triggers": [],
        "releaseNameFormat": "Release-$(rev:r)",
        "tags": [],
        "properties": {
            "DefinitionCreationSource": {
                "$type": "System.String",
                "$value": "ReleaseNew"
            },
            "IntegrateBoardsWorkItems": {
                "$type": "System.String",
                "$value": "True"
            },
            "IntegrateJiraWorkItems": {
                "$type": "System.String",
                "$value": "false"
            }
        },
        "id": 2,
        "name": "New release pipeline (1)",
        "path": "\\",
        "projectReference": null,
        "url": "https://vsrm.dev.azure.com/{org}/59b994e8-4a7xxd-6a25xx/_apis/Release/definitions/2",
        "_links": {
            "self": {
                "href": "https://vsrm.dev.azure.com/{org}/59b994e8-4xx1-xx0d24b6/_apis/Release/definitions/2"
            },
            "web": {
                "href": "https://dev.azure.com/{org}/59b994e8-xx5f10d24b6/_release?definitionId=2"
            }
        }
    }
    

    【讨论】:

    • 嗨@Hugh Lin,我替换了修订号,但不知何故我仍然收到错误。您是否必须更新 modifiedOn 日期时间或其他任何内容? { "$id": "1", "innerException": null, "message": "您使用的是发布管道的旧副本。刷新您的副本并重试。", "typeName": "Microsoft.VisualStudio。 Services.ReleaseManagement.Data.Exceptions.InvalidRequestException,Microsoft.VisualStudio.Services.ReleaseManagement2.Data”,“typeKey”:“InvalidRequestException”,“errorCode”:0,“eventId”:3000 }
    • 我没有更新modifiedOn参数,我只是从请求正文中删除了lastRelease部分,我在答案中附上了我的示例请求正文。
    • 我第一次做错了。我将修订号设置为当前所在的编号,并且现在可以使用。谢谢,我会将所有这些联系在一起并更新我拥有的 PowerShell 脚本。
    猜你喜欢
    • 2020-05-23
    • 2021-08-12
    • 1970-01-01
    • 1970-01-01
    • 2019-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-12
    相关资源
    最近更新 更多