Bash 如何简化YAML CircleCI配置以提高可读性?

Bash 如何简化YAML CircleCI配置以提高可读性?,bash,continuous-integration,yaml,devops,circleci,Bash,Continuous Integration,Yaml,Devops,Circleci,我对持续集成非常陌生,到目前为止我非常喜欢它。我想知道是否有可能使配置文件更易于阅读和简化 我之所以问这个问题,是因为我在开发和生产模式中使用了相同的工作流 如何在不反复复制粘贴同一配置文件的情况下保持配置“干燥” 我可以用BASH脚本来代替吗 我的配置: version: 2 jobs: production: working_directory: ~/production-theme docker: - image: circleci/node:10.16

我对持续集成非常陌生,到目前为止我非常喜欢它。我想知道是否有可能使配置文件更易于阅读和简化

我之所以问这个问题,是因为我在开发生产模式中使用了相同的工作流

如何在不反复复制粘贴同一配置文件的情况下保持配置“干燥”

我可以用BASH脚本来代替吗

我的配置:

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
您可以使用CircleCI配置的“命令”和“执行器”键

详情如下:

应用它,您的配置可以变得更具可读性和更短

将docker定义包装在“执行者”中:

所有步骤定义都组合在
命令:flow:…
下。与此同时,
作业
变得非常精简,因为它们将重用“flow”
命令

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

版本:2.1
遗嘱执行人:
节点:
码头工人:
-图:circleci/节点:10.16
命令:
流量:
参数:
环境:
类型:字符串
默认值:“生产”
纱线:
类型:字符串
默认值:“纱线产品构建”
步骤:
-添加\u ssh\u密钥:
指纹:
-xxx
-存储测试结果:
路径:测试结果
-结帐
-还原U缓存:
名称:Restore Thread包&Packge.json缓存
钥匙:
-纱线包装-{{校验和“纱线.锁”}
-v1依赖项-{{checksum“package.json”}
-纱线包装-
-v1依赖项-
-运行:
名称:安装纱线包
命令:纱线安装
-运行:
名称:楼宇回购
命令:>
-保存缓存:
名称:保存纱线包缓存
键:依赖项缓存-{{checksum“package.json”}
路径:
-/node_模块
-运行:
名称:运行SSH密钥扫描
命令:ssh keyscan${hostname}>~/.ssh/known\u hosts
-运行:
名称:安装Rysnc
命令:sudo apt get install rsync
-运行:
名称:将文件上载到主题文件夹
命令:rsync-avP--delete before--exclude'node_modules'--exclude.git'
--排除'webpack'--exclude.circleci'--exclude'src'--exclude.babelrc'
--exclude'.browserslistrc'-exclude'.eslintrc'-exclude'.gitignore'
--排除'.prettierrc'--exclude'.stylelintignore'--exclude'.stylelintrc'
--排除'env.json'--exclude'package.json'--exclude'spirn.lock'--exclude
'README.md'~/>-theme${username}@${hostname}:/var/www/html/${site_name}/wp content/themes/
工作:
制作:
执行者:节点
工作目录:~/生产主题
步骤:
-流量:
环境:“生产”
纱线:“纱线产品构建”
发展:
执行者:节点
工作目录:~/develope主题
步骤:
-流量:
环境:“发展”
纱线:“纱线测试构建”
工作流程:
版本:2
生产和开发:
工作:
-发展:
过滤器:
分支机构:
唯一:发展
-制作:
过滤器:
分支机构:
仅限:船长

请注意,我刚刚修改了您的配置,但尚未对其进行测试,因此可能会出现一些小的输入错误。

Hi Mv!非常感谢您的帮助和建议。我尝试过,但在运行时出现以下错误。[#/jobs/production]预期类型:String,已找到:Mapping#| | |作业可能是对另一个作业的字符串引用| | 2。[#/jobs/production/steps/0/flow]在总共2个子模式中没有匹配的子模式#| | | | | 1。[#/jobs/production/steps/0/flow]预期类型:映射,找到:SequenceHello你在吗?@Galanthus请检查上面更新的代码。在仔细检查CircleCI文档后,似乎使用参数调用可重用命令必须使用“对象”语法,而不是“数组”。请注意“jobs/production/steps”和“jobs/development/steps”中更新的定义。您可以尝试YAML引用和锚定,它们允许您定义YAML部分,然后重用它。这是YAML规范的一部分,而不是CircleCI的东西。CircleCI中使用的YAML解析器似乎确实支持它。
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