4

I am currently using CodeDeploy for a NodeJS application of mine and I just have 1 burning question, is there a way to instruct say, my BeforeInstall script to run on the revision root instead of the root directory of my Linux OS?

For instance, the BeforeInstall hook script of mine may looks like this:

#!/bin/bash
echo "Current directory: "
echo $PWD
echo `ls`
# Do actual stuff here

By looking at the log it seemed to me that the script is ran on / (OS root), how do I instruct it to run on the revision root? I know I can CD into /opt/codedeploy-agent/deployment-root/app-id/deployment-id but it just seemed like a hassle to me to retrieve the app id and deployment id every time.

2
  • You will need to cd to the desired folder Commented Feb 1, 2018 at 5:30
  • have you found a solution? Commented Jan 7, 2020 at 23:29

3 Answers 3

2

Use cd "$(dirname "${BASH_SOURCE[0]}")" to change directory to the script directory of this deployment.

Example: cd /opt/codedeploy-agent/deployment-root/940bf299-698b-4179-a49f-f221316c1776/d-K24U7DAD1/deployment-archive/scripts

Sign up to request clarification or add additional context in comments.

Comments

0

If you want to run your hook script at the revision root, it's probably because it needs to access some files there (at least that's why I wanted to cd).

A solution is to map the needed files to an absolute path in the appspec.yml.

Let's say that your app needs a configuration (config.yml located at the revision root) file to run.

Your appspec.yml could look like:

files:
  - source: config.yml
    destination: /usr/local/etc/my_app
hooks:
  ApplicationStart:
    - location: scripts/start_app.sh
      timeout: 120
      runas: root

With this appspec.yml, you don't need to change the script execution location or to reconstruct the revision path.

Your script (scripts/start_app.sh) can directly access the config file at the mapped location /usr/local/etc/my_app/config.yml.

1 Comment

the destination for my source files is /var/opt/app/aws and the location for my update_wheel.sh requires paths relative to its location at ApplicationStart: /var/opt/app/aws/update_wheel.sh needs to see /var/opt/app/aws/packages, for example. But when I run this appspec I get /opt/codedeploy-agent/deployment-root/02b_truncatedhere_449/d-WEOED8WCC/deployment-archive/var/opt/app/aws/update_wheel.sh; I want to run update_wheel.sh in the destination files not the deployment-root/deployment-archive
-2

You scripts need to be location independent. Always provide absolute paths for all commands within your scripts to avoid such issues.

2 Comments

that is not what Amazon asks us to do
@VictorFerreira The key is to map your revision files to an absolute path, see my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.