将CI和CD集成到Azure Devops中 [英] Integrate CI and CD together Azure Devops

本文介绍了将CI和CD集成到Azure Devops中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们需要您的支持,以便在我们的发布管道上继续部署.

环境:CI或构建管道位于Azure Devops Services上CD或发布管道在Azure Devops Server上

我们希望在Build版本没有自动启动之后立即将CI和CD集成在一起.(我必须手动执行发布)

[![在此处输入图片描述] [1]] [1]

[![在此处输入图片描述] [2]] [2]

[![在此处输入图片描述] [3]] [3]

azure devops服务和azure devops服务器之间的服务连接

[![在此处输入图片描述] [4]] [4]

 #触发器释放管道-任务:PowerShell @ 2displayName:触发器发布管道"输入:targetType:内联"powershell:|$ url ="https://vsrm.dev.azure.com/{OrganizationName}/{ProjectName}/_apis/release/releases?api-version=6.0"$ token = [System.Convert] :: ToBase64String([System.Text.Encoding] :: ASCII.GetBytes(:$($ env:TOKEN)")))$ JSON = @'{"definitionId":38,变量":{版本":{值":"$(build.buildnumber)"}}}'@$ response = Invoke-RestMethod -Uri $ url -Headers @ {Authorization ="Basic $ token"} -Method Post -ContentType application/json -body $ JSONdisplayName:'PowerShell脚本'环境:代币:$(token)```[1]:https://i.stack.imgur.com/g4J8I.png[2]:https://i.stack.imgur.com/njsVU.png[3]:https://i.stack.imgur.com/MIaJJ.png[4]:https://i.stack.imgur.com/20wk9.png 

解决方案

我们希望在Build版本没有自动启动后立即将CI和CD集成在一起.

由于azure devops服务位于云端,而azure devops服务器位于本地,因此没有可以集成CI/CD的即用型功能.

但是您可以使用PowerShell任务在Azure Devops Service中运行Rest API来触发Azure Devops Server上的发布.

如果您的CI/Build管道运行在

注意::运行rest api触发azure devops服务器版本时,需要确保它们在相同的网络范围内.因此,它需要自托管代理.

更新:

要定义一个阶段,可以参考

第2步:PowerShell任务脚本:

 -powershell:|$ url ="https://{instance}/{collection}/{project}/_apis/release/releases?api-version=5.0"$ token = [System.Convert] :: ToBase64String([System.Text.Encoding] :: ASCII.GetBytes(:$($ env:TOKEN)")))$ JSON = @'{"definitionId":38,变量":{版本":{值":"$(build.buildnumber)"}}}'@$ response = Invoke-RestMethod -Uri $ url -Headers @ {Authorization ="Basic $ token"} -Method Post -ContentType application/json -body $ JSONdisplayName:'PowerShell脚本'环境:代币:$(代币) 

we need your support on enabling continues deployment on our release pipeline .

Environment : CI or Build Pipeline is on Azure Devops Services CD or Release pipeline is on Azure Devops Server

We want to Integrate CI and CD together right now after Build release is not kicking automatically.(I have to manually execute the release )

[![enter image description here][1]][1]

[![enter image description here][2]][2]

[![enter image description here][3]][3]

Service connection between azure devops service and azure devops server

[![enter image description here][4]][4]

# Trigger Release pipeline
- task: PowerShell@2
  displayName: 'Trigger Release pipeline'
  inputs:
    targetType: 'inline'
    powershell: |
     $url = "https://vsrm.dev.azure.com/{OrganizationName}/{ProjectName}/_apis/release/releases?api-version=6.0"
     
     $token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($env:TOKEN)"))
     
     $JSON = @'
     {
       "definitionId": 38,
       "variables": {
            "Version": {
              "value": "$(build.buildnumber)"
            }
      
        }
     }
     '@
          
     $response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Post -ContentType application/json -body $JSON
    displayName: 'PowerShell Script'
    env:
      TOKEN: $(token)```


  [1]: https://i.stack.imgur.com/g4J8I.png
  [2]: https://i.stack.imgur.com/njsVU.png
  [3]: https://i.stack.imgur.com/MIaJJ.png
  [4]: https://i.stack.imgur.com/20wk9.png

解决方案

We want to Integrate CI and CD together right now after Build release is not kicking automatically.

Since the azure devops service is on the cloud side and the azure devops server is local, there is no out-of-the-box feature that can Integrate CI/CD.

But you could use PowerShell task to run the Rest API in Azure Devops Service to trigger the Release on Azure Devops Server . Releases - Create

Here is an example:

You can add the Powershell Task to the end of the build, then you could add the following script in the powershell task:

$token = "PAT"

$url = "https://{instance}/{collection}/{project}/_apis/release/releases?api-version=5.0"

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

$JSON = @'
{
  "definitionId": DefinitionID(e.g. 15), 
  "description": "Creating Sample release",
  "artifacts": [],
  "isDraft": false,
  "reason": "none",
  "manualEnvironments": null
}
'@
     
$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Post -ContentType application/json -body $JSON

If your CI/Build pipeline is running on self-hosted agent, you can directly add the powershell task at the same agent job.

If your build pipeline is running on Microsoft-hosted agent, you need to create a self-hosted agent and add additional agent job to run powershell script.

In this case, you also need to set the Dependencies.

Note: When running the rest api to trigger the azure devops server release, you need to ensure that they are in the same network range. So it needs self-hosted agent.

Update:

To define a stage, you could refer to the following doc and sample:

stages:
- stage: A
  jobs:
  - job: A1
    pool: 
     name: Default
    steps:
      - script: echo 


- stage: B
  pool:
    name: Default
  jobs:
  - job: B1
    steps:
       - task: PowerShell@2
         inputs:
           targetType: 'inline'
           script: |
             $token = "PAT"
             
             $url = "https://{instance}/{collection}/{project}/_apis/release/releases?api-version=5.0"
             
             $token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
             
             $JSON = @'
             {
                "definitionId": ID,
               "variables": {
                    "Version": {
                      "value": "$(Build.buildnumber)"
                    }
              
                }
             }
             '@
                  
             $response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Post -ContentType application/json -body $JSON

Update2:

In order to achieve a function similar to the system.accesstoken variable, you can try the following settings.

Step1: Create a variable in Azure Devops Service Build Pipeline and set it as variable:

Step2: PowerShell Task script:

- powershell: |
   
   
   $url = "https://{instance}/{collection}/{project}/_apis/release/releases?api-version=5.0"
   
   $token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($env:TOKEN)"))
   
   $JSON = @'
   {
     "definitionId": 38,
     "variables": {
          "Version": {
            "value": "$(build.buildnumber)"
          }
    
      }
   }
   '@
        
   $response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Post -ContentType application/json -body $JSON
  displayName: 'PowerShell Script'
  env:
    TOKEN: $(token)

这篇关于将CI和CD集成到Azure Devops中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