Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Readable CircleCI Config
Search
Sawada Shota
April 23, 2019
Programming
640
5
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Readable CircleCI Config
Sawada Shota
April 23, 2019
More Decks by Sawada Shota
See All by Sawada Shota
Introduce A Talk “The Athens Project - A Proxy Server for Go Modules”
sawadashota
1
1.2k
Other Decks in Programming
See All in Programming
霧の中の代数的エフェクト
funnyycat
1
430
Laravelで学ぶ Webアプリケーションチューニング入門/web_application_tuning_101
hanhan1978
4
1.3k
Hatena Engineer Seminar #37「言語モデルの活用に関する研究」
slashnephy
0
550
「正の参照」と 「負の導出」で組む ハーネスエンジニアリング
cottpan
1
150
AI駆動開発を妨げる技術的負債の解消アプローチ / ai-refactoring-approach
minodriven
17
9.3k
PHP初心者セッション2026 〜生成AIでは見えない裏側を知る:今だからLAMPを通して仕組みを学ぶ〜
kashioka
0
670
Embedded SREと共に達成した会員管理システムのAWS移行 - SRE NEXT 2026 ランチスポンサーセッション
niftycorp
PRO
1
3k
ITヒヤリハットを整理してみた ~ライフサイクルと原因から考える再発防止策~
koukimiura
1
110
【やさしく解説 設計編・中級 #1】一つの車に、運転手は一人 ~ある倉庫システムの事例から~
panda728
PRO
0
190
Apache Hive: そしてCloud Native Lakehouseへ
okumin
1
170
分散システム、なんですぐ死んでしまうん?耐障害性を高めたいあなたのためのレジリエンスパターン入門
mshibuya
7
6.9k
Android CLI
fornewid
0
170
Featured
See All Featured
Leading Effective Engineering Teams in the AI Era
addyosmani
9
2.2k
Agile that works and the tools we love
rasmusluckow
331
22k
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
2
4.2k
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
400
Google's AI Overviews - The New Search
badams
0
1.1k
Building a Scalable Design System with Sketch
lauravandoore
463
34k
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
Ecommerce SEO: The Keys for Success Now & Beyond - #SERPConf2024
aleyda
1
2.1k
How to train your dragon (web standard)
notwaldorf
97
6.7k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
12
1.2k
Faster Mobile Websites
deanohume
310
32k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
8.2k
Transcript
None
Shota Sawada • CrowdWorks Inc. • SRE/Auth/Recruit • Twitter: @xioota
• Others: @sawadashota 2
None
◎ 2016.06 CircleCI1.0利用開始 (127 lines) ◎ 2017.03 CircleCI2.0に移行 (332 lines)
◎ 2018.11 CircleCI2.1に移行 (924 lines) ◎ 2019.03 巨大configファイルを分割 (1059 lines) 4
None
◎ 2016.06 CircleCI1.0利用開始 (127 lines) ◎ 2017.03 CircleCI2.0に移行 (332 lines)
◎ 2018.04 澤田入社 (855 lines) ◎ 2018.11 CircleCI2.1に移行 (924 lines) ◎ 2019.03 巨大configファイルを分割 (1059 lines) 6
◎ 2016.06 CircleCI1.0利用開始 (127 lines) ◎ 2017.03 CircleCI2.0に移行 (332 lines)
◎ 2018.04 澤田入社 (855 lines) ◎ 2018.11 CircleCI2.1に移行 (924 lines) ◎ 2019.03 巨大configファイルを分割 (1059 lines) 7
None
None
本日のテーマ
None
◎ Executors ◎ Commands ◎ Parameterized Jobs ◎ Orbs 12
複数のjobで実行環境を使い回すためのしくみ 13 version: 2.1 executors: ruby-executor: docker: - image: circleci/ruby:2.6
jobs: hello-executor: executor: ruby-executor steps: - run: echo hello executor
複数のjobでstepを使い回すためのしくみ。パラメータが渡せる 14 version: 2.1 jobs: notify: docker: - image: cibuilds/base:latest
steps: - slack-notify: message: hello commands
workflow中でjobにパラメータを渡せるようになった 15 version: 2.1 workflows: my-workflow: jobs: - slack-notify: message:
hello commands
commandsや jobs、executors をパッケージとして使い回すことのでき る仕組み 16 version: 2.1 orbs: slack: circleci/
[email protected]
workflows: my-workflow: jobs: - slack/approval-notification: message: Pending approval
◎ 偉人の作った記述をOrbとして取り込むことができるように なった ◎ 重複する記述を共通化できるようになった(yamlのaliasから も脱却) ◎ 肥大化していたJobの記述を読みやすい粒度にまとめるこ とができるようになった 17
None
◎ そのOrbがなにやってるか他の人、わかる? ◎ どういう基準でCommandを作る? ◎ パラメータのとり方、それでいい? ◎ 画面に収まらないWorkflowsの記述 ◎ Job、Commands、Workflowsの全体像を把握しにくい
19
None
None
◎ Orbがやってることを他の人が把握しにくい ◎ どういう基準でCommandに切り出していくか ◎ パラメータ設計 ◎ 縦長になりがちなWorkflows ◎ 巨大configファイルの分割
22
◎ Orbがやってることを他の人が把握しにくい ◎ どういう基準でCommandに切り出していくか ◎ パラメータ設計 ◎ 縦長になりがちなWorkflows ◎ 巨大configファイルの分割
23
Orbを使うときはyamlを読むだけではどんなことをしているかわからないので、 OrbのURLやコメントを多めに書いておく 24 version: 2.1 orbs: # Slack通知をするためのorb # https://circleci.com/orbs/registry/orb/circleci/slack
slack: circleci/
[email protected]
workflows: my-workflow: jobs: # Deployの準備ができたことをSlackに通知する # approveしたときのみdeployされる - slack/approval-notification: message: Pending approval
PRのレビューであればconfigurationタブから、そうでない場合はcircleci CLIで circleci config processコマンドでCircleCI2.0に展開された configを読むことができる 25 https://circleci.com/gh/sawadashota/circleci-meetup-demo/2#config/containers/0
◎ Orbがやってることを他の人が把握しにくい ◎ どういう基準でCommandに切り出していくか ◎ パラメータ設計 ◎ 縦長になりがちなWorkflows ◎ 巨大configファイルの分割
26
• ただし、実行環境に依存している処理は無理にCommandに切り出さない • Yaml故に、デッドコードに気づきにくいので、1回しか呼ばれないものは無 理にcommand化しないほうが運用的によい。代わりにコメントで区切る 27 jobs: yarn-install: executor: my-executor
steps: - https-shallow-clone-checkout - yarn-install: persist-to-workspace: true
◎ Orbがやってることを他の人が把握しにくい ◎ どういう基準でCommandに切り出していくか ◎ パラメータ設計 ◎ 縦長になりがちなWorkflows ◎ 巨大configファイルの分割
28
• 中心となる処理 • 付随して必ず行われる処理 • 一緒に使うことが多いが、必ずしも必要にならない処理 => オプション化 29
30 commands: install: parameters: preinstall-foo: type: boolean default: false steps:
- run: echo "preinstall is << parameters.preinstall-foo >>" - when: condition: << parameters.preinstall-foo >> steps: - run: echo "preinstall" - unless: condition: << parameters.preinstall-foo >> steps: - run: echo "don't preinstall" https://circleci.com/docs/2.0/reusing-config/#defining-conditional-steps
31 jobs: test: executor: my-executor steps: - install: preinstall-foo: true
- install: preinstall-foo: false - install # preinstall-foo: false と同じ https://circleci.com/docs/2.0/reusing-config/#defining-conditional-steps
• 中心となる処理 => bundle install • 付随して必ず行われる処理 => cacheのリストアと保存 •
一緒に使うことが多いが、必ずしも必要にならない処理 => workspaceへ の永続化 32
33 commands: bundle-install: description: BundlerでGemをインストールします。 parameters: persist-to-workspace: description: インストールしたGemをWorkspaceに永続化するかを指定します。 type:
boolean default: false steps: # bundle installやcache利用・保存など - when: condition: << parameters.persist-to-workspace >> steps: - persist_to_workspace: root: /usr/src/app paths: - vendor/bundle - .bundle/config https://circleci.com/docs/2.0/reusing-config/#defining-conditional-steps
◎ Orbがやってることを他の人が把握しにくい ◎ どういう基準でCommandに切り出していくか ◎ パラメータ設計 ◎ 縦長になりがちなWorkflows ◎ 巨大configファイルの分割
34
• filterの重複記述 • Parameterized Jobs => Workflowsでは全体のフローを俯瞰して見やすくしたい 35
36
37 workflows: my-workflow: jobs: - static-checks: filters: branches: only: master
- yarn-install: filters: branches: only: master - bundle-install: filters: branches: only: master - jest: requires: - yarn-install - rspec: requires: - yarn-install - bundle-install
38 workflows: my-workflow: jobs: - static-checks: filters: branches: only: master
- yarn-install: filters: branches: only: master - bundle-install: filters: branches: only: master - jest: requires: - yarn-install - rspec: requires: - yarn-install - bundle-install
• Workflowの発火条件が複数箇所に書かれている • 似たWorkflowをコピペで作ったときにミスりやすい 39
40
41 workflows: my-workflow: jobs: - start: filters: branches: only: master
- static-checks: requires: - start - yarn-install: requires: - start - bundle-install: requires: - start - jest: requires: - yarn-install - rspec: requires: - yarn-install - bundle-install
None
• パラメータの分、Workflowが縦長になる • Workflow上で複数回使ったときに、意味が俯瞰しにくい 43
44 workflows: my-workflow: jobs: - build: filters: branches: only: master
- push: requires: - build - deploy: cluster_name: my-ecs-stg-cluster service_name: my-ecs-stg-service image: my-image aws_access_key_id: AWS_ACCESS_KEY_ID aws_secret_access_key: AWS_SECRET_ACCESS_KEY requires: - push - deploy: cluster_name: my-ecs-prod-cluster service_name: my-ecs-prod-service image: my-image aws_access_key_id: AWS_ACCESS_KEY_ID aws_secret_access_key: AWS_SECRET_ACCESS_KEY requires: - push
45
46 workflows: my-workflow: jobs: - build: filters: branches: only: docker
- push: requires: - build - deploy-staging: requires: - push - deploy-production: requires: - push
47 jobs: deploy-staging: docker: - image: cibuilds/base steps: - ecs-deploy:
cluster_name: my-ecs-stg-cluster service_name: my-ecs-stg-service image: my-image aws_access_key_id: AWS_ACCESS_KEY_ID aws_secret_access_key: AWS_SECRET_ACCESS_KEY deploy-production: docker: - image: cibuilds/base steps: - ecs-deploy: cluster_name: my-ecs-prod-cluster service_name: my-ecs-prod-service image: my-image aws_access_key_id: AWS_ACCESS_KEY_ID aws_secret_access_key: AWS_SECRET_ACCESS_KEY
48
◎ Orbがやってることを他の人が把握しにくい ◎ どういう基準でCommandに切り出していくか ◎ パラメータ設計 ◎ 縦長になりがちなWorkflows ◎ 巨大configファイルの分割
49
None
51 .circleci ├── README.md ├── config.yml └── src ├── commands
│ ├── @slack-on-fail-deploy.yml │ ├── assets-precompile.yml │ ├── assets-sync.yml │ ├── bundle-install.yml │ ├── configure-rails.yml │ ├── docker-build.yml │ ├── docker-tag-copy.yml │ ├── ecs-run-task-migration.yml │ ├── ecs-update-service.yml │ ├── https-shallow-clone-checkout.yml │ ├── restore-database.yml │ ├── setup-database.yml │ └── yarn-install.yml ├── config.yml ├── executors.yml (続く) (続き) ├── jobs │ ├── @staging-deploy.yml │ ├── assets-precompile.yml │ ├── bundle-install.yml │ ├── check-config-pack.yml │ ├── check-landing-pages-builds.yml │ ├── generate_yard_documents.yml │ ├── jest.yml │ ├── report-coverage.yml │ ├── rspec.yml │ ├── setup-database.yml │ ├── start.yml │ ├── static-checks.yml │ ├── validate-dwh-tag.yml │ ├── validate-factory.yml │ └── yarn-install.yml ├── orbs.yml └── workflows ├── @workflows.yml ├── staging-deploy.yml ├── test.yml └── validate-config.yml
• .circleci/src/以下にファイルを分割する • pushする前に.circleci/config.ymlにまとめる • .circleci/config.ymlが最新であるかチェックするjobを追加 • .circleci/README.md書く 52
• circleci config packコマンドで.circleci/config.ymlを生成する 53 $ circleci config pack .circleci/src
> .circleci/config.yml
54 resource_class: small docker: # https://github.com/CircleCI-Public/circleci-cli - image: circleci/circleci-cli:alpine steps:
- checkout - run: name: CircleCIの設定ファイルを再生成して、コミットされているファイルと差分がないかチェック command: | circleci config pack .circleci/src > .circleci/config.yml git diff --exit-code .circleci/src/jobs/check-config-pack.yml
• 運用の方法を書く • 生成された.circleci/config.ymlにはコメントが反映されないので、全体的 な記述などはREADMEに書く 55
• 肥大化しがちなJobやCommands、Workflowsをファイルごとにわけること で、1ファイルの中で行き来するストレスが減った • JobやCommandsがファイル名になっているので、全体を俯瞰しやすく なった 56 • 公式の使い方じゃないので、ローカルルールを決めて運用する必要があ る
• config.ymlが小さいうちはほぼメリットがない
◎ Orbを読み解くときは展開されたconfigで読もう ◎ CommandはJobが何をしているか俯瞰して読みやすい粒 度になるようにしよう ◎ パラメータを適切に設計しよう ◎ Workflowの発火条件をまとめよう ◎
Parameterized Jobsは安易に使わないようにしよう ◎ 巨大configファイルは分割Orbのごとく分割しよう 57
58