10

I have an AWS with two instances. I have configured CodeDeploy to deploy my project automatically on all instances.

In the appspec.yml I have that section:

hooks:
   AfterInstall:
     - location: codedeploy_scripts/deploy_afterinstall
       timeout: 300
       runas: root

deploy_afterinstall is a simple bash script. Sometimes some of commands in it fail. For example this command which updates/installs composer dependencies.

if [ -f "composer.lock" ]; then
   composer update -n
else
   composer install -n
fi

But CodeDeploy ignores any errors in this script and always says that deployment went successfully. How can I change this behaviour? I want deployment to fail when some of the commands in the hook haven't been finished successfully and to see the errors in the deployment console or log.

Stalinko
  • 3,319
  • 28
  • 31

2 Answers2

13

I ran into similar problems with CodeDeploy initially. I would recommend making your bash scripts more strict:

#!/bin/bash
set -euo pipefail

By setting e, u, and o pipefail as options Bash will behave more like a programming language and less like a script. You can read more about "Bash Strict Mode" here.

When your composer install or update fails, Bash will then exit with a non-zero code and the code deployment will fail.

Cory Burke
  • 301
  • 3
  • 4
3

CodeDeploy agent relies on exit status of your script 'deploy_afterinstall' to determine whether to succeed or fail the deployment lifecycle event. You might want to see if you can capture the exit status of the command you run in your script and return it from 'deploy_afterinstall'. Any non zero return value from your script should fail the deployment lifecycle event.

Surya Bala
  • 261
  • 1
  • 5