GitHub更新后分支部署至开发服务器失败,API/UI目录为空求助
开发服务器分支部署脚本失效:UI/API目录克隆为空
问题概述
我们有一套分支部署到开发服务器的脚本,每个分支对应独立URL。此前脚本会在开发服务器目录创建分支名文件夹,并填充对应UI和API代码目录。但GitHub近期更新后脚本失效,分支文件夹能创建,但UI和API目录为空。
已尝试操作
- 在
.server_deploy.sh中添加echo语句排查,确认mkdir api和mkdir ui能创建目录,但后续git clone命令未拉取任何文件 - 手动在服务器通过SSH克隆分支失败,已调整过文件夹权限
- 检查了GitHub Actions的
laravel.yml配置文件
相关配置文件
部署脚本 .server_deploy.sh
#!/bin/bash set -e echo "Deploying application ..... branch = $1" #rm -rf "/var/www/html/devserver/$1" BRANCH_DIR="/var/www/html/devserver/$1" if [ ! -d "$BRANCH_DIR" ] then mkdir -p "$BRANCH_DIR" chown $USER:www-data -R $BRANCH_DIR cd $BRANCH_DIR echo "Cloning API Repo..." mkdir api git config --global --add safe.directory $BRANCH_DIR/api git clone git@github.com:{API URL HERE} api echo "Cloning UI Repo..." mkdir ui git config --global --add safe.directory $BRANCH_DIR/ui git clone git@github.com:{UI URL HERE} ui chown $USER:www-data -R $BRANCH_DIR fi # back to home directory cd # setup Node version source ~/.nvm/nvm.sh nvm use 12.16.3 # setup API cd $BRANCH_DIR/api chmod 777 -R storage bootstrap/cache chown aqib:www-data -R $BRANCH_DIR #switch branch git stash git reset --hard origin/master git pull if git branch -a | grep $1 then git checkout $1 else git checkout master fi cp /var/www/html/devserver/.env $BRANCH_DIR/api/ sed -i -e "s/BRANCH/$1/g" $BRANCH_DIR/api/.env composer install --no-interaction --prefer-dist --optimize-autoloader php artisan config:clear php artisan cache:clear php artisan migrate --force php artisan db:seed php artisan optimize npm install npm run prod #setup UI cd $BRANCH_DIR/ui #switch branch git stash git reset --hard origin/master git pull if git branch -a | grep $1 then git checkout $1 else git checkout master fi cp /var/www/html/devserver/config.js $BRANCH_DIR/ui/src/api/ sed -i -e "s/BRANCH/$1/g" $BRANCH_DIR/ui/src/api/config.js npm install npm run build
GitHub Actions配置 laravel.yml
name: Laravel on: pull_request jobs: devserver: runs-on: ubuntu-latest services: # mysql-service Label used to access the service container mysql-service: # Docker Hub image (also with version) image: mysql:5.7 env: ## Accessing to Github secrets, where you can store your configuration MYSQL_ROOT_PASSWORD: ****** MYSQL_DATABASE: db_test ## map the "external" 33306 port with the "internal" 3306 ** REST OF THE CODE HERE, LIKE UNIT TESTING AND CODE QUALITY ** - name: Deploy to Devserver env: PUSHED_BRANCH_NAME: ${{ steps.branch-name.outputs.head_ref_branch }} uses: appleboy/ssh-action@master with: username: ${{ secrets.DEVSERVER_SSH_USER }} host: ${{secrets.DEVSERVER_HOST}} envs: PUSHED_BRANCH_NAME password: ${{ secrets.DEVSERVER_SSH_PASS }} script: sudo /home/$USER/.server_deploy.sh $PUSHED_BRANCH_NAME
错误日志
======CMD====== sudo /home/***/.server_deploy.sh $PUSHED_BRANCH_NAME ======END====== out: Deploying application ..... branch = lg-30rvkhp out: Now using node v12.16.3 (npm v6.14.4) err: chmod: cannot access 'storage': No such file or directory err: chmod: cannot access 'bootstrap/cache': No such file or directory 2023/03/27 07:45:20 Process exited with status 1
排查与修复步骤
1. 修复SSH密钥权限问题
GitHub更新后对SSH密钥权限要求更严格,执行以下命令修正权限:
chmod 700 ~/.ssh chmod 600 ~/.ssh/id_rsa
同时确认服务器的SSH公钥已添加到GitHub仓库的Deploy Keys中,并勾选「允许写入权限」。
2. 修正Git Clone逻辑
脚本中提前创建api/ui目录会导致git clone失败(Git不允许克隆到已存在的非空目录),去掉提前创建目录的步骤,让Git自动生成目录:
# 替换API克隆部分 echo "Cloning API Repo..." git clone git@github.com:{API URL HERE} api cd api git config --add safe.directory "$(pwd)" cd .. # 替换UI克隆部分 echo "Cloning UI Repo..." git clone git@github.com:{UI URL HERE} ui cd ui git config --add safe.directory "$(pwd)" cd ..
3. 修复sudo执行的环境变量问题
使用sudo执行脚本时,$USER会变成root,导致权限配置错误。修改GitHub Actions的执行命令,保留当前环境变量:
script: sudo -E /home/aqib/.server_deploy.sh $PUSHED_BRANCH_NAME
或者直接在脚本中把$USER替换为具体的部署用户名(比如aqib)。
4. 临时关闭set -e排查错误
脚本开头的set -e会在命令失败时直接终止脚本,暂时注释该行,手动执行脚本查看完整的错误输出,定位克隆失败的具体原因。
验证步骤
- 手动在服务器用部署用户执行
git clone git@github.com:{API URL HERE} test-api,确认能成功拉取代码 - 修改脚本后,先手动执行一次脚本,确认无错误后再通过GitHub Actions触发
内容的提问来源于stack exchange,提问作者Hinan Khan
相关产品推荐
相关产品推荐

