部署 AWS Beanstalk 后执行命令

Execute command after deploy AWS Beanstalk

我在部署后执行命令时遇到问题,我有一些 node.js 项目和脚本,如果我在 .ebextensions/ 中编写我的脚本命令,此脚本使用来自 node_modules 的一些 bin。他在 npm 安装之前执行的配置和 return 错误 ("node_modules/.bin/some": No such file or directory)。部署后我如何执行命令。谢谢

如果您阅读 AWS ebextensions documentation,他们会提到执行,特别是在他们提到所有命令都在部署应用程序版本之前执行的地方。

"You can use the container_commands key to execute commands for your container. The commands in container_commands are processed in alphabetical order by name. They run after the application and web server have been set up and the application version file has been extracted, but before the application version is deployed."

如果您第二次部署它,它应该可以工作;这是因为您的应用程序已经解压。然而,这不是一个有效的解决方案,因为每个生成的新实例都会出错。

创建名为 .ebextensions/post_actions.config 的文件:

container_commands:
 <name of container_command>:
    command: "<command to run>"

这将在提取代码之后但在启动之前执行。

我找到了以下解决方案

我将下一个命令添加到 beantalk 配置中:

commands:
  create_post_dir:
    command: "mkdir /opt/elasticbeanstalk/hooks/appdeploy/post"
    ignoreErrors: true
files:
  "/opt/elasticbeanstalk/hooks/appdeploy/post/some_job.sh":
    mode: "000755"
    owner: root
    group: root
    content: |
      #!/usr/bin/env bash
      cd /var/app/current
      export PATH=$PATH:$(ls -td /opt/elasticbeanstalk/node-install/node-* | head -1)/bin
      npm run some_script

此命令为 post 挂钩脚本创建(如果不存在)文件夹并添加 bash 脚本。此文件夹中的脚本仅在 npm 安装后执行,这对我的问题非常重要。

多亏了这个人http://junkheap.net/blog/2013/05/20/elastic-beanstalk-post-deployment-scripts/

更好的方法是使用 aws 平台挂钩。您可以在何处定义部署后挂钩 AWS Patform Hooks

在项目根目录里面你可以添加.platform/hooks/postdeploy/

Insdie这个路径你可以创建xxx-postdeploy-script.sh。在 Elastic Beanstalk 平台引擎部署应用程序和代理之后 运行 的文件 server.This 是部署工作流的最后一步