mcpField
C
circleci

circleci

github.com/bagelhole云与 DevOps1.2k325 次安装23 周前更新
代码仓库

关于 circleci

面向开发者的 CircleCI 技能,用于配置自动构建、测试和部署流程,涵盖 orbs、缓存、并行执行、审批与机密上下文。

主要功能

  1. 01配置流水线与执行器
  2. 02使用 orbs 复用配置
  3. 03缓存与工作区共享
  4. 04并行运行测试
  5. 05工作流审批与定时触发
  6. 06使用上下文管理机密

适用场景

  1. 01为应用设置 CI/CD
  2. 02通过缓存和并行测试缩短构建时间
  3. 03为生产部署设置审批关卡
  4. 04区分不同部署环境的机密

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/bagelhole/devops-security-agent-skills --skill circleci

安装后在 Claude Code 中输入/circleci确认已加载。

详细介绍

CircleCI

使用 CircleCI 的云原生 CI/CD 平台构建、测试和部署应用程序。

适用场景

使用此技能的场景:

  • 使用 CircleCI 设置 CI/CD 流水线
  • 使用 orbs 实现可复用配置
  • 通过缓存和并行处理优化构建时间
  • 配置 CircleCI 工作流和审批
  • 管理 CircleCI 上下文和密钥

前提条件

  • 已连接到代码仓库的 CircleCI 账户
  • 已在 CircleCI 控制面板中启用项目
  • 具备 YAML 基础知识

配置文件

创建 .circleci/config.yml:

version: 2.1

orbs:
  node: circleci/[email protected]
  docker: circleci/[email protected]

executors:
  default:
    docker:
      - image: cimg/node:20.10
    working_directory: ~/project

jobs:
  build:
    executor: default
    steps:
      - checkout
      - node/install-packages:
          pkg-manager: npm
      - run:
          name: Build application
          command: npm run build
      - persist_to_workspace:
          root: .
          paths:
            - dist

  test:
    executor: default
    steps:
      - checkout
      - node/install-packages:
          pkg-manager: npm
      - run:
          name: Run tests
          command: npm test

  deploy:
    executor: default
    steps:
      - checkout
      - attach_workspace:
          at: .
      - run:
          name: Deploy
          command: ./deploy.sh

workflows:
  build-test-deploy:
    jobs:
      - build
      - test:
          requires:
            - build
      - deploy:
          requires:
            - test
          filters:
            branches:
              only: main

执行器

Docker 执行器
executors:
  node:
    docker:
      - image: cimg/node:20.10
      - image: cimg/postgres:15.0
        environment:
          POSTGRES_USER: test
          POSTGRES_DB: testdb
    working_directory: ~/app
Machine 执行器
executors:
  linux-machine:
    machine:
      image: ubuntu-2204:current
    resource_class: large
macOS 执行器
executors:
  macos:
    macos:
      xcode: "15.0.0"
    resource_class: macos.m1.medium.gen1

缓存

依赖项缓存
jobs:
  build:
    steps:
      - checkout
      - restore_cache:
          keys:
            - v1-deps-{{ checksum "package-lock.json" }}
            - v1-deps-
      - run: npm ci
      - save_cache:
          key: v1-deps-{{ checksum "package-lock.json" }}
          paths:
            - node_modules
多键缓存
- restore_cache:
    keys:
      - v1-{{ .Branch }}-{{ checksum "package-lock.json" }}
      - v1-{{ .Branch }}-
      - v1-main-
      - v1-

工作区

持久化数据
jobs:
  build:
    steps:
      - checkout
      - run: npm run build
      - persist_to_workspace:
          root: .
          paths:
            - dist
            - node_modules

  deploy:
    steps:
      - attach_workspace:
          at: ~/project
      - run: ./deploy.sh

并行处理

拆分测试
jobs:
  test:
    parallelism: 4
    steps:
      - checkout
      - run:
          name: Run tests
          command: |
            TESTFILES=$(circleci tests glob "test/**/*.test.js" | circleci tests split --split-by=timings)
            npm test -- $TESTFILES
      - store_test_results:
          path: test-results

工作流

顺序执行作业
workflows:
  pipeline:
    jobs:
      - build
      - test:
          requires:
            - build
      - deploy:
          requires:
            - test
