如何使用CircleCI为develop合并到master配置不同工作流?
调整CircleCI配置以支持不同PR场景的触发
没问题,我来帮你修改.circleci/config.yml,实现你要的需求:针对合并到develop的功能分支PR继续运行原有测试,同时当develop合并到master的PR出现时,触发yarn integration脚本并使用独立的环境变量。
修改后的完整配置
# Javascript Node CircleCI 2.0 configuration file # Check https://circleci.com/docs/2.0/language-javascript/ for more details version: 2 jobs: build: docker: # specify the version you desire here - image: circleci/node:8.11.1 working_directory: ~/repo steps: - checkout - restore_cache: keys: - v1-dependencies-{{ checksum "package.json" }} # fallback to using the latest cache if no exact match is found - v1-dependencies- - run: yarn install - save_cache: paths: - node_modules key: v1-dependencies-{{ checksum "package.json" }} # run tests! - run: command: yarn test environment: SOME: 'stuff' integration-test: docker: - image: circleci/node:8.11.1 working_directory: ~/repo steps: - checkout - restore_cache: keys: - v1-dependencies-{{ checksum "package.json" }} - v1-dependencies- - run: yarn install - save_cache: paths: - node_modules key: v1-dependencies-{{ checksum "package.json" }} # run integration script with custom env vars - run: command: yarn integration environment: # 替换为你需要的专属环境变量 INTEGRATION_ENV: 'production-like' API_URL: 'https://your-integration-api.com' workflows: version: 2 pr-triggered-jobs: jobs: - build: filters: pull_requests: base: develop # 仅当PR目标分支是develop时触发 branches: ignore: /.*/ # 避免直接推送develop分支时触发,只针对PR - integration-test: filters: pull_requests: base: master head: develop # 仅当PR是从develop合并到master时触发 branches: ignore: /.*/ # 仅针对PR,不触发直接推送master的情况
关键改动说明
新增
integration-testJob- 复用了原
buildjob的依赖缓存逻辑,确保安装速度 - 替换执行脚本为
yarn integration,并配置了你需要的独立环境变量,你可以根据实际需求修改这些变量值
- 复用了原
添加
workflows编排逻辑pr-triggered-jobs是自定义的工作流名称,你可以按需修改buildJob触发规则:仅当PR的目标分支是develop时运行,和你原有逻辑完全匹配,同时通过branches.ignore确保只有PR会触发,直接推送develop分支不会执行integration-testJob触发规则:仅当PR是从develop分支合并到master分支时触发,精准匹配你要的场景,同样通过branches.ignore限制只针对PR
可选调整
- 如果你的功能分支有固定前缀(比如
feature/开头),可以给buildjob的pull_requests添加only: /^feature\/.*/来进一步限制触发范围 - 如果需要在触发
integration-test前先确保build通过,可以给integration-test添加requires: [build]的依赖规则,这样只有build成功后才会执行集成测试
内容的提问来源于stack exchange,提问作者Shamoon
相关产品推荐
相关产品推荐

