File System - Combine all files in directory into single file
Octopus.Script exported 10/12/2018 by octobob belongs to 'File System' category.
This step template will take all the files in a single directory, sort them alphabetically, and combine them into a single file.
Optional, it will create an artifact for that file so it can be reviewed.
Parameters
When steps based on the template are included in a project's deployment process, the parameters below can be set.
Source Package Step
Optional - The name of the package step containing the files to combine. Leave this blank if the full path is in the Source Directory.
Source Directory
Optional - The folder containing all the files to combine into a single file. If using this with the source package step variable then this is the relative path to that. Otherwise this is the full path to it.
Source File Filter
Optional - The filter to apply when looking for tiles. Example *.sql or *.txt. If omitted it will grab all files in the directory.
Combined File Name
Required - the file name of all the combined files. Must include the full path. IE C:\Testing\NewFile.sql
Comment Characters
Optional - Use this if you want to include the file name in your combined file as comments. You will need to specify the comment characters for the language of the file. For example "--" for SQL files, "//" for C# files.
Create Artifact
Indicates if the combined file should be uploaded to Octopus Deploy as an artifact. Useful when combining multiple SQL scripts into a single file so a DBA can review it.
Script body
$sourceDirectoryPath = $OctopusParameters["CombineFiles.Directory.Source"]
$sourceDirectoryPackagePath = $OctopusParameters["CombineFiles.Directory.PackageSource"]
$sourceDirectoryFilter = $OctopusParameters["CombineFiles.Directory.Filter"]
$destinationFile = $OctopusParameters["CombineFiles.Destination.FileName"]
$createArtifact = $OctopusParameters["CombineFiles.Destination.CreateArtifact"]
$commentCharacters = $OctopusParameters["CombineFiles.Destination.CommentCharacters"]
if ([string]::IsNullOrWhiteSpace($sourceDirectoryPackagePath) -eq $false){
Write-Host "A previous package path was specified, grabing that"
$sourceDirectory = $OctopusParameters["Octopus.Action[$sourceDirectoryPackagePath].Output.Package.InstallationDirectoryPath"]
$sourceDirectory = "$sourceDirectory\$sourceDirectoryPath"
}
else {
$sourceDirectory = "$sourceDirectoryPath"
}
Write-Host "Source Directory: $sourceDirectory"
Write-Host "Source File Filter: $sourceDirectoryFilter"
Write-Host "Combined File Name: $destinationFile"
Write-Host "Create Artifact: $createArtifact"
Write-Host "Comment Characters: $commentCharacters"
if ([string]::IsNullOrWhiteSpace($sourceDirectory)){
throw-exception "The source directory variable is required."
}
if ((Test-Path $sourceDirectory) -eq $false){
Write-Host "The directory $sourceDirectory was not found, skipping"
exit 0
}
if ((Test-Path $destinationFile) -eq $false){
Write-Host "Creating the file: $destinationFile"
New-Item -Path $destinationFile -ItemType "file"
}
else {
Write-Host "The file $destinationFile already exists"
}
if ([string]::IsNullOrWhiteSpace($sourceDirectoryFilter)){
Write-Host "Source directory filter not specified, grabbing all files"
$filePath = "$sourceDirectory\*"
}
else {
Write-Host "Source directory filter specified, grabbing filtered files"
$filePath = "$sourceDirectory\*"
}
Write-Host "Getting child items using $filePath"
$filesToCombine = Get-ChildItem -Path $filePath -File | Sort-Object -Property Name
foreach ($file in $filesToCombine)
{
Write-Host "Adding content to $changeScript from $file"
if ([string]::IsNullOrWhiteSpace($commentCharacters) -eq $false){
Add-Content -Path $destinationFile -Value "$commentCharacters Contents from $file"
}
Add-Content -Path $destinationFile -Value (Get-Content $file)
}
if ($createArtifact -eq "True"){
New-OctopusArtifact -Path "$destinationFile"
}To use this template in Octopus Deploy, copy the JSON below and paste it into the Library → Step templates → Import dialog.
Show JSON{
"Id": "a1983aec-a8ca-4fda-b763-081fd0acecf8",
"Name": "File System - Combine all files in directory into single file",
"Description": "This step template will take all the files in a single directory, sort them alphabetically, and combine them into a single file. \n\nOptional, it will create an artifact for that file so it can be reviewed. ",
"Version": 1,
"ExportedAt": "2018-10-12T18:54:49.750Z",
"ActionType": "Octopus.Script",
"Author": "octobob",
"Packages": [],
"Parameters": [
{
"Id": "4754945b-530f-4da5-bb50-94bd8b7a2239",
"Name": "CombineFiles.Directory.PackageSource",
"Label": "Source Package Step",
"HelpText": "**Optional** - The name of the package step containing the files to combine. Leave this blank if the full path is in the Source Directory.",
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "StepName"
}
},
{
"Id": "ee8d882e-f278-4211-93d7-7dadc6d9978d",
"Name": "CombineFiles.Directory.Source",
"Label": "Source Directory",
"HelpText": "**Optional** - The folder containing all the files to combine into a single file. If using this with the source package step variable then this is the relative path to that. Otherwise this is the full path to it.",
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Id": "373ddecf-5038-4791-9ed5-dadc464b61de",
"Name": "CombineFiles.Directory.Filter",
"Label": "Source File Filter",
"HelpText": "**Optional** - The filter to apply when looking for tiles. Example *.sql or *.txt. If omitted it will grab all files in the directory.",
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Id": "9690b60a-7e25-412f-b9b2-0889bfc225b7",
"Name": "CombineFiles.Destination.FileName",
"Label": "Combined File Name",
"HelpText": "**Required** - the file name of all the combined files. Must include the full path. IE C:\\Testing\\NewFile.sql",
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Id": "50b49c3d-d9a4-49a2-b4f7-896d49cbc87b",
"Name": "CombineFiles.Destination.CommentCharacters",
"Label": "Comment Characters",
"HelpText": "**Optional** - Use this if you want to include the file name in your combined file as comments. You will need to specify the comment characters for the language of the file. For example \"--\" for SQL files, \"//\" for C# files.",
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Id": "a06deb13-1561-4df0-8aee-d73696841863",
"Name": "CombineFiles.Destination.CreateArtifact",
"Label": "Create Artifact",
"HelpText": "Indicates if the combined file should be uploaded to Octopus Deploy as an artifact. Useful when combining multiple SQL scripts into a single file so a DBA can review it.",
"DefaultValue": "True",
"DisplaySettings": {
"Octopus.ControlType": "Checkbox"
}
}
],
"Properties": {
"Octopus.Action.Script.ScriptSource": "Inline",
"Octopus.Action.Script.Syntax": "PowerShell",
"Octopus.Action.Script.ScriptBody": "$sourceDirectoryPath = $OctopusParameters[\"CombineFiles.Directory.Source\"]\n$sourceDirectoryPackagePath = $OctopusParameters[\"CombineFiles.Directory.PackageSource\"]\n$sourceDirectoryFilter = $OctopusParameters[\"CombineFiles.Directory.Filter\"]\n$destinationFile = $OctopusParameters[\"CombineFiles.Destination.FileName\"]\n$createArtifact = $OctopusParameters[\"CombineFiles.Destination.CreateArtifact\"]\n$commentCharacters = $OctopusParameters[\"CombineFiles.Destination.CommentCharacters\"]\n\nif ([string]::IsNullOrWhiteSpace($sourceDirectoryPackagePath) -eq $false){\n\tWrite-Host \"A previous package path was specified, grabing that\"\n\t$sourceDirectory = $OctopusParameters[\"Octopus.Action[$sourceDirectoryPackagePath].Output.Package.InstallationDirectoryPath\"]\n $sourceDirectory = \"$sourceDirectory\\$sourceDirectoryPath\"\n}\nelse {\n\t$sourceDirectory = \"$sourceDirectoryPath\"\n}\n\nWrite-Host \"Source Directory: $sourceDirectory\"\nWrite-Host \"Source File Filter: $sourceDirectoryFilter\"\nWrite-Host \"Combined File Name: $destinationFile\"\nWrite-Host \"Create Artifact: $createArtifact\"\nWrite-Host \"Comment Characters: $commentCharacters\"\n\nif ([string]::IsNullOrWhiteSpace($sourceDirectory)){\n\tthrow-exception \"The source directory variable is required.\"\n}\n\nif ((Test-Path $sourceDirectory) -eq $false){\n\tWrite-Host \"The directory $sourceDirectory was not found, skipping\"\n\texit 0\n}\n\nif ((Test-Path $destinationFile) -eq $false){\n\tWrite-Host \"Creating the file: $destinationFile\"\n\tNew-Item -Path $destinationFile -ItemType \"file\"\n}\nelse {\n\tWrite-Host \"The file $destinationFile already exists\"\n}\n\nif ([string]::IsNullOrWhiteSpace($sourceDirectoryFilter)){\n\tWrite-Host \"Source directory filter not specified, grabbing all files\"\n\t$filePath = \"$sourceDirectory\\*\"\n}\nelse {\n\tWrite-Host \"Source directory filter specified, grabbing filtered files\"\n\t$filePath = \"$sourceDirectory\\*\"\n}\n\nWrite-Host \"Getting child items using $filePath\"\n$filesToCombine = Get-ChildItem -Path $filePath -File | Sort-Object -Property Name\n\nforeach ($file in $filesToCombine)\n{\n\tWrite-Host \"Adding content to $changeScript from $file\"\n\n\tif ([string]::IsNullOrWhiteSpace($commentCharacters) -eq $false){ \n\t\tAdd-Content -Path $destinationFile -Value \"$commentCharacters Contents from $file\"\n }\n \n\tAdd-Content -Path $destinationFile -Value (Get-Content $file) \n} \n\nif ($createArtifact -eq \"True\"){\t \n New-OctopusArtifact -Path \"$destinationFile\"\n}"
},
"Category": "File System",
"HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates/file-system-combine-files.json",
"Website": "/step-templates/a1983aec-a8ca-4fda-b763-081fd0acecf8",
"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/9qP/+/P/99//7cA/7IA/7wA/6sA/+/A39/f/64A2tjX//Tfn5+f/7MA/7sA/74A/60A/9Zw/7gA/74Q/7oA/7UA/7EAi6g4fwAAAuRJREFUeNrs21tT2mAUheEvaaCQQCSczyi29WzP7f//Z6UdZBIIM44us9hf13vbC+eRbLo3qnNKKaWUUkoppZRSSimllFJKKaWUwlVrLhsvbrLqnoai2+yFr2x+fwKOwasZm/oXdEczxDQnOyYhqgn3uQpxDZhz3gdCesQ3r3mIjDfwXagj7NEgKywkXLMgSzCE9mz1wBDaOzDYETYEEUSQ/wPSaz6rQa174pDnt1x5Atm8emtPIGG48gUClJAh/XtPILi36+L3Zz6oVdBF4w32/sIbYmWH6qAPX5fzjio/14Q/W7nnqtIPDnYfFfThkGpPovXu68IhNdKSVyXkoQ7qgQypvwNVFwQDuXkP6oYM8WbYBWFDYMPNHnZBTv3tV8MuiCC+Q6K36+ypkn/LsJC4lQSkhhkQch4Qa+MgrYBahIKcB35AYrIjyEAQ9gsCm5E2GTIDQabsJysFQcZkxxj0H2LKfkGmIMjM3KgfgbBHfQSCROwnKwZByNtJsACt8WlChnRAkA7ZkaAOqyEZ0gJB7O2LRyAjsmOIutlNjnoJhD3qL9gXyyFjk6N+CKGPegSC2DsNj0DY++IlCEI/DWMQhL0vjh0GYvE0LIWwT8PEgSDsfXEEgmRmR30PYvI0LIGkRvfFAwj9NExBkIXRfXEfYvQ0PISwT8O2A0HYoz4DQb4ZPQ0PID8sj3oO8tXuvliEPJoe9Rzkk9XTcA9yZnhfLEA+mD0Ni5Ar9gvSAUE+2j0Ni5DPdk/DAuTW9L6Yg7BHfehAkC/WR30LoY96CoL8tL0v7iD0fTECQX4Z3xd3EPa+OANBflvfF58g363vi1sIfV+cgiCPPoz6Xwh71EcgyK0Xo76BsPfFBcbhrjzYF/9l/zTcxj4NWyBH7Mmo038zFvWCsCHt1HnxaCWZg8X8ifQwxjlcSvsJaHLpsGW5v2S8vo4qyymF7+5O3wOllFJKKaWUUkoppZRSSimllPKxPwIMAPj2YtijZbi5AAAAAElFTkSuQmCC",
"$Meta": {
"Type": "ActionTemplate"
}
}Provided under the Apache License version 2.0.