并行执行作业
workflows:
  pipeline:
    jobs:
      - build
      - test-unit:
          requires:
            - build
      - test-integration:
          requires:
            - build
      - deploy:
          requires:
            - test-unit
            - test-integration
手动审批
workflows:
  deploy-prod:
    jobs:
      - build
      - test
      - hold:
          type: approval
          requires:
            - test
      - deploy-production:
          requires:
            - hold
计划工作流
workflows:
  nightly:
    triggers:
      - schedule:
          cron: "0 2 * * *"
          filters:
            branches:
              only:
                - main
    jobs:
      - build
      - test
分支筛选
workflows:
  build-deploy:
    jobs:
      - build:
          filters:
            branches:
              only:
                - main
                - /feature-.*/
      - deploy:
          filters:
            branches:
              only: main
            tags:
              only: /^v.*/

Orbs

使用 Orbs
version: 2.1

orbs:
  aws-cli: circleci/[email protected]
  kubernetes: circleci/[email protected]

jobs:
  deploy:
    executor: aws-cli/default
    steps:
      - aws-cli/setup:
          aws_access_key_id: AWS_ACCESS_KEY_ID
          aws_secret_access_key: AWS_SECRET_ACCESS_KEY
      - kubernetes/install-kubectl
      - run: kubectl apply -f k8s/
常用 Orbs
orbs:
  node: circleci/[email protected]              # Node.js
  docker: circleci/[email protected]          # Docker builds
  aws-cli: circleci/[email protected]        # AWS CLI
  aws-ecr: circleci/[email protected]        # ECR push
  aws-ecs: circleci/[email protected]        # ECS deploy
  gcp-cli: circleci/[email protected]        # GCP CLI
  kubernetes: circleci/[email protected]  # K8s deploy
  slack: circleci/[email protected]           # Notifications

Docker 构建

version: 2.1

orbs:
  docker: circleci/[email protected]

jobs:
  build-and-push:
    executor: docker/docker
    steps:
      - setup_remote_docker:
          version: 20.10.24
      - checkout
      - docker/check
      - docker/build:
          image: myorg/myapp
          tag: $CIRCLE_SHA1
      - docker/push:
          image: myorg/myapp
          tag: $CIRCLE_SHA1

环境变量

项目变量

在 CircleCI Project Settings > Environment Variables 中设置

上下文
workflows:
  deploy:
    jobs:
      - deploy-staging:
          context: staging-secrets
      - deploy-production:
          context: production-secrets
使用变量
jobs:
  deploy:
    steps:
      - run:
          name: Deploy
          command: |
            aws s3 sync dist/ s3://$S3_BUCKET
          environment:
            AWS_DEFAULT_REGION: us-east-1

构建产物和测试结果

jobs:
  test:
    steps:
      - run:
          name: Run tests
          command: npm test -- --coverage
      - store_test_results:
          path: test-results
      - store_artifacts:
          path: coverage
          destination: coverage-report

资源类别

jobs:
  build:
    docker:
      - image: cimg/node:20.10
    resource_class: large  # 4 vCPU, 8GB RAM
    steps:
      - checkout
      - run: npm run build

# Available classes:
# small: 1 vCPU, 2GB RAM
# medium: 2 vCPU, 4GB RAM (default)
# large: 4 vCPU, 8GB RAM
# xlarge: 8 vCPU, 16GB RAM

常见问题

问题:缓存未恢复

问题:每次构建都会缓存未命中
解决方案:检查缓存键格式,确保校验和文件未更改

问题:附加工作区失败

问题:找不到持久化的工作区
解决方案:确保已完成 persist_to_workspace 作业,并检查路径

问题:Docker 层缓存

问题:Docker 构建速度缓慢
解决方案:在项目设置中启用 Docker Layer Caching(付费功能)

最佳实践

  • 使用 orbs 执行常见任务
  • 实施积极的缓存策略
  • 使用工作区在作业之间共享数据
  • 通过并行处理拆分测试以加快构建
  • 使用上下文管理特定环境的密钥
  • 定义可复用的执行器
  • 保存测试结果以供分析

相关技能

评论

支持 Markdown