Run Grunt on ElasticBeanstalk

2019-02-13 05:29发布

问题:

I have a node application that requires grunt to do a "build" before the application can be executed successfully. (runs concat/minification/revving of source code etc). I've got this running on an EC2 instance with SSH access as I can just SSH into the directory and run Grunt as part of the deployment process. However to automate this I'm now moving the application to ElasticBeanstalk and I'm having difficulty getting the application to run grunt successfully. The reason for the move to EB is to keep SSH keys OFF live servers so these EB instances are setup with no ssh access.

There seems to be no official documentation available, could anybody point me in a good direction to be able to achieve the above? I need grunt to execute before the application is started so that the application has the files available (otherwise there'll be a 404).

回答1:

Running grunt would be very similar to running gulp, so, i'll include my config below.

This is inside a .ebextensions folder at the root of my project, named 01run.config. You can have multiple config files, they will run in alphabetical order (hence the 01 at the beginning.)

This basically just instructs the process to run these commands in order, again, it's done in alphabetical order, so I named them accordingly.

commands:
  01get_sudo:
    command: echo Defaults:root \!requiretty >> /etc/sudoers
  02npm_install:
    command: sudo yum -y --enablerepo=epel install nodejs npm
  03npm_install_bower:
    command: sudo npm install -g bower
  04npm_install_gulp:
    command: sudo npm install -g gulp
  05yum_install_git:
    command: sudo yum -y --enablerepo=epel install git
container_commands:
  01bower_install:
    command: sudo bower install --allow-root
  02gulp_sass:
    command: sudo gulp sass
  1. Get sudo access
  2. install node.js and npm with yum
  3. install bower (my gulp process needed bower)
  4. install gulp
  5. install git (needed by bower)
  6. i then run two container commands, which happen after npm install and before npm start:
    1. bower install
    2. gulp sass

In your case, you'd simply remove bower and git installs, install grunt-cli, and then run grunt.


Since doing this, I've removed the need for the above process by doing it all up front before deploying and committing the built files to the git repository. It was at least a good learning experience that gives me much more control over my ec2 instances deployed by beanstalk.



回答2:

This does not exactly answer the question, but in line with Kevin B's last remark, I do grunt tasks, including build, outside of Elastic Beanstalk, and use .ebignore to control the deployment. When .ebignore is present, eb deploy follows it instead of .gitignore. This allows me to control the build process outside of Elastic Beanstalk, while keeping build artifacts out of my git repo.

For example, if build artifacts go to .build/

.gitignore

node_modules
.elastic_beanstalk
...
.build

.ebignore

node_modules
.elastic_beanstalk
...
.git

Remember to add .git in .ebignore to prevent local git repo metadata from getting deployed. Also, I find that eb deploy evaluates all subfolders within an excluded folder, slowing the deployment unnecessarily. I had to temporarily move node_modules elsewhere before running eb deploy to speed it up.