-
Notifications
You must be signed in to change notification settings - Fork 506
Expand file tree
/
Copy pathsql-execute-sql-files.json
More file actions
130 lines (130 loc) · 12.2 KB
/
sql-execute-sql-files.json
File metadata and controls
130 lines (130 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
{
"Id": "2bd3b8ef-35b4-43e9-b6de-8e0c515f3f10",
"Name": "SQL - Execute SQL Script Files",
"Description": "Executes SQL script file(s) against the specified database using the `SQLServer` Powershell Module. This template includes an `Authentication` selector and supports SQL Authentication, Windows Authentication, and Azure Managed Identity.\n\nNote: If the `SqlServer` PowerShell module is not present, the template will download a temporary copy to perform the task.",
"ActionType": "Octopus.Script",
"Version": 7,
"CommunityActionTemplateId": null,
"Packages": [
{
"Id": "8473acaf-aaeb-4c23-923a-91f664290f16",
"Name": "template.Package",
"PackageId": null,
"FeedId": null,
"AcquisitionLocation": "Server",
"Properties": {
"Extract": "True",
"SelectionMode": "deferred",
"PackageParameterName": "template.Package",
"Purpose": ""
}
}
],
"Properties": {
"Octopus.Action.Script.ScriptBody": "\nfunction Get-ModuleInstalled {\n # Define parameters\n param(\n $PowerShellModuleName\n )\n\n # Check to see if the module is installed\n if ($null -ne (Get-Module -ListAvailable -Name $PowerShellModuleName)) {\n # It is installed\n return $true\n }\n else {\n # Module not installed\n return $false \n }\n}\n\nfunction Get-NugetPackageProviderNotInstalled {\n # See if the nuget package provider has been installed\n return ($null -eq (Get-PackageProvider -ListAvailable -Name Nuget -ErrorAction SilentlyContinue))\n}\n\nfunction Install-PowerShellModule {\n # Define parameters\n param(\n $PowerShellModuleName,\n $LocalModulesPath\n )\n \n # Set TLS order\n [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls11 -bor [System.Net.SecurityProtocolType]::Tls12\n\n # Check to see if the package provider has been installed\n if ((Get-NugetPackageProviderNotInstalled) -ne $false) {\n # Display that we need the nuget package provider\n Write-Output \"Nuget package provider not found, installing ...\"\n \n # Install Nuget package provider\n Install-PackageProvider -Name Nuget -Force\n }\n\n # Save the module in the temporary location\n Save-Module -Name $PowerShellModuleName -Path $LocalModulesPath -Force\n}\n\n\nfunction Invoke-ExecuteSQLScript {\n\n [CmdletBinding()]\n param\n (\n [parameter(Mandatory = $true, Position = 0)]\n [ValidateNotNullOrEmpty()]\n [string]\n $serverInstance,\n\n [parameter(Mandatory = $true, Position = 1)]\n [ValidateNotNullOrEmpty()]\n [string]\n $dbName,\n\n [string]\n $Authentication,\n\n [string]\n $SQLScripts,\n\n [bool]\n $DisplaySqlServerOutput,\n \n [bool]\n $TrustServerCertificate\n )\n \n # Check to see if SqlServer module is installed\n if ((Get-ModuleInstalled -PowerShellModuleName \"SqlServer\") -ne $true) {\n # Display message\n Write-Output \"PowerShell module SqlServer not present, downloading temporary copy ...\"\n\n # Download and install temporary copy\n Install-PowerShellModule -PowerShellModuleName \"SqlServer\" -LocalModulesPath $LocalModules\n }\n\n # Display\n Write-Output \"Importing module SqlServer ...\"\n\n # Import the module\n Import-Module -Name \"SqlServer\"\n \n $ExtractedPackageLocation = $($OctopusParameters['Octopus.Action.Package[template.Package].ExtractedPath'])\n\n $matchingScripts = @()\n\n # 1. Locate matching scripts\n foreach ($SQLScript in $SQLScripts.Split(\"`n\", [System.StringSplitOptions]::RemoveEmptyEntries)) {\n try {\n \n Write-Verbose \"Searching for scripts matching '$($SQLScript)'\"\n $scripts = @()\n $parent = Split-Path -Path $SQLScript -Parent\n $leaf = Split-Path -Path $SQLScript -Leaf\n Write-Verbose \"Parent: '$parent', Leaf: '$leaf'\"\n if (-not [string]::IsNullOrWhiteSpace($parent)) {\n $path = Join-Path $ExtractedPackageLocation $parent\n if (Test-Path $path) {\n Write-Verbose \"Searching for items in '$path' matching '$leaf'\"\n $scripts += @(Get-ChildItem -Path $path -Filter $leaf)\n }\n else {\n Write-Warning \"Path '$path' not found. Please check the path exists, and is relative to the package contents.\"\n }\n }\n else {\n Write-Verbose \"Searching in root of package for '$leaf'\"\n $scripts += @(Get-ChildItem -Path $ExtractedPackageLocation -Filter $leaf)\n }\n \n Write-Output \"Found $($scripts.Count) SQL scripts matching input '$SQLScript'\"\n\n $matchingScripts += $scripts\n }\n catch {\n Write-Error $_.Exception\n }\n }\n \n # Create arguments hash table\n $sqlcmdArguments = @{}\n\n\t# Add bound parameters\n $sqlcmdArguments.Add(\"ServerInstance\", $serverInstance)\n $sqlcmdArguments.Add(\"Database\", $dbName)\n #$sqlcmdArguments.Add(\"Query\", $SQLScripts)\n \n if ($DisplaySqlServerOutput)\n {\n \tWrite-Host \"Adding Verbose to argument list to display output ...\"\n $sqlcmdArguments.Add(\"Verbose\", $DisplaySqlServerOutput)\n }\n \n if ($TrustServerCertificate)\n {\n \t$sqlcmdArguments.Add(\"TrustServerCertificate\", $TrustServerCertificate)\n }\n\n # Execute based on selected authentication method\n switch ($Authentication) {\n \"AzureADManaged\" {\n # Get login token\n Write-Verbose \"Authenticating with Azure Managed Identity ...\"\n \n $response = Invoke-WebRequest -Uri 'http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https%3A%2F%2Fdatabase.windows.net%2F' -Method GET -Headers @{Metadata = \"true\" } -UseBasicParsing\n $content = $response.Content | ConvertFrom-Json\n $AccessToken = $content.access_token\n \n $sqlcmdArguments.Add(\"AccessToken\", $AccessToken)\n\n break\n }\n \"SqlAuthentication\" {\n Write-Verbose \"Authentication with SQL Authentication ...\"\n $sqlcmdArguments.Add(\"Username\", $username)\n $sqlcmdArguments.Add(\"Password\", $password)\n\n break\n }\n \"WindowsIntegrated\" {\n Write-Verbose \"Authenticating with Windows Authentication ...\"\n break\n }\n } \n\n # Only execute if we have matching scripts\n if ($matchingScripts.Count -gt 0) {\n foreach ($script in $matchingScripts) {\n $sr = New-Object System.IO.StreamReader($script.FullName)\n $scriptContent = $sr.ReadToEnd()\n $cmdArguments = @{}\n $cmdArguments += $sqlcmdArguments\n \n $cmdArguments.Add(\"Query\", $scriptContent)\n \n # Invoke sql cmd\n Invoke-SqlCmd @cmdArguments\n \n $sr.Close()\n\n Write-Verbose (\"Executed manual script - {0}\" -f $script.Name)\n }\n }\n}\n\n# Define PowerShell Modules path\n$LocalModules = (New-Item \"$PSScriptRoot\\Modules\" -ItemType Directory -Force).FullName\n$env:PSModulePath = \"$LocalModules$([System.IO.Path]::PathSeparator)$env:PSModulePath\"\n\nif (Test-Path Variable:OctopusParameters) {\n Write-Verbose \"Locating scripts from the literal entry of Octopus Parameter SQLScripts\"\n $ScriptsToExecute = $OctopusParameters[\"SQLScripts\"]\n $DisplaySqlServerOutput = $OctopusParameters[\"ExecuteSQL.DisplaySQLServerOutput\"] -ieq \"True\"\n $TemplateTrustServerCertificate = [System.Convert]::ToBoolean($OctopusParameters[\"ExecuteSQL.TrustServerCertificate\"])\n \n Invoke-ExecuteSQLScript -serverInstance $OctopusParameters[\"serverInstance\"] `\n -dbName $OctopusParameters[\"dbName\"] `\n -Authentication $OctopusParameters[\"Authentication\"] `\n -SQLScripts $ScriptsToExecute `\n -DisplaySqlServerOutput $DisplaySqlServerOutput `\n -TrustServerCertificate $TemplateTrustServerCertificate\n}",
"Octopus.Action.Script.Syntax": "PowerShell",
"Octopus.Action.Script.ScriptSource": "Inline",
"Octopus.Action.RunOnServer": "false"
},
"Parameters": [
{
"Id": "1f2b60c9-b85c-4c23-a313-fc18e82cd500",
"Name": "serverInstance",
"Label": "Server Instance Name",
"HelpText": "The SQL Server Instance name",
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Id": "9884b8c1-01a0-4c6f-97b1-ff5146fa0836",
"Name": "dbName",
"Label": "Database Name",
"HelpText": "The database name",
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Id": "05dc20d9-f75c-4971-9efb-e9aaad82a3a9",
"Name": "Authentication",
"Label": "Authentication",
"HelpText": "The authentication method",
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "Select",
"Octopus.SelectOptions": "SqlAuthentication|SQL Authentication\nWindowsIntegrated|Windows Integrated\nAzureADManaged|Azure Active Directory Managed Identity"
}
},
{
"Id": "4c2fc1b4-bdd0-4a9a-adb1-da1e818e62bc",
"Name": "Username",
"Label": "Username",
"HelpText": "The username to use to connect (only applies with SqlAuthentication selected)",
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Id": "819f9b19-042d-42d8-9d19-5f5bf28c06b7",
"Name": "Password",
"Label": "Password",
"HelpText": "The password to use to connect (only applies with SqlAuthentication selected)",
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "Sensitive"
}
},
{
"Id": "dd22f955-8317-4d58-8173-fc7d44df1192",
"Name": "SQLScripts",
"Label": "SQL Scripts",
"HelpText": "Provide the path to search for matching scripts, each one on a new line. Wildcards for filenames only are accepted, e.g.\n- `/Scripts/*.sql`\n- `/Scripts/SQL/Deploy*.sql`\n- `src/Permissions/Pre*Permissions.sql`\n\n**Please Note:** The step looks for files relative to the extracted package location, and does *not* recursively search the folder hierarchy.",
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "MultiLineText"
}
},
{
"Id": "3bfed638-6649-438f-a02b-353e36a63c87",
"Name": "template.Package",
"Label": "Package",
"HelpText": "Package containing the SQL scripts to be executed.",
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "Package"
}
},
{
"Id": "c6b85a12-bf2f-4963-8526-ebbe8f14707d",
"Name": "ExecuteSQL.DisplaySQLServerOutput",
"Label": "Display SQL Output",
"HelpText": "You can display SQL Server message output, such as those that result from the SQL `PRINT` statement, by checking this parameter",
"DefaultValue": "False",
"DisplaySettings": {
"Octopus.ControlType": "Checkbox"
}
},
{
"Id": "52d8f897-696b-4f77-87b5-383d6ce559c3",
"Name": "ExecuteSQL.TrustServerCertificate",
"Label": "Trust Server Certificate",
"HelpText": "Force connection to trust the server certificate.",
"DefaultValue": "False",
"DisplaySettings": {
"Octopus.ControlType": "Checkbox"
}
}
],
"StepPackageId": "Octopus.Script",
"$Meta": {
"ExportedAt": "2026-04-09T00:26:48.778Z",
"OctopusVersion": "2026.1.11319",
"Type": "ActionTemplate"
},
"LastModifiedBy": "twerthi",
"Category": "sql"
}