通过GitHub Actions构建部署Flutter Web应用时遇路径匹配错误
问题
在使用GitHub Actions构建并部署Flutter Web应用时,遇到如下错误:
error: pathspec 'build/web/*' did not match any file(s) known to git
对应的.github/workflows配置文件代码如下:
name: Deploy on: push: branches: - main jobs: build-and-deploy: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v2 - name: Set up Flutter uses: subosito/flutter-action@v2 with: flutter-version: '3.24.3' - name: Install dependencies run: flutter pub get - name: Build Flutter web run: | echo "SUPABASE_URL=${{ secrets.SUPABASE_URL }}" >> .env echo "SUPABASE_ANON_KEY=${{ secrets.SUPABASE_ANON_KEY }}" >> .env flutter build web - name: Deploy to production branch run: | git config --local user.email "my email" git config --local user.name "my name" if [ -d "build/web" ]; then echo "Build directory exists, proceeding with deployment." else echo "Build directory does not exist. Build might have failed." exit 1 fi echo "Listing contents of build/web:" ls -la build/web git checkout -B production git rm -rf * git checkout main "build/web/*" echo "Listing contents of production branch after copying:" ls -la git add . git commit -m "Deploy to production" git push origin production env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} deploy-to-server: runs-on: ubuntu-latest needs: build-and-deploy steps: - uses: actions/checkout@v2 - run: set -eu - run: mkdir "$HOME/.ssh" - run: echo "${{ secrets.SSH_KEY }}" > "$HOME/.ssh/key" - run: chmod 600 "$HOME/.ssh/key" - run: | rsync -e "ssh -p 20022 -i $HOME/.ssh/key -o StrictHostKeyChecking=no" \ --archive --compress --delete \ ./build/web/ HERE IS MY SITE
需求逻辑:当main分支有代码推送时,构建Flutter项目,将build/web/下的构建产物迁移到production分支,再从该分支同步到服务器。
解决方法
错误原因
git checkout main "build/web/*"命令无效,因为build/web目录通常会被加入.gitignore,main分支的Git仓库中根本不存在这个路径,Git无法从main分支检出不存在的文件。而构建产物是存在于本地工作区的临时文件,不在Git版本记录里。
修正步骤
替换构建产物复制逻辑
把git checkout main "build/web/*"替换为直接复制本地构建好的文件到production分支的根目录:# 原错误代码 # git checkout main "build/web/*" # 替换为 cp -r build/web/* .调整production分支推送方式
由于每次构建都是覆盖式更新production分支,推送时需要加上-f强制推送,避免分支历史冲突:git push origin production -f修正deploy-to-server任务的同步路径
当前deploy任务拉取的是main分支,且同步的./build/web/在production分支中不存在(production分支根目录就是构建产物),需要修改为拉取production分支并同步根目录文件:deploy-to-server: runs-on: ubuntu-latest needs: build-and-deploy steps: - uses: actions/checkout@v2 with: ref: production # 指定拉取production分支 - run: set -eu - run: mkdir "$HOME/.ssh" - run: echo "${{ secrets.SSH_KEY }}" > "$HOME/.ssh/key" - run: chmod 600 "$HOME/.ssh/key" - run: | rsync -e "ssh -p 20022 -i $HOME/.ssh/key -o StrictHostKeyChecking=no" \ --archive --compress --delete \ ./ HERE IS MY SITE # 同步当前根目录的构建产物
完整修正后的Deploy to production branch步骤
git config --local user.email "my email" git config --local user.name "my name" if [ -d "build/web" ]; then echo "Build directory exists, proceeding with deployment." else echo "Build directory does not exist. Build might have failed." exit 1 fi echo "Listing contents of build/web:" ls -la build/web git checkout -B production # 清理当前分支所有文件 git rm -rf . # 复制本地构建产物到当前分支根目录 cp -r build/web/* . echo "Listing contents of production branch after copying:" ls -la git add . git commit -m "Deploy to production" git push origin production -f
内容的提问来源于stack exchange,提问作者Alexandr
相关产品推荐
相关产品推荐

