如何在GitHub Action中匹配静态文件变更并执行Zappa命令?
在GitHub Action中仅当Django静态文件变更时执行Zappa collectstatic命令
要实现仅当静态文件发生新增、修改或删除时才运行zappa manage dev collectstatic --noinput,你需要借助GitHub Action的文件变更检测能力,结合第三方动作来实现条件判断。以下是具体的配置方案:
核心思路
利用tj-actions/changed-files动作检测指定静态文件路径下的变更,根据检测结果决定是否执行collectstatic命令。
完整YAML配置示例
name: Deploy Django with Zappa on: push: branches: [ main ] pull_request: branches: [ main ] jobs: deploy: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v4 # 必须拉取完整历史,否则无法对比文件变更 with: fetch-depth: 0 - name: Detect static file changes id: changed-files uses: tj-actions/changed-files@v41 with: # 匹配所有app下的static目录及项目根目录的static文件 # 根据你的项目结构调整路径,比如指定单个app:myapp/static/**/* files: | **/static/**/* static/**/* - name: Install dependencies run: | python -m pip install --upgrade pip pip install -r requirements.txt pip install zappa - name: Run collectstatic only if static files changed if: ${{ steps.changed-files.outputs.has_changed == 'true' }} run: zappa manage dev collectstatic --noinput # 后续的部署步骤... - name: Deploy with Zappa run: zappa deploy dev
关键细节说明
- 路径匹配规则:
**/static/**/*:匹配所有子目录下的static文件夹中的任意文件(比如每个Django app下的static目录)static/**/*:匹配项目根目录下static文件夹中的任意文件- 如果你的静态文件有特定后缀(如.css/.js/.png),可以更精准匹配:
**/static/**/*.{css,js,png,jpg}
fetch-depth: 0的作用:必须拉取完整的仓库历史,这样changed-files动作才能对比当前提交与上一版本的文件差异- 条件判断逻辑:通过
steps.changed-files.outputs.has_changed的布尔值来控制命令是否执行,只有当静态文件有变更时,该值才会为true
内容的提问来源于stack exchange,提问作者Shine Angel
相关产品推荐
相关产品推荐

