appspec.yml failed to call scripts

2019-09-11 08:48发布

问题:

I am trying to setup CI using AWS CodeDeploy and CircleCI. Right now I am stuck at the step where AWS CodeDeploy should copy stuff into EC2 and run scripts. But somehow CircleCI tells me something is wrong. Does anyone know what might be happening? Thanks.

the appspec.yml is:

version: 0.0
os: linux
files:
  - source: /
    destination: /home/ubuntu
hooks:
  BeforeInstall:
    - location: scripts/setup.sh
      timeout: 3800
      runas: root
  ApplicationStart:
    - location: scripts/start.sh
      timeout: 3800
      runas: root

and setup.sh is:

#!/bin/bash
sudo apt-get install nodejs npm
npm install

in the above code I also tried only apt-get install nodejs npm but it's still nor working.

the error message in /var/log/aws/codedeploy-agent/codedeploy-agent.log is as follows:

2015-10-22 08:02:54 ERROR [codedeploy-agent(1314)]: InstanceAgent::Plugins::CodeDeployPlugin::CommandPoller: Error during 
 perform: InstanceAgent::Plugins::CodeDeployPlugin::ScriptError - Script at specified location:
./scripts/setup.sh run as user root failed with exit code 127 - /opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/hook_executor.rb:150:in `execute_script'
/opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/hook_executor.rb:107:in `block (2 levels) in execute'
......

回答1:

Exit code 127 generally means that the OS couldn't find something required to execute the command. In this case it could be either the script wasn't at the expected path or /bin/bash doesn't exist (unlikely).

Check that the archive being produced by your build process is actually putting your scripts in the archive where your appspec expects them. scripts/setup.sh needs to be in that exact path within your archive.

You can also look at what the agent actually got by checking the deployment archive for your deployment: /opt/codedeploy-agent/deployment-root/deployment-group-id/deployment-id/deployment-archive to make sure the archive is being extracted correctly.



回答2:

Do the following steps for the debugging:

  • in the CodeDeploy error log /var/log/aws/codedeploy-agent/codedeploy-agent.log there is a line that says Error during perform: InstanceAgent::Plugins::CodeDeployPlugin::ScriptError - Script at specified location: scripts/setup.sh failed with exit code 1. So from the error log I know the problems might be from this script.

  • In the above mentioned script setup.sh, put something like this at the beginning of the script:

exec 3>&1 4>&2 trap 'exec 2>&4 1>&3' 0 1 2 3 exec 1>/home/ubuntu/out.log 2>&1

This logs the entire error outputs for you.

  • Permission issues

It's also possible that EC2 failed to execute those scripts, you need to make sure those files have at least 755 permissions when copied to your instance. So you need to specify 755 file mode for your scripts.

How to change the File Mode on GitHub?

Also in appspec.yml you need can specify a runas directive. Could be ubuntu or root or whatever that gives you the correct permission.

  • miscellaneous

Some pitfalls on deploying like when you do sudo apt-get install nodejs there will be intermediate steps that ask if you want to install packages and used disk spaces and you have to type Y or N to proceed installation. those scripts would hang there and timeout resulting in failed deployment. So instead you do

sudo apt-get -y install nodejs npm

Or in your setup.sh script maybe you have

chmod -R 777 public

but it's possible CodeDeploy is executing this code in a folder that's different than your project root. So make sure all the paths are valid.