I am trying to use AWS CodeDeploy to pull my latest changes from Github down to a server. The problem I am running into is on the install step I am getting this error:
Error CodeUnknownError
Script Name
MessageFile already exists at location /data/sites/wordpress/api_now_playing_staging.php
Log Tail
And my appspec.yml looks like this:
version: 0.0
os: "linux"
files:
- source: "/"
destination: "/data/sites/wordpress"
permissions:
- object: /data/sites/wordpress
pattern: "**"
owner: wp
group: nginx
mode: 755
type:
- file
My question is if it git is suppose to be pulling using CodeDeploy, why am I getting file already exist error? Am I doing something wrong?
Did you deploy the same git repo with a different deployment group or did it manually before? If the same resource is already existed in the destination folder, CodeDeploy will check whether the resource in destination folder is created by the same deployment group. If you you are using the same deployment group, re-deploy shouldn't cause this problem.
The current way CodeDeploy host agent works is to pull down the deployment artifacts and move them to the right repository according to AppSpec file. Even you deploy from a Github repo, it's not just running git pull in the destination folder.
All the other answers are right but in my opinion, fail to solve your problem. They are however a good starting point.
(short: codedeploy sees a file which wasn't presumable there in the previous codedeploy)
This is solvable during a deployment: "Content options:
Choose the action for AWS CodeDeploy to take during a deployment when a file on a target instance has the same name as a file in the application revision, for the same target location."
You can choose fail, overwrite and retain. Overwrite is probably the best choice in your case.
You can't find more information
Since the file already exists in the server, CodeDeploy has hooks (BeforeInstall) which can be used to run custom scripts to cleanup existing files, as codedeploy Install does not override files if already exists.
I made it work like this:
I had a couple of failed deployments for various reasons. The thing is that CD keeps in the EC2 instance and in the path /opt/codedeploy-agent/deployment-root/ a folder named by the ID of the failed deployment [a very long alphanumeric sting] . Delete this folder and create a new deployment [from the aws UI console] and redeploy the application. It should now succeed.
Notice: CD does not rewrite files [that have not been created by it's specific deployment]
CodeDeploy does not deploy in a folder that there is already code[files] as it does not want to interfere with different CD deployments and/or other CI/CD tools [like Jenkins]. It only deploys in a path that has already deploy code with the specific deployment.
You can empty the folder where your deployment want to happen and redeploy your code via CD.