title: Files deleted or modified between git revisions are automatically getting deleted from instances
body: <p><strong>Background</strong> </p>
  
  <p>I have a setup triggered by Jenkins with the following -</p>
  
  <ul>
  <li>The files to be deployed are prepared by phing, by talking to git server and taking a <code>git diff</code> between the required git revisions, in a separate build server, without the involvement of AWS code deploy (as far as I think). The phing build is triggered by Jenkins.</li>
  <li>I add only the files to be added/modified (based on the git difference of revisions) dynamically to the appspec.yml file. I prepare only the files to be added/modified to a path <code>/home/jenkins/deployment/cd_deploy/codebase/</code> and I have specified the path <code>/home/jenkins/deployment/cd_deploy/</code> under "Use custom workspace" option under "Advanced Project Options" of Jenkins project which is basically the location in the build server which needs to be uploaded to the S3 bucket. <strong><em>Note that I would need to delete the files from the instances that are deleted between the two git revisions.</em></strong></li>
  <li>Jenkins then triggers AWS Codedeploy with the information about the application name, deployment group of Code deploy that I have configured.</li>
  </ul>
  
  <p><strong>Problem</strong></p>
  
  <p>The files that I dynamically add to the appspec.yml file are getting modified/added in the EC2 instances, as I expect, however, strangely, the files that are to be deleted are also getting deleted. I verified that I have no logic to delete those files written in the beforeInstall hook of my appspec file. I have only a beforeInstall.sh file in my beforeInstall hook, and no other hook. As soon as I remove that hook from the appspec file, the deletion stops. Here is my appspec file - </p>
  
  <pre><code>version: 0.0
  os: linux
  files:
  {Pair of files dynamically generated}
    - source: config/deployment_config.json
      destination: /var/cake_1.2.0.6311-beta/deployment
  permissions:
    - object: .
      pattern: "**"
      owner: sandeepan
      group: sandeepan
      mode: 777
      type:
        - file
  hooks:
    BeforeInstall:
      - location: beforeInstall.sh
  </code></pre>
  
  <p>Is AWS Codedeploy somehow talking to my git hosting (I am using gitlab and not even github) and somehow getting the information about the files to be deleted.</p>
  
  <p><strong>Update</strong></p>
  
  <p>I later observed that even after removing the hooks section completely from the appspec.yml file, and deleting the corresponding .sh files, i.e. beforeInstall.sh, afterInstall.sh etc from the central build server (where the S3 bundle is prepared), so that none of my logic and any reference to it is going to the instances, the files that are to be deleted are still getting deleted automatically.</p>
  
  <p><strong>Update 2</strong></p>
  
  <p><strong><em>Today I found that the files that are modified in between git revisions are also getting deleted automatically.</em></strong> 
  I had logic to dynamically prepare the appspec.yml file. I modified to not add some files. So, there were some files which were there in the git diff, but were not there in the appspec file. As a result, they are getting deleted but not reappearing. <strong>Code deploy is automatically doing a cleanup before the deployment, it seems. How do I stop that?</strong> I would like to add my custom cleanup logic.</p>
  
  <p><strong>Update 3</strong></p>
  
  <p>Content of beforeInstall.sh -</p>
  
  <pre><code>OUTPUT="$(w | grep -Po '(?&lt;=load average: )[^,]*')"
  rm -f /var/cake_1.2.0.6311-beta/deployment/deployment_config.json
  path="$PWD"
  php $path"/deployment-root/"$DEPLOYMENT_GROUP_ID"/"$DEPLOYMENT_ID"/deployment-archive/beforeInstall.php" ${OUTPUT}
  
  /usr/local/nagios/libexec/check_logwarn -d /tmp/logwarn_hiphop_error /mnt/log/hiphop/error_`(date +'%Y%m%d')`.log #Just run a nagios check, so that counter corresponds to the line in the log corresponding to current timestamp/instant. Do not care about output. Note that we are not even looking for error hinting keywords (and hence not using -p because it needs to be used alongwith), because all we need to care about here is incrementing the nginx counter.
  
  /usr/local/nagios/libexec/check_logwarn -d /tmp/logwarn_nginx_access /mnt/log/nginx/access_`(date +'%Y%m%d')`_`(date +'%H')`.log #Just run a nagios check, so that counter corresponds to the line in the log corresponding to current timestamp/instant. Acceptable http codes are also not being read from deployment_config.json.
  printf "\n `date +%Y-%m-%d:%H:%M:%S` End of beforeInstall.sh"  &gt;&gt; /var/cake_1.2.0.6311-beta/deployment/deployment.log
  exit 0
  </code></pre>
  
  <p>And content of beforeInstall.php which is called from the above - </p>
  
  <pre><code>&lt;?php 
  file_put_contents('/var/cake_1.2.0.6311-beta/deployment/deployment.log', "\n ".date("Y-m-d H:i:s")." - Load print  ".$argv[1], FILE_APPEND);
  $loadData = json_encode(array("load" =&gt; intval($argv[1]), "access_error_check_day" =&gt; date("Ymd"), "access_error_check_hour" =&gt; date("H"))); //error_check_day -&gt; day when nagios error check was last run. We will accordingly check log files of days in between this day and the day of afterinstall (practically this can include a span of 2 days).
  
  file_put_contents("/var/cake_1.2.0.6311-beta/deployment/serverLoad.json",$loadData); //separate from deployment_config.json. serverLoad.json is not copied from build server.
  file_put_contents('/var/cake_1.2.0.6311-beta/deployment/deployment.log', "\n ".date("Y-m-d H:i:s")." loadData to config ".$loadData, FILE_APPEND);
  ?&gt;
  </code></pre>