'How to get Jenkins to perform deletes on remote server

I am trying to get started with an automated build process using Jenkins.

So far i have managed to get Jenkins to build a project from BitBucket, run tests and upload the files to a production server using the Publish Over SSH plugin.

When i delete files from my git repo, i would like those files to be deleted on the remote server. There are a few ways that i have thought about doing this already.

  1. Wipe all the files on the production server, then upload the latest build release.

    • seems like a bad solution for me, as this would cause more downtime than i would like.
  2. Create a script that lives on the remote server to clean up the files after the new build process. I would need to find out from git (somehow?) what the deleted files on that commit were.

  3. Instead of having Jenkin use the publish over ssh plugin, have git installed on the production server, so after a successful build, we can get Jenkins (somehow?) to push the project to the git production server.

This seems like a common task for developers, and i feel like i am missing something here. Is there any standard practice for this.



Solution 1:[1]

Cleaning the remote directory before deployment

I was able to do it in the following way:

  1. In the Job configuration in the Build Environment section, check Send files or execute commands over SSH before the build starts.
  2. Delete the files of the previous version of the build on the remote server:
sudo rm -r /var/www/html/*

Solution 2:[2]

Maybe it is too late this answer, but I think other people search for this. I found a solution to delete file on the production server after you delete them in development.
In Jenkins, you use rsync to synchronize files between dev and production. add to this line at the end --delete
and this will do the trick

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 tol64
Solution 2 Raul_26