Skip to content

Commit

Permalink
Closes #36. Project Variable Templates are now cloned properly for bo…
Browse files Browse the repository at this point in the history
…th new and existing projects.
  • Loading branch information
BobJWalker committed Mar 21, 2022
1 parent 42318aa commit f064074
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ This script has been tested against the following versions of Octopus Deploy:
- `2021.2.x`
- `2021.3.x`
- `2022.1.x`
- `2022.2.x`

It should work with `3.4.x`+ release of Octopus Deploy. The script will run some version checks to ensure it doesn't call the wrong API endpoint. There is a far better chance the script will work using a `2020.x` or higher release of Octopus Deploy.

Expand Down
49 changes: 49 additions & 0 deletions src/Cloners/ProjectCloner.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ function Copy-OctopusProjectSettings
$copyOfProject.ClonedFromProjectId = $null

$copyOfProject.IncludedLibraryVariableSetIds = @(Get-OctopusProjectReferencedVariableSets -project $copyOfProject -sourceProjectLibraryVariableSets @($copyOfProject.IncludedLibraryVariableSetIds) -sourceData $sourceData -destinationData $destinationData)
$copyOfProject.Templates = @()
$copyOfProject.Templates = @(Get-OctopusProjectTemplatesExistingProject -sourceProject $sourceProject -destinationProject $copyOfProject)

$copyOfProject.ProjectGroupId = Convert-SourceIdToDestinationId -SourceList $SourceData.ProjectGroupList -DestinationList $DestinationData.ProjectGroupList -IdValue $copyOfProject.ProjectGroupId -ItemName "$($copyOfProject.Name) Project Group" -MatchingOption "ErrorUnlessExactMatch"
$copyOfProject.LifeCycleId = Convert-SourceIdToDestinationId -SourceList $SourceData.LifeCycleList -DestinationList $DestinationData.LifeCycleList -IdValue $copyOfProject.LifeCycleId -ItemName "$($copyOfProject.Name) Default Lifecycle" -MatchingOption "ErrorUnlessExactMatch"
Expand Down Expand Up @@ -107,6 +109,8 @@ function Copy-OctopusProjectSettings
$existingVariableSetIds = @($matchingProject.IncludedLibraryVariableSetIds)
$matchingProject.IncludedLibraryVariableSetIds = @(Get-OctopusProjectReferencedVariableSets -project $sourceProject -sourceProjectLibraryVariableSets @($sourceProject.IncludedLibraryVariableSetIds) -sourceData $sourceData -destinationData $destinationData)

$matchingProject.Templates = @(Get-OctopusProjectTemplatesExistingProject -sourceProject $sourceProject -destinationProject $matchingProject)

foreach ($variableSetId in $existingVariableSetIds)
{
if ($matchingProject.IncludedLibraryVariableSetIds -notcontains $variableSetId)
Expand Down Expand Up @@ -227,5 +231,50 @@ function Get-OctopusProjectReferencedVariableSets
}
}

return $returnList
}

function Get-OctopusProjectTemplatesExistingProject
{
param (
$sourceProject,
$destinationProject
)

$sourceTemplateList = @($sourceProject.Templates)
$destinationTemplateList = @($destinationProject.Templates)

$returnList = @()

Write-OctopusVerbose "Looping through all the source project variable templates and add any new ones to the destination."
foreach ($sourceTemplate in $sourceTemplateList)
{
Write-OctopusVerbose "Cloning the project template variable $($sourceTemplate.Name) to the destination project $($destinationProject.Name)"

$newTemplate = Copy-OctopusObject -ItemToCopy $sourceTemplate -ClearIdValue $true -SpaceId $null
$matchingDestinationTemplate = Get-OctopusItemByName -ItemList $destinationTemplateList -ItemName $sourceTemplate.Name

if ($null -ne $matchingDestinationTemplate)
{
Write-OctopusVerbose "The project variable template $($sourceTemplate.Name) already exists on the destination project with the ID of $($matchingDestinationTemplate.Id). Updating all other properties to match."
$newTemplate.Id = $matchingDestinationTemplate.Id
}

$returnList += $newTemplate
}

Write-OctopusVerbose "Making sure we aren't about to delete and project variable templates."
foreach ($destinationTemplate in $destinationTemplateList)
{
Write-OctopusVerbose "Checking to see if the project template variable $($destinationTemplate.Name) exists on the source"

$matchingSourceTemplate = Get-OctopusItemByName -ItemList $sourceTemplateList -ItemName $destinationTemplate.Name
if ($null -eq $matchingSourceTemplate)
{
Write-OctopusVerbose "The project template variable $($destinationTemplate.Name) does not exist on the source. Adding it back to the list so we don't lose it."
$returnList += $destinationTemplate
}
}

return $returnList
}
2 changes: 1 addition & 1 deletion src/Core/Logging.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
$currentDate = Get-Date
$currentDateFormatted = $currentDate.ToString("yyyy_MM_dd_HH_mm_ss")
$clonerVersion = "3.2.4"
$clonerVersion = "3.3.0"

$logFolder = "$PSScriptRoot\..\..\"
$logArchiveFolder = "$PSScriptRoot\..\..\logs\archive_$currentDateFormatted"
Expand Down

0 comments on commit f064074

Please sign in to comment.