15
votes

I'm trying to copy a release definition from one Azure Devops (VSTS) project to another. In the source project I'm able to export the release definition as a json file.

export release definition

But I'm not able to find a way to import this into the target project. I only get options to create new definitions.

create release definition

6

6 Answers

10
votes

Since VSTS is Renamed/updated into Azure DevOps, there are some changes in the UI to find those option above.

For Importing

enter image description here

For Exporting

enter image description here

Regarding the addendum

All the build/Release definition came along with the single term called Pipeline. So there is no need to create a dummy release definition in order to import a new one. You can import a new release definition without having any dummy one.

10
votes

If you don't see the import option, you can try creating a first empty pipeline

enter image description here

Once you have it, it enables the import option

enter image description here

9
votes

enter image description here

Go to Build and Releases and then Releases and there you will find an option. Click the + button and there is an option to import release definitions

Let me know if you need further help.

8
votes

From reading the comments above the reason you are having issues with importing a Release Definition from a different VSTS/Azure DevOps origanization is because the Release Definition that you exported contains GUID references to objects that do not exist in the organization or project that you are importing the Release Definition into. So, you are going to have to roll up your sleeves and do some work in order to have a successful smooth and fully integrated Release Definition that you can use as a template. We are going to be using the REST API found here: https://docs.microsoft.com/en-us/rest/api/vsts/release/definitions/create?view=vsts-rest-4.1

Here is my technical answer and what I did to duplicate a Release Definition and created a template to be used numerous times:

  1. Export the Release Definition of your choice

  2. Format the JSON in some tool like https://jsonformatter.curiousconcept.com to make it more readable, and then save it in a text file like ReleasePipelineTemplate-Formatted.json

  3. I don't know what components you have defined in your Release Definition, but take a look around and see how GUIDs are used throughout. It might be helpful to create a release definition in order to find the GUIDs that you are going to have to replace.

  4. The GUIDs to find are SYSTEM_COLLECTIONID, SYSTEM_TEAMPROJECTID, SYSTEM_DEFINITIONID, BUILD_QUEUEDBYID

  5. Let us get some variables mentioned above from your build organization and project. In order to get these we need to print some environment variables during a regular build. So, please add a build step somewhere in the beginning of your CI build that adds a "Batch Script" step with the following command "C:\Windows\System32\cmd.exe" and argument "/c set". After the build is done, look into the logs at that build step and find the values for those variables. You will need those in the Release Definition.

  6. You should make a copy before we modify the json file. You should trim down the json file a bit by removing a few things like "createdOn", "modifiedBy", "modifiedOn". At the very bottom is "url" are 3 link. Those needs to modified with the Team Project ID GUID for example. This is just an example of places that need to be modified. Please inspect your own Release Definition.

  7. I'm going to assume you've made the necessary changes to your JSON file and ready to try to import it. Create a PowerShell script called 'CreateReleasePipeline.ps1' with the following script example.

$homeDir = "$(Split-Path -parent $MyInvocation.MyCommand.Definition)"
$VSTS_RestApiHost = "https://vsrm.dev.azure.com/yourpath/_apis"
$VSTS_PAT = "YOUR_PAT_TOKEN_HERE"
$headers = @{Authorization = "Basic " + [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($VSTS_PAT)"))}
$relDefInfo = Get-Content "$homeDir\ReleasePipelineTemplate-Formatted.json"
Write-Host "Creating Release Pipeline..."
$createReleaseDef = "$($VSTS_RestApiHost)/release/definitions?api-version=4.1-preview.3"
$response = Invoke-RestMethod -Method Post -Uri $createReleaseDef -Body $relDefInfo -ContentType 'application/json' -Headers $headers
Write-Host "Create Release Pipeline done. Pipeline id: $($response.id)"
if ($response.id -gt 0) {
  Write-Host "Release definition: succeeded."
}
Write-Host $response.id
  1. Execute your script with '.\CreateReleasePipeline.ps1' and the output will look something like this:

.\CreateReleasePipeline.ps1
Creating Release Pipeline...
Create Release Pipeline done. Pipeline id: 71
Release definition: succeeded.
71
  1. Refresh your Release Pipeline web page and you should see your newly created Release Pipeline.

  2. You can go ahead and re-try until you are satisfied with your results.

I am using this REST API to put a Release Definition template into the source code repository so that I don't have to update 50+ and growing Release Pipelines when I make an adjustment to them. I can a REST API call check if a Release Definition exists, create it if it doesn't, update it if it is not up-to-date. All without having to cramp my hand having to update them manually or create a pipeline when a new get repo is created. I want to automate everything as much as possible. I want to change once or as few as possible and let the system update itself.

Please let me know if this helps.

4
votes

Leaving this here as an addendum to the accepted answer.

The accepted answer does work, but I should mention that my initial problem wasn't precisely solved by it.

Basically if you don't have any existing release definitions, there doesn't seem to be a way to import definitions. What I did was to create a 'dummy' definition and then follow what the accepted answer says. Creating at least one definition brings you to that screen otherwise you'll only get the screens I show in my question.

0
votes

After export it generates json file save it in the local folder. And then go to the release or plus (+) button click on it and you can see inport option there click on it and select that json file. and thats it.....