主仓库删除的文件未同步至dist仓库,如何修改Workflow?
解决主仓库删除文件无法同步到dist仓库的问题
问题核心在于当前rsync命令缺少删除同步的参数——默认情况下rsync只会同步新增或修改的文件,不会处理目标目录中已不存在于源目录的文件。只需修改rsync命令,添加--delete参数即可实现删除同步,同时推荐使用归档模式-a来保留文件属性并简化参数:
修改后的完整Workflow
name: Build and Deploy on: push: branches: - "master" - "dev" jobs: build-and-deploy: runs-on: ubuntu-latest steps: - name: Checkout the source repository uses: actions/checkout@v2 with: fetch-depth: 0 path: src - name: Setup node uses: actions/setup-node@v2 with: node-version: 14 - name: Build the source run: | cd src npm ci npm run webpack:build cd .. - name: Checkout the dist repository uses: actions/checkout@v2 with: repository: xxx path: dist ssh-key: ${{ secrets.xxx_DIST_PUSH_KEY }} ref: ${{ github.ref }} - name: Commit and push the changes to the dist repo run: | # 关键修改:添加--delete参数实现删除同步,-a替代-r包含更多归档属性 rsync -ra --delete src/shopify/ dist/ cd src commit=`git rev-parse HEAD` cd ../dist git config user.name github-actions git config user.email github-actions@github.com git add . git diff-index --quiet HEAD || git commit -m "Built $commit" git push
参数说明
-a:归档模式,等价于-rlptgoD,包含递归同步、保留文件权限、时间戳、符号链接等属性,比单独用-r更全面。--delete:强制删除目标目录(dist)中源目录(src/shopify)不存在的文件/目录,确保两边文件完全一致。
注意点
确保源路径src/shopify/末尾的斜杠不要省略,否则rsync会在dist目录下创建shopify子目录,而非直接同步内容到dist根目录。
内容的提问来源于stack exchange,提问作者Mathijs Delva
相关产品推荐
相关产品推荐

