GitLab CI配置:仅特定文件变更时跳过Deploy作业
GitLab CI 配置优化:避免重复执行Deploy作业
当前配置
build-docker: stage: build-docker rules: - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH' changes: - app/Dockerfile - app/requirements.txt when: always - when: manual allow_failure: true image: name: alpine entrypoint: [""] script: - echo 'Git Pulling, building and restarting' deploy: stage: deploy rules: - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH' changes: - app/**/* when: always - when: manual allow_failure: true image: name: alpine entrypoint: [""] script: - echo 'Git Pulling and restarting'
需求
当变更文件仅为app/Dockerfile和/或app/requirements.txt时,无需运行deploy作业(因为build-docker作业已包含部署操作且步骤更完整);但当app目录下其他文件发生变更时,正常运行deploy作业。
之前尝试的无效配置
曾尝试在deploy阶段使用如下配置,但未达到预期效果:
- if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH' changes: - "app/!(Dockerfile)" - "app/!(requirements.txt)" - app/**/* when: always - when: manual allow_failure: true
解决方案
要实现需求,需要在deploy的rules中添加否定逻辑,排除仅变更Dockerfile或requirements.txt的场景,以下是两种可行的配置方式:
方式一:使用except: changes明确排除
deploy: stage: deploy rules: - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH' changes: - app/**/* except: changes: - app/Dockerfile - app/requirements.txt when: always - when: manual allow_failure: true image: name: alpine entrypoint: [""] script: - echo 'Git Pulling and restarting'
方式二:使用扩展通配符匹配排除目标文件
deploy: stage: deploy rules: - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH' changes: - "app/!(Dockerfile|requirements.txt)" - "app/**/*" when: always - when: manual allow_failure: true image: name: alpine entrypoint: [""] script: - echo 'Git Pulling and restarting'
配置说明
- 方式一逻辑清晰:先匹配
app目录下所有变更,再排除仅变更Dockerfile和requirements.txt的情况,确保只有存在其他文件变更时才触发deploy。 - 方式二利用扩展通配符
!(Dockerfile|requirements.txt)直接匹配app目录下除目标文件外的所有内容,同时app/**/*覆盖子目录的变更,确保所有非目标文件的修改都能触发作业。
内容的提问来源于stack exchange,提问作者Manuel Simões
相关产品推荐
相关产品推荐

