我可以遍历过滤值列表,并在 Circle CI 中对每个过滤值开始一个作业吗?

Can I loop through a list of filter values, and start a job with each of them, in Circle CI?

类似于 How start identical jobs with different parameters in parallel execution? 但对于 CircleCI 而不是 Jenkins。

我有一个工作圈 CI 配置。我的工作之一是 run-tests。它采用一个名为 filter 的参数,该参数传递给实际 运行 测试我们的命令(在本例中为 jest)。

  run tests:
    description: Run backend tests
    # https://circleci.com/docs/reference-2-1/#docker
    parameters:
      filter:
        description: "Test filter for jest"
        default: ""
        type: string
    executor: full-backend

    steps:
      - attach_workspace:
          at: /home/circleci/project

      - run:
          # Note the 'circle_test' DB is already created on the DB image
          name: Run DB migration
          command: |
            cd /home/circleci/project; set -o allexport; cp .env.circleci .env; source .env
            npm run typeorm -- migration:run

      - run:
          name: Run tests
          # 30GB of 32GB total per resource_class above
          command: |
            cd /home/circleci/project; set -o allexport; cp .env.circleci .env; source .env
            node --max_old_space_size=30720 ./node_modules/.bin/jest --passWithNoTests --runInBand --logHeapUsage << parameters.filter >>

然后我们多次调用 run-tests 作业,并行地,使用不同的 filter 值:

      - run tests:
          requires:
            - install npm packages
          name: Test feedArea
          filter: --testPathPattern='feedArea.test'

      - run tests:
          requires:
            - install npm packages
          name: Test blackListArea
          filter: --testPathPattern='blackListArea.test'

      - run tests:
          requires:
            - install npm packages
          name: Test projectsAreaV2
          filter: --testPathPattern='projectsAreaV2.test'

      (and so on)

这非常有效。但是这里有很多重复。

有没有一种方法可以遍历过滤器值列表,然后 运行 每个值的作业?

你要的是矩阵。这是一个片段:

workflows:
  main:
    jobs:
      - tests:
          matrix:
            parameters:
              filter:
                - "--testPathPattern='feedArea.test'"
                - "--testPathPattern='blackListArea.test'"
                - "--testPathPattern='projectsAreaV2.test'"

这是文档:https://circleci.com/blog/circleci-matrix-jobs/