Reputation: 572
My node.js app consists of two things, an express backend and a react frontend.
I have two package.json files in this structure
package.json
/app/package.json
What I want to do is run a script in my frontend folder, app, that builds my code.
the script I want to run is npm install
and npm run build
How do I run this script after a deploy have happened?
commands:
01_app_npm_install:
command: npm install
cwd: app/
02_app_npm_build:
command: npm run build
cwd: app/
But it returns an error saying that "No such file or direc tory: 'app/'."
Is this possible to do on aws elastic beanstalk?
Upvotes: 6
Views: 15564
Reputation: 665
As of Amazon Linux 2 you must use Platform Hooks. Other answers here only work with Amazon Linux AMI platform versions.
For example in your application root, create the
.platform/hooks/postdeploy
folder(s), then create a file called service-httpd-restart.sh
with contents of
#!/usr/bin/env bash
service httpd restart
To do the same thing as Gustaf's comment.
Upvotes: 1
Reputation: 1349
Update: This is now much more developed and defined.
https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/platforms-linux-extend.html
Original:
I haven't done nodejs myself with Elastic Beanstalk, but I have done a lot of Django.
The commands:
section runs before the project files are put in place. This is where you can install server packages for example.
The container_commands:
section runs in a staging directory before the files are put in its final destination (which is /var/app/current it seems for nodejs). Here you can modify your files if you need to.
Reference for above: http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers-ec2.html
You can also run a post deploy scripts. This is not very well documented (at least it wasn't). You can do something like this:
files:
"/opt/elasticbeanstalk/hooks/appdeploy/post/99_restart_delayed_job.sh":
mode: "000755"
owner: root
group: root
content: |
#!/usr/bin/env bash
service httpd restart
Upvotes: 14
Reputation: 572
Thanks to @gustaf and some other sites I managed to pull a solution together. It's not pretty but it works.
I created a file in .ebextensions/01_build.config
commands:
create_post_dir:
command: "mkdir /opt/elasticbeanstalk/hooks/appdeploy/post"
ignoreErrors: true
files:
"/opt/elasticbeanstalk/hooks/appdeploy/post/99_build_app.sh":
mode: "000755"
owner: root
group: root
content: |
#!/usr/bin/env bash
cd /var/app/current/app/
sudo /opt/elasticbeanstalk/node-install/node-v6.2.2-linux-x64/bin/npm install
sudo /opt/elasticbeanstalk/node-install/node-v6.2.2-linux-x64/bin/npm run build
hard coding the node-version is not perfect but it works for now.
Upvotes: 6
Reputation: 3748
If you refer to the docs you'll find that container_commands
is where you should put the app related stuff.
The file is executed based on preference of various parts, commands
being the first and container_commands
being the last. During this phase, the app hasn't been copied to the desired location (/var/app/current
).
Also, if you set environment variables, these will be available to you in container_commands
only.
Upvotes: 2