Search code examples
wordpressgitamazon-web-servicesamazon-ec2aws-code-deploy

AWS CodeDeploy: How to stop it from deleting files?


We have a CodePipeline set up which uses CodeDeploy to deploy the latest updates from our repository on GitHub to an EC2 instance. This works fine, except for one issue: everything we have in our .gitignore file is deleted from the server whenever a deployment is performed.

For instance, this is a WordPress site, so we have wp-config.php and wp-content/uploads excluded from the repository. When a deployment runs, it deletes these files rendering the site unusable.

Our desired behavior is for CodeDeploy to overwrite existing files, but also ignore any files/directories not included in the repository so they can remain untouched. By default there seems to be a step that "clears out" the deployment destination before adding the new files, but we need to skip that.

Is there any setting, either in the console or appspec.yml, which will allow us to make deployments without having anything deleted? It seems like this would be a very common use case...if we can't make deployments like this then I'll have to just do all our updates via SFTP, which is pretty lame.


Solution

  • We have a WordPress implementation, and assume CodeDeploy will remove all files and replace them with the deployment package. This is its standard behavior, and I am pretty certain you cannot change that. It will want to sync the local file system with the deployment package you have provided.

    For this reason, consider moving the upload directory outside of the document root to account for this. Check out https://premium.wpmudev.org/blog/change-default-wordpress-uploads-folder/

    Regarding files, we moved the upload folder to /var/files, and mounted that as a EFS volume. This provides you with better durability, and makes the file system independent of any given instance.

    Also you should check in all files like wp-config.php on to the repo, for the same reasons - if you do not include it, then it will not be deployed.

    With this approach we can easily replace instances via autoscalling. You may only have one instance at this time, but at some point you will want to scale.

    But to answer the question directly:

    Yes CodeDeploy can be configured so that files are retained the way you require.

    You would implement a lifecycle hook script, where beforeInstall the reserved files are moved to /tmp, then the afterinstall hook would move them back. extra overhead for a deploy, which is why I suggest the above approach.

    See https://docs.aws.amazon.com/codedeploy/latest/userguide/reference-appspec-file-example.html