Skip to content

Instantly share code, notes, and snippets.

@oazabir
Created September 29, 2014 12:11
Show Gist options
  • Save oazabir/8e3187b5756b25b52393 to your computer and use it in GitHub Desktop.
Save oazabir/8e3187b5756b25b52393 to your computer and use it in GitHub Desktop.
Build, deploy, anonymize config, zip package, git commit, push from a single command
# Build, anonymize config, deploy, zip, commit, push in one shot
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
)
# If visual studio has the solution open, close VS, as we can't delete obj folder while it is open
$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
}
}
Push-Location
if (Test-Path $zipname) { rm $zipname; }
# Clean up deploy folder
rm $deployPath\*.* -Force -Recurse
# Build new version
msbuild /verbosity:minimal $solution
# Delete obj
if (Test-Path $folder\obj) { rm $folder\obj -Force -Recurse }
# backup the web.config and remove sensitive entries before pushing to git, eg connectionString
[string]$filename = gi $folder\web.config
[string]$backup = [System.IO.File]::ReadAllText($filename)
$xml = [xml]$backup
$xml.PreserveWhitespace = $true
foreach($n in $xml.configuration.connectionStrings.add)
{
$n.ParentNode.RemoveChild($n);
}
# Anonymize any sensitive appSettings entry
foreach($n in $xml.configuration.appSettings.add)
{
switch($n.key)
{
"Password" { $n.value = "Password" }
}
}
# Remove authorization blocks
$xml.configuration.'system.web'.authorization.RemoveAll()
$xml.Save($filename)
# verify if web.config still contains any sensitive info
[string]$config = gc $folder\web.config
if ( ($config -match 'connectionString="\w+') -or ($config -match 'users="\w+') ) {
Write-Host "Configuration file is not cleaned."
# Restore web.config
[System.IO.File]::WriteAllText($filename, $backup)
Exit
}
# Compress the solution folder and copy to deploy folder
cmd /c $compressor a -tzip $zipname $folder -r
cmd /c copy $zipname $deployPath /Y
cmd /c del $zipname
# Commit and push to GitHub
cd $commitFrom
git pull
git add -A *.*
git commit -a -m $comment
git push
Pop-Location
# Restore web.config
[System.IO.File]::WriteAllText($filename, $backup)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment