Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make AWS CodeDeploy return an error when some of appspec hooks fails?

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.

like image 799
Stalinko Avatar asked Feb 17 '16 04:02

Stalinko


People also ask

What happens if an event hook is not present CodeDeploy?

If an event hook is not present, no operation is executed for that event. This section is required only if you are running scripts or Lambda validation functions as part of the deployment.

What should be checked first when an AWS CodeDeploy deployment fails?

Here's the steps I would take: Confirm that you installed the CodeDeploy agent. Confirm that you've created the IAM service role. Confirm that you have the IAM Instance Profile and that it's associated with the instance.

How does CodeDeploy rollback work?

CodeDeploy rolls back deployments by redeploying a previously deployed revision of an application as a new deployment. These rolled-back deployments are technically new deployments, with new deployment IDs, rather than restored versions of a previous deployment. Deployments can be rolled back automatically or manually.

What is specified within an AWS CodeDeploy AppSpec file?

The application specification file (AppSpec file) is a YAML -formatted or JSON-formatted file used by CodeDeploy to manage a deployment. The AppSpec file for an EC2/On-Premises deployment must be named appspec. yml or appspec. yaml , unless you are performing a local deployment.


2 Answers

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.

like image 170
Cory Burke Avatar answered Sep 29 '22 01:09

Cory Burke


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.

like image 44
Surya Bala Avatar answered Sep 29 '22 01:09

Surya Bala