0

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
Satyam Pandey
  • 593
  • 2
  • 10
  • 32
  • Hi @Satyam Pandey.Is there any update about this ticket? Feel free to let me know if the answers could give you some help. Just a reminder of [this](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work). – Kevin Lu-MSFT Feb 24 '21 at 05:05

1 Answers1

0

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

enter image description here

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.

enter image description here

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:

enter image description here

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)
Kevin Lu-MSFT
  • 20,786
  • 3
  • 19
  • 28
  • i came across a extension https://msftplayground.com/2019/02/trigger-a-pipeline-from-an-azure-devops-pipeline/ , can i use this extension to trigger my release pipeline on server, once i have done with my build pipeline on service, if yes, i am using build pipeline as YAML base so how can i define add a stage – Satyam Pandey Feb 25 '21 at 02:35
  • @SatyamPandey. This extension can be used in Azure Devops Service. But it couldn't trigger the Azure Devops Server pipeline in Azure Deveops Service. – Kevin Lu-MSFT Feb 25 '21 at 02:37
  • 1
    Just to tell you one behavior which i observed i added this in my release pipeline and when i executed my release pipeline ,my build pipe line also got triggered automatically . attaching images – Satyam Pandey Feb 25 '21 at 02:45
  • Thanks for your info.Got it. This is because the Azure Devops is on cloud. When you azure devops server could connect the Internet, it could trigger the pipeline on Azure Devops Service (They could connect via Service connection). But Azure Devops Service couldn't trigger the pipeline on Azure Devops Server, Because they cannot connect via service connection. – Kevin Lu-MSFT Feb 25 '21 at 02:49
  • ohk, appreciate your effort on helping me i have tried creating ""azure devops api service connection"" on azure devops service, side, i was able to do, image attached i think next i have to use "" https://learn.microsoft.com/en-us/azure/devops/pipelines/scripts/powershell?view=azure-devops&tabs=yaml#example-powershell-script-access-rest-api "" in my pipeline and see – Satyam Pandey Feb 25 '21 at 03:23
  • The "azure devops api service connection can only be used in Trigger Azure Devops pipeline task. Therefore it does not apply to your situation, From the doc, you want to use **system.accesstoken** variable. This variable can only be used in the same organization. – Kevin Lu-MSFT Feb 25 '21 at 05:09
  • Please refer to my update2. You could manually set the token as secret and use it in Powershell task – Kevin Lu-MSFT Feb 25 '21 at 05:13
  • i don't know how to give thanks you, you helping me to keep going keep going – Satyam Pandey Feb 25 '21 at 05:17
  • It's my pleasure to help you. If you have any questions feel free to let me know. If the answer could give you some help, you may consider [accepting it as answer liker this](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work). Thank you. – Kevin Lu-MSFT Feb 25 '21 at 05:20
  • ohk i going with your instruction on update2, i have added the pipeline script – Satyam Pandey Feb 25 '21 at 05:43
  • Sorry. The api url need a liitle change: https://{instance}/{collection}/{project}/_apis/release/releases?api-version=5.0 This format is for Azure Devops Server. Note: Don't forget to use self-hosted agent to run the pipeline. Feel free to let me know if it could work – Kevin Lu-MSFT Feb 25 '21 at 05:45
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/229183/discussion-between-satyam-pandey-and-kevin-lu-msft). – Satyam Pandey Feb 25 '21 at 06:14