<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>

CC BY-SA 3.0
2017-12-13T08:12:38.930Z
2017-12-13T08:12:38.930Z
Yuri Zolotarev
<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>

CC BY-SA 3.0
2017-12-11T18:08:29.677Z
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?
Ilya Finkelsheyn
CC BY-SA 3.0
2017-12-11T18:13:51.673Z
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.
Yuri Zolotarev
CC BY-SA 3.0
2017-12-11T18:14:58.000Z
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.
Yuri Zolotarev
CC BY-SA 3.0
2017-12-11T18:27:13.513Z
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.
Yuri Zolotarev
CC BY-SA 3.0
2017-12-11T18:30:48.900Z
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.
Yuri Zolotarev
CC BY-SA 3.0
2017-12-11T19:07:02.193Z
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.
Ilya Finkelsheyn
<deployment><appveyor>
Best practices on how to erase everything in destination deployment folder and restart the app after deployment using Appveyor
Yuri Zolotarev
# 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.


---

