AWS Elastic Beanstalk预构建钩子执行权限添加失败求助
问题分析与解决
错误日志
2022/12/02 09:06:55.661307 [INFO] Executing instruction: RunAppDeployPreBuildHooks 2022/12/02 09:06:55.661326 [INFO] Executing platform hooks in .platform/hooks/prebuild/ 2022/12/02 09:06:55.661362 [INFO] Following scripts will be executed in order: [00_npm_install.sh] 2022/12/02 09:06:55.661368 [INFO] Try add execution permission 2022/12/02 09:06:55.661375 [INFO] Running script: .platform/hooks/prebuild/00_npm_install.sh 2022/12/02 09:07:19.325093 [ERROR] An error occurred during execution of command [app-deploy] - [RunAppDeployPreBuildHooks]. Stop running the command. Error: Command .platform/hooks/prebuild/00_npm_install.sh failed with error exit status 244.
当前文件权限检查
$ git ls-files -s .platform 100755 e8c201d7464dcf52ae874bb106de1a0da6a7bd20 0 .platform/confighooks/prebuild/00_npm_install.sh 100755 e8c201d7464dcf52ae874bb106de1a0da6a7bd20 0 .platform/hooks/prebuild/00_npm_install.sh 100644 7744deea7dfdafe4f584e01991cb6e80ee3079b9 0 .platform/nginx/conf.d/proxy.conf
此前正常执行日志
2022/11/30 18:56:18.539468 [INFO] Executing instruction: RunAppDeployPreBuildHooks 2022/11/30 18:56:18.539488 [INFO] Executing platform hooks in .platform/hooks/prebuild/ 2022/11/30 18:56:18.539521 [INFO] Following scripts will be executed in order: [00_npm_install.sh] 2022/11/30 18:56:18.539527 [INFO] Try add execution permission 2022/11/30 18:56:18.539535 [INFO] Adding execute mode to file, original mode is 436 2022/11/30 18:56:18.539543 [INFO] Running script: .platform/hooks/prebuild/00_npm_install.sh 2022/11/30 18:56:32.415210 [INFO] Finished running scripts in /var/app/staging/.platform/hooks/prebuild
原因分析
问题出在换行符格式上:新设备使用GitHub Desktop时,默认Git配置core.autocrlf可能被设为true,导致shell脚本的换行符从Linux要求的LF(换行)转换成了Windows的CRLF(回车+换行)。AWS Elastic Beanstalk的Linux环境中,bash无法识别CRLF格式的脚本,执行时会抛出退出码244的错误。
虽然Git中文件权限显示为100755(可执行),但换行符的变更会直接导致脚本执行失败。
解决步骤
- 调整Git全局换行符配置
在新设备上执行以下命令,确保提交代码时自动将CRLF转换为LF,拉取时不转换:
git config --global core.autocrlf input
- 修复已被转换的脚本文件
- 使用
dos2unix工具转换脚本换行符:
dos2unix .platform/hooks/prebuild/00_npm_install.sh .platform/confighooks/prebuild/00_npm_install.sh
- 或者在编辑器(如VS Code)中打开脚本,将右下角的换行符设置从
CRLF改为LF后保存。
- 添加.gitattributes文件避免后续问题
在仓库根目录创建.gitattributes文件,添加以下内容,强制所有shell脚本始终使用LF换行符:
*.sh text eol=lf
- 重新提交并推送
将修改后的文件和.gitattributes文件提交推送到远程仓库,重新触发Elastic Beanstalk部署即可恢复正常。
内容的提问来源于stack exchange,提问作者April Summers
相关产品推荐
相关产品推荐

