我对持续集成还很陌生,到目前为止我非常喜欢它。我想知道是否有可能使配置文件更好地可读和简化。
我之所以问这个问题,是因为我在开发和生产模式中使用相同的工作流程。
如何保持配置“干燥”,而不需要一遍又一遍地复制粘贴相同的配置文件?
我可以用 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 配置的“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: ...
下。与此同时,工作
变得相当渺茫,因为它们将重用“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
请注意,我刚刚修改了您的配置,但尚未对其进行测试,因此可能存在轻微的拼写错误。
关于bash - 如何简化 YAML CircleCI 配置以获得更好的可读性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57374727/