CircleCI:为啥有两个工作:构建和测试不是 DRY?

Posted

技术标签:

【中文标题】CircleCI:为啥有两个工作:构建和测试不是 DRY?【英文标题】:CircleCI: why have two jobs: build and test that aren't DRY?CircleCI:为什么有两个工作:构建和测试不是 DRY? 【发布时间】:2021-11-16 00:16:24 【问题描述】:

我正在查看CircleCI's sample config for Ruby on Rails,它设置了 2 个工作:构建和测试。

version: 2.1 # Use 2.1 to enable using orbs and other features.

# Declare the orbs that we'll use in our config.
# read more about orbs: https://circleci.com/docs/2.0/using-orbs/
orbs:
  ruby: circleci/ruby@1.0
  node: circleci/node@2

jobs:
  build: # our first job, named "build"
    docker:
      - image: cimg/ruby:2.7-node # use a tailored CircleCI docker image.
        auth:
          username: mydockerhub-user
          password: $DOCKERHUB_PASSWORD  # context / project UI env-var reference
    steps:
      - checkout # pull down our git code.
      - ruby/install-deps # use the ruby orb to install dependencies
      # use the node orb to install our packages
      # specifying that we use `yarn` and to cache dependencies with `yarn.lock`
      # learn more: https://circleci.com/docs/2.0/caching/
      - node/install-packages:
          pkg-manager: yarn
          cache-key: "yarn.lock"

  test:  # our next job, called "test"
    # we run "parallel job containers" to enable speeding up our tests;
    # this splits our tests across multiple containers.
    parallelism: 3
    # here we set TWO docker images.
    docker:
      - image: cimg/ruby:2.7-node # this is our primary docker image, where step commands run.
        auth:
          username: mydockerhub-user
          password: $DOCKERHUB_PASSWORD  # context / project UI env-var reference
      - image: circleci/postgres:9.5-alpine
        auth:
          username: mydockerhub-user
          password: $DOCKERHUB_PASSWORD  # context / project UI env-var reference
        environment: # add POSTGRES environment variables.
          POSTGRES_USER: circleci-demo-ruby
          POSTGRES_DB: rails_blog_test
          POSTGRES_PASSWORD: ""
    # environment variables specific to Ruby/Rails, applied to the primary container.
    environment:
      BUNDLE_JOBS: "3"
      BUNDLE_RETRY: "3"
      PGHOST: 127.0.0.1
      PGUSER: circleci-demo-ruby
      PGPASSWORD: ""
      RAILS_ENV: test
    # A series of steps to run, some are similar to those in "build".
    steps:
      - checkout
      - ruby/install-deps
      - node/install-packages:
          pkg-manager: yarn
          cache-key: "yarn.lock"
      # Here we make sure that the secondary container boots
      # up before we run operations on the database.
      - run:
          name: Wait for DB
          command: dockerize -wait tcp://localhost:5432 -timeout 1m
      - run:
          name: Database setup
          command: bundle exec rails db:schema:load --trace
      # Run rspec in parallel
      - ruby/rspec-test

# We use workflows to orchestrate the jobs that we declared above.
workflows:
  version: 2
  build_and_test:     # The name of our workflow is "build_and_test"
    jobs:             # The list of jobs we run as part of this workflow.
      - build         # Run build first.
      - test:         # Then run test,
          requires:   # Test requires that build passes for it to run.
            - build   # Finally, run the build job.

test 作业包含 build 作业执行的所有步骤。那么,运行build 作业有什么意义呢?每次运行此作业似乎都需要额外的步骤。

我猜可能是这样的:

steps:
      ...
      - checkout
      - ruby/install-deps

在尝试运行测试之前会告诉我我的代码库是否有问题。但是,在实际开始运行测试之前,测试作业不会同时失败吗?

在我看来,运行 build 作业完全是浪费时间。

我错过了什么吗?

【问题讨论】:

【参考方案1】:

在我看来,运行构建作业完全是浪费时间。

我猜这个想法是测试步骤是并行运行的。如果您首先运行构建步骤,它将更新依赖项,然后为三个测试步骤缓存这些依赖项。否则,您的每个测试步骤都可能需要更新依赖项,因此您最终需要安装 3 次 ruby​​、bundle、node 等,这有点浪费,而且这些任务更有可能失败(因为它们依赖于外部资源)。对于总构建时间,如果您运行捆绑步骤或并行运行 3 次,则应该没有区别。

【讨论】:

可能是这种情况,但到目前为止我的少数部署似乎并未表明这一点。我还没有看到缓存用于测试步骤的位置,它可能是ruby orb 的一个功能,我只是在界面中没有看到。 我尝试使用 build 作业和不使用此设置运行此设置。通过删除build,我平均节省了将近 2 分钟。

以上是关于CircleCI:为啥有两个工作:构建和测试不是 DRY?的主要内容,如果未能解决你的问题,请参考以下文章

CircleCI:跳过整个工作流程

在 CircleCI 工作流或作业之后触发 Github Action

CircleCI 的 Fastlane 测试飞行

如何在CircleCI 2.0上配置顺序工作流?

GitHub + circleCI 自动构建/自动部署 应用

GitHub + circleCI 自动构建/自动部署 应用