简体   繁体   English

构建管道中 AzurePowershell 任务中的 IF 条件

[英]IF condition in AzurePowershell task in build pipeline

I have an azure build pipeline task that publishes/creats wiki from a repository.我有一个 azure 构建管道任务,它从存储库发布/创建 wiki。 The code is the following代码如下


    - task: AzurePowerShell@5
      inputs:
          azureSubscription: '$(service_connection)'
          ScriptType: 'InlineScript'
          Inline: |
            $url = 'https://dev.azure.com/data-platform/uc_test/_apis/wiki/wikis?api-version=6.0'
            
            $body= @'
            {
              "version": {
                "version": "main"
              },
              "type": "codeWiki",
              "name": "wiki",
              "projectId": "c800c392-67aa-4892-a070-7284435bda9b",
              "repositoryId": "2avd6061-b265-417f-8b55-81cc65kf90b6",
              "mappedPath": "/"
            }

            '@
            
            $response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Bearer $(System.AccessToken)"} -Method Post -Body $body -ContentType application/json 
            echo $response 


          azurePowerShellVersion: 'LatestVersion'

On the first run of this build pipeline, it successfully publishes/creates wiki.在此构建管道的第一次运行中,它成功发布/创建了 wiki。 But in the next run, it throws the following error但是在下次运行时,它会抛出以下错误

 {"$id":"1","innerException":null,"message":"Wiki already exists with
     | name
     | 'wiki'.","typeName":"Microsoft.TeamFoundation.Wiki.Server.WikiAlreadyExistsException, Microsoft.TeamFoundation.Wiki.Server","typeKey":"WikiAlreadyExistsException","errorCode":0,"eventId":3000}

How can I tackle this issue?我该如何解决这个问题? I was thinking if there any way to implement a if else condition under AzurePowerShell@5 task - if wiki doesn't exist- create wiki, else skip the script.我在想是否有任何方法可以在 AzurePowerShell@5 任务下实现 if else 条件 - 如果 wiki 不存在 - 创建 wiki,否则跳过脚本。

Wiki already exists with Wiki 已经存在

The cause of the issue is that wiki names are unique.问题的原因是 wiki 名称是唯一的。 When you run successfully for the first time, the wiki name already exists.第一次运行成功时,wiki名称已经存在。 You need to change the wiki name to let it work again.您需要更改 wiki 名称才能使其再次运行。

I was thinking if there any way to implement a if else condition under AzurePowerShell@5 task - if wiki doesn't exist- create wiki, else skip the script.我在想是否有任何方法可以在 AzurePowerShell@5 任务下实现 if else 条件 - 如果 wiki 不存在 - 创建 wiki,否则跳过脚本。

Yes.是的。 You can use PowerShell script to run the Rest API: Wikis - List to list all wiki names.您可以使用 PowerShell 脚本运行 Rest API: Wikis - List以列出所有 wiki 名称。 Then we can check if the wiki name exists and do next actions.然后我们可以检查 wiki 名称是否存在并执行下一步操作。

Here is an example:这是一个例子:

$Testwikiname = "sampleCodeWiki10"

$url="https://dev.azure.com/orgname/projectname/_apis/wiki/wikis?api-version=7.0"

$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$(system.accesstoken)"))

$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Get -ContentType application/json 

echo $response 

$count = 0

Foreach($wikiname in $response.value.name)
{
  echo $wikiname 

  if($wikiname -eq $Testwikiname)
  {
     $count = $count + 1 
  }
}

echo $count

if($count -eq 1 )
{
  echo "Wiki Page: $Testwikiname exists in the project"
}

else
{

$url1="https://dev.azure.com/orgname/project/_apis/wiki/wikis?api-version=6.0"


$JSON = 
"{
  `"version`": {
    `"version`": `"master`"
  },
  `"type`": `"codeWiki`",
  `"name`": `"$Testwikiname`",
  `"projectId`": `"aaea0fe6-801c-46e3-be1e-dcacdcb0c384`",
  `"repositoryId`": `"d7c2d91d-9175-4a45-97bc-aa743275bebc`",
  `"mappedPath`": `"/test5`"

}"
   

$response1 = Invoke-RestMethod -Uri $url1 -Headers @{Authorization = "Basic $token"} -Method Post -Body $JSON -ContentType application/json 

echo " Wiki Page: $Testwikiname  has been created"


  
}

Result:结果:

在此处输入图像描述

Your task works as expected.... Before creation, you have to check the existence of your wiki... Check this request: Query by name您的任务按预期工作....在创建之前,您必须检查您的维基是否存在...检查此请求: 按名称查询

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM