Trigger deployment project after another deployment project done
Asked by Sai Yu:

i have 10 deployment projects. is it possible to trigger all 10 deployment projects without manually trigger each one at a time?


Answers:
author: Sai Yu
body: i end up using powershell to call REST Api.

1. loop all deployment plans
2. pickup all project with name "Production"
3. get the latest release id (first one return) from each project
4. trigger deploy plans using REST API

below is my code if you are interested:

$username = "bambooUserName"  
$password = "bambooPassword"  
$deployEnv = "PRODUCTION"

function Get-Data(\[string\]$httpMethod, \[string\]$url) {  
$credPair = "$($username):$($password)"  
$encodedCredentials = \[System.Convert\]::ToBase64String(\[System.Text.Encoding\]::ASCII.GetBytes($credPair))  
$headers = @{ Authorization = "Basic $encodedCredentials" }  
$responseData = Invoke-WebRequest -Uri $url -Method $httpMethod -Headers $headers -ContentType 'application/json; charset=utf-8' -UseBasicParsing  
return $responseData  
}

function TriggerLatestDeployByProjectId(\[string\]$projectId){  

$data = Get-Data -httpMethod Get -url http://localhost:9087/bamboo/rest/api/latest/deploy/project/$projectId.json  
$dataToDict = $data \| ConvertFrom-Json

$envId = ""  
for ($i=0; $i -lt $dataToDict.environments.length; $i++) {  
$env = $dataToDict.environments\[$i\]  
If ($env.name -eq $deployEnv)  
{  
$envId = $env.id  
}  
}  

if ($envId)  
{  
$releaseId = ""  
$data = Get-Data -httpMethod Get -url http://localhost:9087/bamboo/rest/api/latest/deploy/project/$projectId/versions.json  
$dataToDict = $data \| ConvertFrom-Json  

$releaseId = $dataToDict.versions\[0\].id  

if ($releaseId)  
{  
$data = Get-Data -httpMethod POST -url "http://localhost:9087/bamboo/rest/api/latest/queue/deployment/?environmentId=$($envId)\&versionId=$($releaseId)"  
$dataToDict = $data \| ConvertFrom-Json  
return "Sucess: resultId= $($dataToDict.deploymentResultId)"  
}  
else  
{  
return "Error: $($projectId) Missing releaseId"  
}  
}  
else  
{  
return "Error: $($projectId) Missing EnvId"  
}  
}

function DeployAll()  
{  
$data = Get-Data -httpMethod Get -url http://localhost:9087/bamboo/rest/api/latest/deploy/project/all.json  
$dataToDict = $data \| ConvertFrom-Json  

for ($i=0; $i -lt $dataToDict.length; $i++) {  
$currProject = $dataToDict\[$i\]  
$deployResultId = TriggerLatestDeployByProjectId -projectId $currProject.id  
"Result:$deployResultId $($currProject.name) - $($currProject.id)"  
}  
}

DeployAll


author: Jimmy Seddon
body: Hi [@Sai Yu](/t5/user/viewprofilepage/user-id/3261332),

Sorry I shouldn't have assumed that you were trying to deploy environments from the same project. In this case I think the only way you can accomplish this is to use a script task at the end of "project A environment A1" that uses an API call to trigger "project B enviroment B1".

You can see more about how to do that here:

<https://developer.atlassian.com/server/bamboo/rest-api-deployment-triggers-for-bamboo/>

I hope that helps!

-Jimmy

comments: author: Sai Yu
body: [@Jimmy Seddon](/t5/user/viewprofilepage/user-id/512822)

I was looking at the Rest Api trigger deploy too. But the problem i am facing is how to get the latest versionId?

my build plan needs to do all these

1. get the latest versionId by projectID \*\*curl rest/api/latest/deploy/project/7405569/versions'
2. save it as variable (how to read response and how to save it as variable to use on the next step)
3. use it on the curl command? (call another Rest Api using variable)

```
curl -X POST 'http://localhost:9087/bamboo/rest/api/latest/queue/deployment/?environmentId=7536641&versionId=28803073' 
```

thanks

Sai


author: Sai Yu
body: thanks [@Jimmy Seddon](/t5/user/viewprofilepage/user-id/512822) for the quick reply.

The "after successful deployment" trigger only allow me to select deployment within the project. i need to be able to trigger different deployment project.

what i need to do is "project A environment A1" trigger deploy after successfully deployed "project B enviroment B1".


author: Jimmy Seddon
body: Hello [@Sai Yu](/t5/user/viewprofilepage/user-id/3261332)

Welcome to the Community!

Yes you can do this, what you will need to do is on each environment you wish to deploy automatically. Edit the environment, and select triggers:

![EditEnvironment.JPG](https://community.atlassian.com/t5/image/serverpage/image-id/70613i923520445BC71F5F/image-dimensions/490x181?v=v2 "EditEnvironment.JPG")

Then you will want to click the add trigger button and select what type of trigger you would like to execute:

![AddTriggers.JPG](https://community.atlassian.com/t5/image/serverpage/image-id/70614i3F52E764B051034A/image-dimensions/475x292?v=v2 "AddTriggers.JPG")

In the example above I'm using the successful completion of another deployment environment, but there are some other options as well. This will automatically trigger the deployment of this environment when the trigger conditions are met. You can use this to setup multiple environments the same way so they will all automatically trigger under the same conditions.

I hope that helps!

-Jimmy