[vc_row][vc_column][vc_column_text]BY ERIC SCHRADER | POSTED ON MAY 17, 2017
We use Visual Studio Team Services for source code on a LAMP stack Azure VM. When deploying via VS TS and copying the files over SSH through VS TS, I had a few challenges to automate the build/deployment process. Here is how I set things up.
Check your files into source control (PHP files, web assets, etc.)
- I manually configured the deployment of 4 environment VMs for Dev, INT, STG, PRD using 4 instances of Azure Ubuntu Linux VMs.
- I manually deployed machine specific content, such as config files to the server. I later filter these files out of the deployment if they are in the web root. For security, I keep these files outside of the web root.
Create Build definition (one for each environment, DEV, INT, STG, PRD)
Use an Empty Process
- Under Get Sources, say This Project and chose the repository.
- Add a task to Copy files securely over SSH
- On the SSH endpoint, click the gear to configure your endpoints:
- Add SSH endpoints with your key file and IP, etc.
Select your endpoint and apply chose your web root from your project under Source Folder. Also, under Contents, apply any filters: (below, ** is everything, then we filter out files/folders using !**/)
I use the following filter examples:
I added two SSH commands to set permissions before/after the copy
- You can choose a Shell Script and provide environment specific variables, such as a user.
- I have my script in Source Control as well. This uses the same user as VS uses to overwrite files, then after the deployment, I use a second script to set my special permissions. The second script I will not post since it is specific to my application, and for security reasons. $1 is the argument I pass in for the user, who I set as owner recursively for all web files during deployment.
# Reset permissions before TFS deployment
echo “Reset permissions before TFS deployment”
if [ “$1” != “” ]; then
echo “Ready, Positional parameter 1 contains user $1”
echo “Resetting permissions to $1 for TFS deployment”
sudo chown -R “$1″:”$1” /home/s5/html
echo “Fail, Positional parameter 1 is empty. Please pass in the environments user”
- One important note, on Windows when I created the script as http://stackoverflow.com/questions/8195839/choose-newline-character-in-notepad) in NotePad++, you have to go to Edit -> EOL Conversion (thanks to this article
Otherwise, you will get the following error:
./ : line 2: $’\r’: command not found
./ : line 10: syntax error: unexpected end of file
Command failed with errors on remote machine.
That’s it, then save and queue a new build!
Much easier than copying files via FTP. Now I can click a button and update my application in each environment. Next steps are to automate the testing, release process.
More on SSH with Visual Studio Team Services https://www.visualstudio.com/en-us/docs/build/steps/deploy/ssh
Note: you can also have a build definition trigger the release definition to copy the files over SSH, etc. This is the way the Azure Portal sets up Continuous Delivery for Web Apps.