First step, let’s define the parameters for the script with some default values:
param (
[string]$solution = "OracleDashboard.sln",
[string]$zipname = "OracleDashboard.zip",
[string]$compressor = "c:\Program Files\7-Zip\7z.exe",
[string]$folder = "OracleDashboard",
[string]$deployPath = "..\Binary",
[string]$commitFrom = "..",
[Parameter(Mandatory=$true)][string]$comment
)
Some description of these parameters:
$solution
= path of the solution file relative to the script location $zipname
= name of the zip file $compressor
= 7-zip’s 7z.exe file path $folder
= the folder that contains the code, which is zipped $deployPath
= relative path where the zip file will be moved to $commitFrom
= relative path where the script will run git commit and git push from $comment
= A comment for the git commit
The first thing the script does is look for the solution open in Visual Studio and close it. You can comment this section out if you want. But if Visual Studio is open, then /obj folder cannot be deleted.
$windowTitle = $solution.Replace(".sln", "")
$vsProcess = get-process | where
{$_.mainwindowtitle -match $windowTitle -and $_.ProcessName -eq "devenv"}
if ($vsProcess.Length -gt 0) {
Write-Host "Visual Studio has this solution open. Closing..."
$vsProcess | ForEach-Object { $_.CloseMainWindow(); }
Sleep 5
Read-Host "Press ENTER to proceed if Visual Studio is closed"
$vsProcess = get-process | where {$_.mainwindowtitle -match $windowTitle
-and $_.ProcessName -eq "devenv"}
if ($vsProcess.Length -gt 0) {
Write-Host "Visual Studio still has the solution open. Aborting."
Return
}
}
Next step is to do some spring cleaning:
Push-Location
if (Test-Path $zipname) { rm $zipname; }
rm $deployPath\*.* -Force -Recurse
First remember the current path. We have to come back to this path after we are done. Then remove the zip file if it already exists. Then cleanup the $deployPath
. You can remove this if you want to keep old deployment packages. Then, you have to handle generation of unique file names for the packages.
Now, let’s build and remove /obj folder:
msbuild /verbosity:minimal $solution
if (Test-Path $folder\obj) { rm $folder\obj -Force -Recurse }
Next step: Remove all sensitive information from the web.config, which includes connection strings, authorization block, appSettings entries, etc. This is all up to you to customize:
[string]$filename = gi $folder\web.config
[string]$backup = [System.IO.File]::ReadAllText($filename)
$xml =
$backup $xml.PreserveWhitespace = $true foreach($n in $xml.configuration.connectionStrings.add)
{ $n.ParentNode.RemoveChild($n); }
foreach($n in $xml.configuration.appSettings.add) { switch($n.key)
{ "Password" { $n.value = "Password" } } }
$xml.Save($filename)
Finally, let’s run some regular expression check to ensure the web.config does not have any sensitive information left accidentally. Again, this is all up to you to customize.
[string]$config = gc $folder\web.config
if ( ($config -match 'connectionString="\w+') -or ($config -match 'users="\w+') ) {
Write-Host "Configuration file is not cleaned."
[System.IO.File]::WriteAllText($filename, $backup)
Exit
}
Now, it's time to compress the source folder and create a zip file using 7-zip.
cmd /c $compressor a -tzip $zipname $folder -r
cmd /c copy $zipname $deployPath /Y
cmd /c del $zipname
Finally, git commit and push:
cd $commitFrom
git pull
git add -A *.*
git commit -a -m $comment
git push
Pop-Location
And the last step is to restore your own web.config, that was anonymized:
[System.IO.File]::WriteAllText($filename, $backup)
That’s it. Now all you have to do is, just hit ./gitpush.ps1 from Powershell command line and you are done!