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.

3 Answers3

1

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

JJ Wu
  • 11
  • 2
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.

asachet
  • 6,620
  • 2
  • 30
  • 74
  • 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` – d8aninja Sep 12 '21 at 18:33
-1

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

Yeshodhan Kulkarni
  • 2,844
  • 1
  • 16
  • 19