Answers: <p>So it actually only took one line of code:</p>
  
  <p>1) First take the SSH public key from the settings page inside the appveyor project and make sure you put it into the authorized_keys inside user_that_you_are_going_to_login_with_from_appveyor/.ssh/authorized_keys</p>
  
  <p>2) To clear the folder before deploying to it on a Ubuntu server</p>
  
  <pre><code>before_deploy:    
    ssh user@host -t "rm -r -v /pathtofolder/*;exit; bash --login"
  </code></pre>
  
  <p>3) Now about the restart of the app I wanted to use after_deploy however there is a problem with that. After deploy fires straight after deploy and doesn't wait untill upload is finished. I wanted to use it to run </p>
  
  <pre><code>systemctl restart myapp
  </code></pre>
  
  <p>However the problem seemed to solved itself because systemctl restarted my app at the moment when it was substituted with a new one or maybe at the moment when I call it the first time after the files have been reloaded. So far it seems I have a new app running after I run the script.</p>
  
Answers: CC BY-SA 3.0
Answers: 2017-12-13T08:12:38.930Z
Answers: 2017-12-13T08:12:38.930Z
Answers: Yuri Zolotarev
Body: <p>So far I have managed to get a simple deployment procedure running via SFTP. I really need to make two more steps:</p>
  
  <ol>
  <li>Erase everything inside the folder that I am deploying to before I deploy a fresh build into it or (even better not sure this is possible) see what has changed and upload only the required and erase the not required. </li>
  <li>Restart the app after deployment is done.</li>
  </ol>
  
  <p>What could be the best solution for this with Appveyor? So far I see the easiest way is to possibly include a power shell script that will connect via ssh and clear out the folder, not sure how that would work with restarting the app though. Would be really great to hear some more experienced users about this solution.</p>
  
Comments: CC BY-SA 3.0
Comments: 2017-12-11T18:08:29.677Z
Comments: There are number of approaches to this problem. To find more suitable one, please clarify the following: 1. What king of application is it? ASP.NET, ASP.NET Core or some other framework? 2. Do you have control over server or it is some kind of hosting? 3. What was rationale behind using SFTP and not other deployment method?
Comments: Ilya Finkelsheyn
Comments: CC BY-SA 3.0
Comments: 2017-12-11T18:13:51.673Z
Comments: ASP.NET Core, its my own server so full control, sftp seemed like the easiest way to me possibly because I don't know much about other ways that exist. The way I see it now is I only have 1 step left which is write a simple ps script that will connect before deploy clear out the directory and I'm done. However overall I get a feeling there could be a more appropriate approach. Thank you.
Comments: Yuri Zolotarev
Comments: CC BY-SA 3.0
Comments: 2017-12-11T18:14:58.000Z
Comments: Also, I'm pretty sure I won't be able to restart the app via ps so this is a big issue which actually fails the whole process right at the end unfortunatly where I have one little step to go.
Comments: Yuri Zolotarev
Comments: CC BY-SA 3.0
Comments: 2017-12-11T18:27:13.513Z
Comments: So probably best to go some other way straight from the beginning. Since I'm only starting my way in CI would really appreciate some advice on what would be the most appropriate path. Thank you.
Comments: Yuri Zolotarev
Comments: CC BY-SA 3.0
Comments: 2017-12-11T18:30:48.900Z
Comments: So overall its a ASP.NET Core WebApi App that I want to deploy to a Ubuntu 16.04 which I totally control. The server systemctl is set up to fire up the apps specific dll so all I have to do there is to make correction inside the directory or totally erase everything and put new files back inside and possible restart the app as well.
Comments: Yuri Zolotarev
Comments: CC BY-SA 3.0
Comments: 2017-12-11T19:07:02.193Z
Comments: You need to use  ‘before_deploy’ and ‘after_deploy’ scripts. To remotely execute something on Ubuntu, try to run your commands over SSH. I have no ready to use script, but the following link can help you:
  https://www.digitalocean.com/community/tutorials/how-to-use-ssh-to-connect-to-a-remote-server-in-ubuntu. Bush and SSH are installed on build workers and you can use them out of the box. Side note: use secure variables when storing credentials for your server.
