如何使用 Az Cli 将所有策略从一个分支复制到 Azure Devops 上的另一个分支

问题描述 投票:0回答:1

我需要自动从另一个分支创建新分支,并从源分支复制所有策略。这将发生在 ADO 中的 YAML 管道内,我通过 az cli 实现它。

此刻我陷入了困境,我能够从指定的存储库和分支检索策略列表:

REPOID=$(az repos list --query "[?name == '<nameOfRepo>'].id" --output tsv)  
POLICIES=$(az repos policy list --branch develop --repository-id $REPOID)

但是从现在起,我无法从 文档命令列表 中找到可以帮助我执行此操作的命令,因为仅支持特定策略创建(创建构建策略、创建审阅者策略...)。

我找到了this文章,但它仅解释了构建策略的创建。

bash azure-devops azure-pipelines command-line-interface cicd
1个回答
0
投票

您可以使用以下 PowerShell 脚本:

[CmdletBinding()]

param(
    [Parameter(Mandatory=$true)]
    [string] $AzureDevOpsPAT,
    [Parameter(Mandatory=$true)]
    [string] $OrganizationName,
    [Parameter(Mandatory=$true)]
    [string] $teamProjectName,
    [Parameter(Mandatory=$true)]
    [string] $repositoryName,
    [Parameter(Mandatory=$true)]
    [string] $fromBranch,
    [Parameter(Mandatory=$true)]
    [string] $toBranch
)


$ErrorActionPreference = 'Stop';

$User="";

$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $User,$AzureDevOpsPAT)));
$header = @{Authorization=("Basic {0}" -f $base64AuthInfo)};

# Get repository
$Url = 'https://dev.azure.com/'+ $OrganizationName + '/' + $teamProjectName + '/_apis/git/repositories/' + $repositoryName +'?api-version=5.1'
$repository = Invoke-RestMethod -Uri $Url -Method Get -ContentType application/json -Headers $header

$Url = 'https://dev.azure.com/'+ $OrganizationName + '/' + $teamProjectName + 
            '/_apis/git/policy/configurations?repositoryId=' + $repository.id + '&refName=refs/heads/' + $fromBranch + '&api-version=5.1-preview.1'

# get policies
$policies = Invoke-RestMethod -Uri $Url -Method Get -ContentType application/json -Headers $header

$Url = 'https://dev.azure.com/'+ $OrganizationName + '/' + $teamProjectName + '/_apis/policy/configurations?api-version=5.1';


foreach($policy in $policies.value)
{
    if ($policy.type.id -eq '0517f88d-4ec5-4343-9d26-9930ebd53069') 
    {
        continue; # skipping GitRepositorySettingsPolicyName
    }

    # remove properties from policy so it can be applied to target branch
    $props = $policy.PSObject.Properties
    $props.remove('createdBy');
    $props.remove('createdDate');
    $props.remove('revision');
    $props.remove('id');
    $props.remove('url');

    $policy._links.PSObject.Properties.Remove('self')

    # set target branch

    if (($null -ne $policy.settings.scope[0].refname))
    {
        $policy.settings.scope[0].refName = 'refs/heads/' + $toBranch;
    }
    if (($null -ne $policy.settings.searchBranches))
    {
        $policy.settings.searchBranches[0] = 'refs/heads/' + $toBranch;
    }

    # create policy in target branch
    
    $body = $policy | ConvertTo-Json -Depth 10

    $policyCreateResponse = Invoke-RestMethod -Uri $Url -Method Post -Body $body -ContentType application/json -Headers $header
    $policyCreateResponse
}

用途:

.\PolicyCopy.ps1 -AzureDevOpsPAT 'yourPAT' -OrganizationName 'test' -teamProjectName 'test' -repositoryName 'test' -fromBranch 'master' -toBranch 'feature'

来源:这里

© www.soinside.com 2019 - 2024. All rights reserved.