[英]Integrate CI and CD together Azure Devops
我们需要您的支持才能在我们的发布管道上继续部署。
环境:CI 或 Build Pipeline 位于 Azure Devops Services CD 或 Release 管道位于 Azure Devops Server
我们想在构建版本没有自动启动后立即将 CI 和 CD 集成在一起。(我必须手动执行发布)
[![在此处输入图像描述][1]][1]
[![在此处输入图像描述][2]][2]
[![在此处输入图像描述][3]][3]
azure devops 服务和 azure devops 服务器之间的服务连接
[![在此处输入图像描述][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
我们希望在 Build 版本没有自动启动后立即将 CI 和 CD 集成在一起。
由于 azure devops 服务在云端,而 azure devops 服务器在本地,因此没有可以集成 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. 发布 - 创建
这是一个例子:
您可以将 Powershell 任务添加到构建的末尾,然后您可以在 powershell 任务中添加以下脚本:
$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
如果您的 CI/Build 管道在自托管代理上运行,您可以直接在同一代理作业中添加 powershell 任务。
如果您的构建管道在 Microsoft 托管的代理上运行,您需要创建一个自托管代理并添加额外的代理作业以运行 powershell 脚本。
在这种情况下,您还需要设置Dependencies 。
注意:运行 rest api 以触发 azure devops 服务器版本时,需要确保它们在同一网络范围内。 所以它需要自托管代理。
更新:
要定义阶段,您可以参考以下文档和示例:
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
更新2:
为了实现一个类似于system.accesstoken变量的function,可以尝试如下设置。
Step1:在Azure Devops Service Build Pipeline中创建一个变量并将其设置为变量:
Step2: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,
"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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.