Comments: Ilya Finkelsheyn
Tags: <deployment><appveyor>
Title: Best practices on how to erase everything in destination deployment folder and restart the app after deployment using Appveyor
UserDisplayName: Yuri Zolotarev
ThreadText: # Best practices on how to erase everything in destination deployment folder and restart the app after deployment using Appveyor
  
  (Asked by: username_0 on 2017-12-11)
  
  So far I have managed to get a simple deployment procedure running via SFTP. I really need to make two more steps:
  
  1. Erase everything inside the folder that I am deploying to before I deploy a fresh build into it or (even better not sure this is possible) see what has changed and upload only the required and erase the not required.
  2. Restart the app after deployment is done.
  
  What could be the best solution for this with Appveyor? So far I see the easiest way is to possibly include a power shell script that will connect via ssh and clear out the folder, not sure how that would work with restarting the app though. Would be really great to hear some more experienced users about this solution.
  
  **Tags:** deployment, appveyor
  
  > **Comments:**
  > **username_1**: There are number of approaches to this problem. To find more suitable one, please clarify the following: 1. What king of application is it? ASP.NET, ASP.NET Core or some other framework? 2. Do you have control over server or it is some kind of hosting? 3. What was rationale behind using SFTP and not other deployment method?  
  > **username_0**: ASP.NET Core, its my own server so full control, sftp seemed like the easiest way to me possibly because I don't know much about other ways that exist. The way I see it now is I only have 1 step left which is write a simple ps script that will connect before deploy clear out the directory and I'm done. However overall I get a feeling there could be a more appropriate approach. Thank you.  
  > **username_0**: Also, I'm pretty sure I won't be able to restart the app via ps so this is a big issue which actually fails the whole process right at the end unfortunatly where I have one little step to go.  
  > **username_0**: So probably best to go some other way straight from the beginning. Since I'm only starting my way in CI would really appreciate some advice on what would be the most appropriate path. Thank you.  
  > **username_0**: So overall its a ASP.NET Core WebApi App that I want to deploy to a Ubuntu 16.04 which I totally control. The server systemctl is set up to fire up the apps specific dll so all I have to do there is to make correction inside the directory or totally erase everything and put new files back inside and possible restart the app as well.  
  > **username_1**: You need to use  ‘before_deploy’ and ‘after_deploy’ scripts. To remotely execute something on Ubuntu, try to run your commands over SSH. I have no ready to use script, but the following link can help you: https://www.digitalocean.com/community/tutorials/how-to-use-ssh-to-connect-to-a-remote-server-in-ubuntu. Bush and SSH are installed on build workers and you can use them out of the box. Side note: use secure variables when storing credentials for your server.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2017-12-13. Score: 1
  
  So it actually only took one line of code:
  
  1) First take the SSH public key from the settings page inside the appveyor project and make sure you put it into the authorized\_keys inside user\_that\_you\_are\_going\_to\_login\_with\_from\_appveyor/.ssh/authorized\_keys
  
  2) To clear the folder before deploying to it on a Ubuntu server
  
  ```
  before_deploy:    
    ssh user@host -t "rm -r -v /pathtofolder/*;exit; bash --login"
  
  ```
  
  3) Now about the restart of the app I wanted to use after\_deploy however there is a problem with that. After deploy fires straight after deploy and doesn't wait untill upload is finished. I wanted to use it to run
  
  ```
  systemctl restart myapp
  
  ```
  
  However the problem seemed to solved itself because systemctl restarted my app at the moment when it was substituted with a new one or maybe at the moment when I call it the first time after the files have been reloaded. So far it seems I have a new app running after I run the script.
  
  
  ---
  
  