Build, Git Commit, FTP When You Publish In Visual Studio
For various reasons, I have a really weird build setup in Visual Studio. I just want to write this post so I can remember how it works now if I ever re-install my computer and something doesn’t work.
I use the regular publish feature in Visual Studio and deploy the code to a Website on my local IIS. This has always worked great and I don’t want to mess with firewalls and stuff to make it deploy to any remote site on my server. That just seems complicated.
My first modification to my .csproj file (that is more or less a regular MS Build file) is a command to open a text file where I write the message that I want to use for a git commit later on. The reason why I’m doing this is that I always forget to commit to Git and I couldn’t figure out any other way to force myself to enter something when I publish my app.
<Target Name="BeforeBuild"> <Exec Command="$(MSBuildProjectDirectory)/../commit-message.txt" /> </Target>
When I close notepad or the default text editor you use the build continues and when the build is complete I execute this command that runs a WinSCP script to sync my local folder with the remote folder on my server. You also see my second command to commit to Git there but ignore that for now.
<Target Name="CustomPostPublishActions" AfterTargets="MSDeployPublish"> <Exec Command="$(PathToWinSCP) /script=$(MSBuildProjectDirectory)/../winscp-upload.txt" /> <Exec Command=""C:WindowsSystem32WindowsPowerShellv1.0powershell.exe" -command "& {$(MSBuildProjectDirectory)/../git.ps1 '$(TargetPath)'}"" /> </Target>
The WinSCP script looks like this.
open serverbeach synchronize remote C:DeployAlternativeTo /alternativeto/ exit
This works incredibly well.
At last, I execute a PowerShell command to commit to GitHub using that message I wrote earlier. I use Posh-Git for PowerShell. My PowerShell script looks like this.
$message = Get-Content ../commit-message.txt git commit -a -m "$message"
Well, that’s it. I understand I don’t explain this in much detail but as I said before I think this scenario is very unique to me and my situation. But feel free to ask any questions if you have similar needs.