Azure Linux WebApp通过DevOps Pipeline无法执行.sh文件及相关疑问
解决Azure Linux WebApp(PHP 8.3)部署Laravel的三个问题
1. Kudu API执行自定义.sh脚本未生效的排查与修复
先聚焦几个核心排查点:
- 脚本路径与部署状态:确认
.sh脚本已部署到/home/site/wwwroot目录,Kudu执行时必须指定完整绝对路径,比如/home/site/wwwroot/setup.sh,不能用相对路径。 - 替换为更可靠的Azure CLI调用:放弃直接调用Kudu REST API,改用Azure CLI的部署命令执行脚本,示例YAML任务:
- task: AzureCLI@2 displayName: 'Run Setup Script via Kudu' inputs: azureSubscription: '你的Azure订阅名' scriptType: bash inlineScript: | az webapp deploy --name <你的WebApp名称> --resource-group <资源组名称> --src-path setup.sh --type bash --target-path /home/site/wwwroot/setup.sh --execute
- 脚本本身的合法性:脚本开头必须加
#!/bin/bash,所有命令用绝对路径(比如mkdir -p /home/site/wwwroot/storage/app/public而非mkdir storage/app/public);可先通过WebApp的SSH终端手动执行脚本,验证是否有语法或权限错误。 - 查看Kudu日志定位问题:访问
https://<你的WebApp名>.scm.azurewebsites.net/api/logs/docker,查看脚本执行的错误输出,精准定位失败原因。
2. 无需执行.sh脚本解决Laravel/Bootstrap写入权限问题
Azure Linux WebApp的/home目录是持久化可写目录,把Laravel需要写入的目录迁移到这里即可:
- 修改.env配置:指定可写目录的绝对路径:
APP_STORAGE=/home/storage BOOTSTRAP_CACHE=/home/bootstrap/cache
- 调整Laravel文件系统配置:编辑
config/filesystems.php,将默认磁盘的根路径指向/home下的目录:
'disks' => [ 'local' => [ 'driver' => 'local', 'root' => env('APP_STORAGE', storage_path('app')), ], 'public' => [ 'driver' => 'local', 'root' => env('APP_STORAGE', storage_path('app')) . '/public', 'url' => env('APP_URL') . '/storage', 'visibility' => 'public', ], ],
- 流水线中提前创建目录:在部署步骤前添加Azure CLI任务,预创建所需目录:
- task: AzureCLI@2 displayName: 'Create Writable Directories' inputs: azureSubscription: '你的Azure订阅名' scriptType: bash inlineScript: | az webapp ssh --name <你的WebApp名称> --resource-group <资源组名称> --command 'mkdir -p /home/storage/app/public /home/bootstrap/cache'
全程无需修改权限,直接利用/home的可写特性解决问题。
3. CD末尾执行Laravel迁移无需自定义.sh脚本
直接在YAML流水线中添加Azure CLI任务,通过WebApp SSH执行迁移命令:
- task: AzureCLI@2 displayName: 'Run Laravel Migrations' inputs: azureSubscription: '你的Azure订阅名' scriptType: bash inlineScript: | az webapp ssh --name <你的WebApp名称> --resource-group <资源组名称> --command 'cd /home/site/wwwroot && php artisan migrate --force'
或者用Kudu的exec API直接执行:
- task: AzureCLI@2 displayName: 'Run Migrations via Kudu' inputs: azureSubscription: '你的Azure订阅名' scriptType: bash inlineScript: | az rest --method POST --uri "https://<你的WebApp名>.scm.azurewebsites.net/api/command" --body '{"command":"cd /home/site/wwwroot && php artisan migrate --force","type":"bash"}' --headers "Content-Type=application/json"
注意必须加--force参数,避免交互确认,适配CI/CD自动化场景。
内容的提问来源于stack exchange,提问作者Herald Gjura
相关产品推荐
相关产品推荐

