File System - Clean Configuration Transforms

Octopus.Script exported 2016-01-21 by boro2g belongs to ‘File System’ category.

Clean out configuration transform files from the installation directory after a deployment (e.g. Web.Release.config, YourApp.Production.config, etc.).

Parameters

When steps based on the template are included in a project’s deployment process, the parameters below can be set.

Path to clean

PathToClean

The path to clean.

Usually you would set this to #{Octopus.Action[StepName].Output.Package.InstallationDirectoryPath}.

Environment Name

EnvironmentName = *

If you want to nuke all config files i.e. *.*.config then leave with the default value *.

Otherwise to just nuke *.EnvironmentName.config then set as the environment name: #{Octopus.Environment.Name}

Script body

Steps based on this template will execute the following PowerShell script.

# Running outside Octopus Deploy
param(
    [string]$pathToClean,
    [string]$environmentName,
    [switch]$whatIf
)

function GetParam($Name, [switch]$Required) {
    $result = $null

    if ($OctopusParameters -ne $null) {
        $result = $OctopusParameters[$Name]
    }

    if ($result -eq $null) {
        $variable = Get-Variable $Name -EA SilentlyContinue
        if ($variable -ne $null) {
            $result = $variable.Value
        }
    }

    if ($Required -and [string]::IsNullOrEmpty($result)) {
        throw "Missing parameter value $Name"
    }

    return $result
}

& {
    param(
        [string]$pathToClean,
        [string]$environmentName
    )

    Write-Host "Cleaning Configuration Transform files from $pathToClean and environment: $environmentName"

    if (Test-Path $pathToClean) {
        Write-Host "Scanning directory $pathToClean"
        $regexFilter = "*.$environmentName.config" 
        Write-Host "Filter $regexFilter"

        if ($pathToClean -eq "\" -or $pathToClean -eq "/") {
            throw "Cannot clean root directory"
        }

        $filesToDelete = Get-ChildItem $pathToClean -Filter $regexFilter -Recurse | `
                         Where-Object {!$_.PsIsContainer -and ($_.Name -NotMatch "((?i)(^.*\.exe\.config$|.*\.dll\.config$)$)")}

        if (!$filesToDelete -or $filesToDelete.Count -eq 0) {
            Write-Warning "There were no files matching the criteria"
        } else {

            Write-Host "Deleting files"
            if ($whatIf) {
                Write-Host "What if: Performing the operation `"Remove File`" on targets"
            }

            foreach ($file in $filesToDelete)
            {
                Write-Host "Deleting file $($file.FullName)"
                
                if (!$whatIf) {
                    Remove-Item $file.FullName -Force
                }
            }
        }

    } else {
        Write-Warning "Could not locate path `"$pathToClean`""
    }

} `
(GetParam 'PathToClean' -Required) `
(GetParam 'EnvironmentName' -Required)

Provided under the Apache License version 2.0.

Report an issue

To use this template in Octopus Deploy, copy the JSON below and paste it into the Library → Step templates → Import dialog.

{
  "Id": "9a2b84db-2940-4d9a-b61f-c82df35cee6c",
  "Name": "File System - Clean Configuration Transforms",
  "Description": "Clean out configuration transform files from the installation directory after a deployment (e.g. Web.Release.config, YourApp.Production.config, etc.).",
  "Version": 10,
  "ExportedAt": "2016-01-21T15:39:24.527+00:00",
  "ActionType": "Octopus.Script",
  "Author": "boro2g",
  "Parameters": [
    {
      "Name": "PathToClean",
      "Label": "Path to clean",
      "HelpText": "The path to clean.\n\nUsually you would set this to `#{Octopus.Action[StepName].Output.Package.InstallationDirectoryPath}`.",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Name": "EnvironmentName",
      "Label": "Environment Name",
      "HelpText": "If you want to nuke all config files i.e. `*.*.config` then leave with the default value `*`.\n\nOtherwise to just nuke `*.EnvironmentName.config` then set as the environment name: `#{Octopus.Environment.Name}`",
      "DefaultValue": "*",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    }
  ],
  "Properties": {
    "Octopus.Action.Script.ScriptBody": "# Running outside Octopus Deploy\nparam(\n    [string]$pathToClean,\n    [string]$environmentName,\n    [switch]$whatIf\n)\n\nfunction GetParam($Name, [switch]$Required) {\n    $result = $null\n\n    if ($OctopusParameters -ne $null) {\n        $result = $OctopusParameters[$Name]\n    }\n\n    if ($result -eq $null) {\n        $variable = Get-Variable $Name -EA SilentlyContinue\n        if ($variable -ne $null) {\n            $result = $variable.Value\n        }\n    }\n\n    if ($Required -and [string]::IsNullOrEmpty($result)) {\n        throw \"Missing parameter value $Name\"\n    }\n\n    return $result\n}\n\n& {\n    param(\n        [string]$pathToClean,\n        [string]$environmentName\n    )\n\n    Write-Host \"Cleaning Configuration Transform files from $pathToClean and environment: $environmentName\"\n\n    if (Test-Path $pathToClean) {\n        Write-Host \"Scanning directory $pathToClean\"\n        $regexFilter = \"*.$environmentName.config\" \n        Write-Host \"Filter $regexFilter\"\n\n        if ($pathToClean -eq \"\\\" -or $pathToClean -eq \"/\") {\n            throw \"Cannot clean root directory\"\n        }\n\n        $filesToDelete = Get-ChildItem $pathToClean -Filter $regexFilter -Recurse | `\n                         Where-Object {!$_.PsIsContainer -and ($_.Name -NotMatch \"((?i)(^.*\\.exe\\.config$|.*\\.dll\\.config$)$)\")}\n\n        if (!$filesToDelete -or $filesToDelete.Count -eq 0) {\n            Write-Warning \"There were no files matching the criteria\"\n        } else {\n\n            Write-Host \"Deleting files\"\n            if ($whatIf) {\n                Write-Host \"What if: Performing the operation `\"Remove File`\" on targets\"\n            }\n\n            foreach ($file in $filesToDelete)\n            {\n                Write-Host \"Deleting file $($file.FullName)\"\n                \n                if (!$whatIf) {\n                    Remove-Item $file.FullName -Force\n                }\n            }\n        }\n\n    } else {\n        Write-Warning \"Could not locate path `\"$pathToClean`\"\"\n    }\n\n} `\n(GetParam 'PathToClean' -Required) `\n(GetParam 'EnvironmentName' -Required)\n",
    "Octopus.Action.Script.Syntax": "PowerShell"
  },
  "Category": "File System",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/file-system-clean-configuration-transforms.json",
  "Website": "/step-templates/9a2b84db-2940-4d9a-b61f-c82df35cee6c",
  "Logo": "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAMAAACahl6sAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAKhQTFRF/////78A/6oAVVVV/++//+q/gICA1dXV/79A/9R///ff/8MQ/9dg/8cg//vv/68A/+ef//PP//rv/7Ug/68Q/+Sv/8sw/9tw/9NQ/89w/+uv/+OP/8pg/89A/8VQ/9+f/9+A/9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