Error while adding step template Find and Replace

When we try to add the step template File system : Find and Replace we are getting the error as below:

Actions names ‘File System - Find and Replace’, ‘Read File Content’ are already in use by other steps in this deployment process. Action names must be unique.

Here is the script:

function Execute-FindReplace($target, $find, $replace, $ignoreCase) {
$options = [System.Text.RegularExpressions.RegexOptions]::None
if ($ignoreCase) {
$options = [System.Text.RegularExpressions.RegexOptions]::IgnoreCase
}

Write-Output "Searching $target..."
$orig = [System.IO.File]::ReadAllText($target)

$escFind = [System.Text.RegularExpressions.Regex]::Escape($find)
$regex = new-object System.Text.RegularExpressions.Regex($escFind, $options)
$removed = $regex.Replace($orig, '')

$occurrences = ($orig.Length - $removed.Length) / $find.Length
if ($occurrences -gt 0) {
    Write-Output "Found $occurrences occurrence(s), replacing..."
    
    $escReplace = $replace.Replace('$', '$$')
    $replaced = $regex.Replace($orig, $escReplace)
    [System.IO.File]::WriteAllText($target, $replaced)
}

}

if ([string]::IsNullOrEmpty($FRFindText)) {
throw “A non-empty ‘Find’ text block is required”
}

Write-Output “Replacing occurrences of ‘$FRFindText’ with ‘$FRReplaceText’”
if ([Boolean] $FRIgnoreCase) {
Write-Output “Case will be ignored”
}

$FRCandidatePathGlobs.Split(";") | foreach {
$glob = $_.Trim()
Write-Output “Searching for files that match $glob…”

$matches = $null
$splits = $glob.Split(@('/**/'), [System.StringSplitOptions]::RemoveEmptyEntries)

if ($splits.Length -eq 1) {
    $splits = $glob.Split(@('\**\'), [System.StringSplitOptions]::RemoveEmptyEntries)
}

if ($splits.Length -eq 1) {
    $matches = ls $glob
} else {
    if ($splits.Length -eq 2) {
        pushd $splits[0]
        $matches = ls $splits[1] -Recurse
        popd
    } else {
        $splits
        throw "The segment '**' can only appear once, as a directory name, in the glob expression"

    }
}

$matches | foreach {
    
    $target = $_.FullName

    Execute-FindReplace -target $target -find $FRFindText -replace $FRReplaceText -ignoreCase ([Boolean] $FRIgnoreCase)
}

}

Write-Output “Done.”

Hi Ramya,

Thanks for getting in touch.

Looking at the error message, you need to rename the step as there is already a step in your deployment process with the same name.

I hope that helps.

Thank you and kind regards,
Henrik

We tried renaming the step and that also provided the same error. All type of new names were provided but still the error persists.

Can you send through a screenshot of your current full deployment process, and the error you receive when trying to add another File System - Find and Replace step with a different name.

Thanks,
Henrik

Please find the screen shot:

Could you send through your deployment process as JSON for me, see below:

image

Thanks,
Henrik

deploymentprocess-Projects-13.json (324.1 KB)

As I suspected, you have 2 steps that are named File System - Find and Replace and 2 steps that are named Read File Content already in your deployment process and if I remember correctly we had a bug a while back that allowed for this scenario to occur.

I’ve written up the C# script (can be run using LINQPad, link to script or just added to a .NET console app and run that way) below that should rename the duplicate steps so that you can start editing the deployment process again (please note: requires the Octopus.Clients and JSON.Net NuGet packages):

var octopusServer = "https://<your server address>";
var octopusApiKey = "API-AAAAAAAAAAAAAAAAAAAAAAAAA";
var endPoint = new OctopusServerEndpoint(octopusServer, octopusApiKey);
var repo =  new OctopusRepository(endPoint);
var deploymentProcess = repo.DeploymentProcesses.Get("deploymentprocess-Projects-13");
var duplicateActionGroups = deploymentProcess.Steps.SelectMany(s => s.Actions).GroupBy(a => a.Name).Where(g => g.Count() > 1);
foreach(var duplicateActionGroup in duplicateActionGroups)
{
	var i = 1;
	foreach(var action in duplicateActionGroup)
	{
		action.Name = $"{action.Name} {i}";
		i++;
	}
}
repo.DeploymentProcesses.Modify(deploymentProcess);

I hope that helps.

Thank you,
Henrik

Where should i implement this script. Does this act as step template to remove the duplicate Action name.

You have 2 options here:

  • You can run it using LINQPad (I’ve shared the script here so you can download it and open in LINQPad, update the octopusServer, octopusApiKey and deploymentProcessId values with your details).
  • Or, you can create a new .NET console app and add the script there and run it that way.

I recommend you take a DB backup before running the script just to be on the safe side, in case something goes wrong (it shouldn’t as the script is a pretty simple script and only affects 1 record in the database)

Thanks,
Henrik

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.