首页 > 解决方案 > How to simplify my YAML CircleCI configuration for better readability?

问题描述

I am pretty new to continuous integration, so far I like it very much. I am wondering if there is by any chance a possibility to make the configuration file better readable and simplified.

The reason why I am asking is because of the fact that I am using the same Workflow for Development and Production mode.

How can I keep the configuration "DRY" without copy-pasting the same configuration file over and over again?

Can I do something with BASH script instead?

My config:

version: 2
jobs:
  production:
    working_directory: ~/production-theme
    docker:
    - image: circleci/node:10.16
    steps:
    - add_ssh_keys:
        fingerprints:
        - xxx
    - store_test_results:
        path: test-results
    - checkout
    - restore_cache:
        name: Restore Yarn Package & Packge.json Cache
        keys:
        - yarn-packages-{{ checksum "yarn.lock" }}
        - v1-dependencies-{{ checksum "package.json" }}
        - yarn-packages-
        - v1-dependencies-
    - run:
        name: Install Yarn Packages
        command: yarn install
    - run:
        name: Building Repo In Production Mode
        command: yarn prod-build
    - save_cache:
        name: Save Yarn Package Cache
        key: dependency-cache-{{ checksum "package.json" }}
        paths:
        - ./node_modules
    - run:
        name: Run SSH keyscan
        command: ssh-keyscan ${hostname} >> ~/.ssh/known_hosts
    - run:
        name: Install Rysnc
        command: sudo apt-get install rsync
    - run:
        name: Upload files to theme folder
        command: rsync -avP --delete-before --exclude 'node_modules' --exclude '.git'
          --exclude 'webpack' --exclude '.circleci' --exclude 'src' --exclude '.babelrc'
          --exclude '.browserslistrc' --exclude '.eslintrc' --exclude '.gitignore'
          --exclude '.prettierrc' --exclude '.stylelintignore' --exclude '.stylelintrc'
          --exclude 'env.json' --exclude 'package.json' --exclude 'yarn.lock' --exclude
          'README.md' ~/production-theme ${username}@${hostname}:/var/www/html/${site_name}/wp-content/themes/
  development:
    working_directory: ~/develop-theme
    docker:
    - image: circleci/node:10.16
    steps:
    - add_ssh_keys:
        fingerprints:
        - xxx
    - store_test_results:
        path: test-results
    - checkout
    - restore_cache:
        name: Restore Yarn Package & Packge.json Cache
        keys:
        - yarn-packages-{{ checksum "yarn.lock" }}
        - v1-dependencies-{{ checksum "package.json" }}
        - yarn-packages-
        - v1-dependencies-
    - run:
        name: Install Yarn Packages
        command: yarn install
    - run:
        name: Building Repo In Develop Mode
        command: yarn test-build
    - save_cache:
        name: Save Yarn Package Cache
        key: dependency-cache-{{ checksum "package.json" }}
        paths:
        - ./node_modules
    - run:
        name: Run SSH keyscan
        command: ssh-keyscan ${hostname} >> ~/.ssh/known_hosts
    - run:
        name: Install Rysnc
        command: sudo apt-get install rsync
    - run:
        name: Upload files to theme folder
        command: rsync -avP --delete-before --exclude 'node_modules' --exclude '.git'
          --exclude 'webpack' --exclude '.circleci' --exclude 'src' --exclude '.babelrc'
          --exclude '.browserslistrc' --exclude '.eslintrc' --exclude '.gitignore'
          --exclude '.prettierrc' --exclude '.stylelintignore' --exclude '.stylelintrc'
          --exclude 'env.json' --exclude 'package.json' --exclude 'yarn.lock' --exclude
          'README.md' ~/develop-theme ${username}@${hostname}:/var/www/html/${site_name}/wp-content/themes/
workflows:
  version: 2
  production_and_development:
    jobs:
    - development:
        filters:
          branches:
            only: develop
    - production:
        filters:
          branches:
            only: master

标签: bashcontinuous-integrationyamldevopscircleci

解决方案


您可以使用 CircleCI 配置的“commands”和“executors”键。

更多信息: https ://circleci.com/docs/2.0/reusing-config/#the-commands-key https://circleci.com/docs/2.0/reusing-config/#executors

应用它,您的配置可能会变得更具可读性并且更短。

将 docker 定义包装在“executors”中:

executors:
  node:
    docker:
      - image: circleci/node:10.16

所有步骤定义都合并在commands: flow: .... 同时jobs变得非常苗条,因为他们将重用“流” command

jobs:
  production:
    executor: node
    working_directory: ~/production-theme
    steps:
      - flow:
          environment: "production"
          yarn: "yarn prod-build"

更新.config.yml

version: 2.1

executors:
  node:
    docker:
      - image: circleci/node:10.16

commands:
  flow:
    parameters:
      environment:
        type: string
        default: "production"
      yarn:
        type: string
        default: "yarn prod-build"
    steps:
      - add_ssh_keys:
          fingerprints:
            - xxx
      - store_test_results:
          path: test-results
      - checkout
      - restore_cache:
          name: Restore Yarn Package & Packge.json Cache
          keys:
            - yarn-packages-{{ checksum "yarn.lock" }}
            - v1-dependencies-{{ checksum "package.json" }}
            - yarn-packages-
            - v1-dependencies-
      - run:
          name: Install Yarn Packages
          command: yarn install
      - run:
          name: Building Repo
          command: << parameters.yarn >>
      - save_cache:
          name: Save Yarn Package Cache
          key: dependency-cache-{{ checksum "package.json" }}
          paths:
            - ./node_modules
      - run:
          name: Run SSH keyscan
          command: ssh-keyscan ${hostname} >> ~/.ssh/known_hosts
      - run:
          name: Install Rysnc
          command: sudo apt-get install rsync
      - run:
          name: Upload files to theme folder
          command: rsync -avP --delete-before --exclude 'node_modules' --exclude '.git'
            --exclude 'webpack' --exclude '.circleci' --exclude 'src' --exclude '.babelrc'
            --exclude '.browserslistrc' --exclude '.eslintrc' --exclude '.gitignore'
            --exclude '.prettierrc' --exclude '.stylelintignore' --exclude '.stylelintrc'
            --exclude 'env.json' --exclude 'package.json' --exclude 'yarn.lock' --exclude
            'README.md' ~/<< parameters.environment >>-theme ${username}@${hostname}:/var/www/html/${site_name}/wp-content/themes/


jobs:
  production:
    executor: node
    working_directory: ~/production-theme
    steps:
      - flow:
          environment: "production"
          yarn: "yarn prod-build"


  development:
    executor: node
    working_directory: ~/develop-theme
    steps:
      - flow:
          environment: "develop"
          yarn: "yarn test-build"

workflows:
  version: 2
  production_and_development:
    jobs:
      - development:
          filters:
            branches:
              only: develop
      - production:
          filters:
            branches:
              only: master

请注意,我刚刚修改了您的配置,但尚未对其进行测试,因此可能存在轻微的拼写错误。


推荐阅读