16

I'm trying to run AfterInstall script in AWS code deploy, but it is being run from the /opt/codedeploy-agent/ dir instead of my app directory.

This is how appspec.yml file looks like:

version: 0.0

os: linux

files:
  - source: /
    destination: /tmp/epub

hooks:
  AfterInstall:
    - location: server/install-packages.sh
      runas: root

As you can see it's a basic example.

Now, the bash script looks like this:

#!/bin/bash
npm install

I just want to npm install and that's it.

Unfortunately I'm getting the error:

LifecycleEvent - AfterInstall
Script - server/install-packages.sh
[stderr]npm ERR! install Couldn't read dependencies
[stderr]npm ERR! Linux 3.13.0-48-generic
[stderr]npm ERR! argv "/usr/bin/nodejs" "/usr/bin/npm" "install"
[stderr]npm ERR! node v4.2.1
[stderr]npm ERR! npm  v2.14.7
[stderr]npm ERR! path /opt/codedeploy-agent/package.json
[stderr]npm ERR! code ENOPACKAGEJSON
[stderr]npm ERR! errno -2
[stderr]npm ERR! syscall open
[stderr]
[stderr]npm ERR! package.json ENOENT: no such file or directory, open '/opt/codedeploy-agent/package.json'
[stderr]npm ERR! package.json This is most likely not a problem with npm itself.
[stderr]npm ERR! package.json npm can't find a package.json file in your current directory.
[stderr]
[stderr]npm ERR! Please include the following file with any support request:
[stderr]npm ERR!     /opt/codedeploy-agent/npm-debug.log

I was trying different appspec.yml configs like adding runas or adding "/" at the beginning of the location path. All the time it's trying to run from /opt/codedeoploy-agent/ directory.

In desperation, I've set absolute path to the script, but then I got :

Script does not exist at specified location: /tmp/epub/server/install-packages.sh

It's really annoying as I'm doing everything according to docs, but probably I'm missing something very very small !

Thanks

matewilk
  • 947
  • 1
  • 12
  • 25

1 Answers1

27

Ok,

So I've found out, that codedeoloy-agent is running AfterInstall (and probably all the other steps) from the temporary directory created by the agent on deploy instance, so in my case I had to modify the bash script by cd-ing to the proper directory:

#!/bin/bash
cd /tmp/epub/server/
npm install
matewilk
  • 947
  • 1
  • 12
  • 25
  • Where did you find this out? – artburkart Dec 17 '15 at 20:08
  • 2
    I don't remember if I concluded it from here: http://docs.aws.amazon.com/codedeploy/latest/userguide/app-spec-ref.html#app-spec-ref-hooks - maybe there is one sentence suggesting that - I remember it took me quite a while - or more likely by trials and errors, or a combination of both :) – matewilk Dec 18 '15 at 13:48
  • @matewilk - super helpful! – Ben Jan 18 '16 at 22:38
  • Thanks, actually this question surpassingly was down voted saying "This question does not show any research effort; it is unclear or not useful". So thanks for that, glad to help! – matewilk Feb 03 '16 at 14:44
  • This is correct, came to the same realization during testing. Also FYI you can use "(cd /your/path && npm install)" in your bash script to do the directory change in a subshell so it doesn't affect other things. – DougW May 17 '17 at 04:10
  • I'm putting `cd "$( dirname "${BASH_SOURCE[0]}" )"` at the top of each script. It's also followed by a `source config.sh` for ...well... configuration. – musicin3d Oct 27 '17 at 05:26