Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to CircleCI

yubessy
August 09, 2018

Introduction to CircleCI

社内勉強会用資料です

yubessy

August 09, 2018
Tweet

More Decks by yubessy

Other Decks in Programming

Transcript

  1. Agenda • جૅฤ • CI, CD ͱ͸ • Circle CI

    ͷߏ੒ཁૉ • جຊతͳ Job, Step ͷ࢖͍ํ • ൃలฤ • ߴ౓ͳ Job, Step ͷ࢖͍ํ • ߴ౓ͳ Workflow ͷ࢖͍ํ
  2. ໨త ໨ࢦ͢͜ͱ • CI, CD ͱ CircleCI ͷجຊతͳ֓೦Λཧղ͢Δ • CircleCI

    2.0 ͷ Job, Step Λ࢖͍͜ͳͤΔΑ͏ʹͳΔ ѻΘͳ͍͜ͱ • CircleCI 1.0 • ݸผͷݴޠɾϑϨʔϜϫʔΫಛ༗ͷ࿩
  3. Agenda • جૅฤ • CI, CD ͱ͸ • Circle CI

    ͷߏ੒ཁૉ • جຊతͳ Job, Step ͷ࢖͍ํ • ൃలฤ
  4. CI, CD ͱ͸ʁ ιϑτ΢ΣΞ։ൃʹ͓͚ΔɺϏϧυɾςετɾϦϦʔεͷΑ͏ͳ Ұ࿈ͷఆܕॲཧΛࣗಈԽʹΑΓܧଓతʹ܁ΓฦͤΔΑ͏ʹ͢Δ͜ͱ • CI = Continuous Integration

    = ܧଓతΠϯςάϨʔγϣϯ • ϦϦʔεʹࢸΔ·ͰͷϏϧυɾςετͳͲΛओʹѻ͏ • CD = Continuous Delivery = ܧଓతσϦόϦ • ϦϦʔεͦͷ΋ͷʢσϓϩΠɾϚΠάϨʔγϣϯͳͲʣΛओʹѻ͏
  5. CI, CD ͕͋ΔͱԿ͕خ͍͠ʁ CI, CD ͕ͳ͔ͬͨࠒ • Ϗϧυ: Ϗϧυ৬ਓʹຖճґଘؔ܎ͷղܾ͔Βґཔ •

    ςετ: ςελʔ͕਺ඦͷखॱͱ໨ࢹ֬ೝΛखಈͰ࣮ࢪ • ϦϦʔε: ΠϯϑϥΤϯδχΞ͕ຊ൪؀ڥͰ਺ेͷίϚϯυΛ࣮ߦ CI, CD Λ͏·͘΍Ε͹ɾɾɾ • Ϗϧυɾςετ: ίʔυΛ push ͢Δͱશ෦ࣗಈͰ࣮ߦ • ϦϦʔε: ४උ͕Ͱ͖ͨΒঝೝϘλϯΛԡ͚ͩ͢
  6. CircleCI ͱ͸ʁ Ϛωʔδυͷ CI, CD αʔϏε ಛ௃: • Github ͷ

    branch, tag ͷ push Λى఺ʹ೚ҙͷॲཧΛ࣮ߦ • 1.0 -> 2.0 Ͱେ෯ͳΞοϓσʔτ͕͋Γ΄ͱΜͲผ෺ • ݪଇͱͯ͠ίϯςφΛར༻ (VM΋࢖͑ͳ͍͜ͱ͸ͳ͍) • ॲཧ͸ݴޠ΍ϑϨʔϜϫʔΫʹґଘͤͣࣗ෼Ͱॻ͘
  7. Agenda • جૅฤ • CI, CD ͱ͸ • Circle CI

    ͷߏ੒ཁૉ • جຊతͳ Job, Step ͷ࢖͍ํ • ൃలฤ
  8. Workflow, Job, Step: ॲཧͷ֊૚Λ੒͢ཁૉ Ұ࿈ͷॲཧ͸ Workflow, Job, Step ͷॱʹ֊૚Խ͞ΕΔ workflows:

    main: jobs: - test: ... - deploy: ... jobs: test: docker: - image: circleci/python:3 steps: - checkout - run: python -m unittest deploy: ...
  9. Workflow ॲཧΛ·ͱΊΔ࠷΋େ͖ͳ୯ҐͰɺͻͱͭҎ্ͷ Job Ͱߏ੒͞ΕΔ workflows: main: jobs: - test: ...

    - deploy: ... ଟ͘ͷ৔߹͸ 1 ϨϙδτϦʹ͖ͭ 1 Workflow & 1 Job ͱͳΔ ࣮͸௚ྻɾฒྻɾ෼ذͳͲ͔ͳΓॊೈͳॲཧ͕Ͱ͖Δ (ൃలฤͰ঺հ)
  10. Job ॱ൪ʹ࣮ߦ͞ΕΔॲཧΛ·ͱΊΔ୯ҐͰɺͻͱͭҎ্ͷ Step Ͱߏ੒͞ΕΔ jobs: test: docker: - image: circleci/python:3

    steps: - checkout - run: python -m unittest - ... ॲཧͷ࣮ߦ؀ڥ͸جຊతʹ Docker ίϯςφͰఆٛ͢Δ
  11. Step ॲཧͷ࠷΋খ͞ͳ୯ҐͰɺ࣍ͷΑ͏ͳ͍͔ͭ͘ͷछྨ͕͋Δ (ൃలฤͰ΋঺հ) # ϨϙδτϦ͔ΒίʔυΛऔಘ - checkout # ίϚϯυΛ࣮ߦ -

    run: <command> # ίϚϯυΛ࣮ߦ (͜ͷΑ͏ʹॻ͘͜ͱ΋Ͱ͖Δ) - run: name: <name> command: <command> # ίϚϯυΛ࣮ߦ (run ͱ΄΅ಉ͕ͩ͡ฒྻ࣮ߦ͞Εͳ͍) - deploy: <command>
  12. Agenda • جૅฤ • CI, CD ͱ͸ • Circle CI

    ͷߏ੒ཁૉ • جຊతͳ Job, Step ͷ࢖͍ํ • ൃలฤ
  13. Job Λॻ͖࢝ΊΔલʹ ʮԿ͕ʯࣗಈԽʹඞཁ͔Λௐ΂Δ • ඞཁͳϥΠϒϥϦ͸Ͳ͏Πϯετʔϧ͢Δʁ • DBϚΠάϨʔγϣϯ͕ผͰ؅ཧ͞Ε͍ͯΔͳΒςετDBΛͲ͏ཱͯΔʁ • ίʔυ؅ཧͰ͖ͳ͍ΫϨσϯγϟϧ͸Ͳ͏΍ͬͯ౉͢ʁ ʮຊ౰ʹʯࣗಈԽ͢΂͖͔Λߟ͑Δ

    • ϦϦʔε·ͰࣗಈԽ͠ͳ͍ͱ͍͚ͳ͍ͷ͔ʁϏϧυɾςετͰे෼͔ʁ • ϓϧϦΫϚʔδͰࣗಈϦϦʔε͞Εͯେৎ෉͔ʁࣦഊ࣌ʹ੾Γ໭ͤΔ͔ʁ • ຊ൪؀ڥʹSSH͢Δඞཁ͕͋Δ͔ʁΫϥ΢υඪ४ͷσϓϩΠαʔϏεΛ࢖͑ͳ͍͔ʁ
  14. ͸͡ΊͷҰา ࠷ॳ͸ 1 Job Ͱ Workflow ͸লུͯ͠΋Α͍ (͜ΕͰ΋ Job ͸࣮ߦ͞ΕΔ)

    version: 2 jobs: build: docker: - image: circleci/ruby:2.4.1-jessie steps: - checkout - run: echo "A first hello"
  15. Docker ΠϝʔδΛબͿ ࣮ߦ؀ڥͱͯ͠࢖͏ Docker Πϝʔδ͸Α͘ߟ͑ͯબͿ ֤ Step ͷίϚϯυ͸ͻͱͭΊͷίϯςφͰ࣮ߦ͞ΕΔ͜ͱʹ஫ҙ docker: #

    ެࣜͷ `circleci/ruby` ΍ `circleci/mysql` ͳͲΛͳΔ΂͘࢖͏ - image: circleci/python:3.6.2 # ຊ൪؀ڥͱόʔδϣϯΛἧ͑Δ - image: circleci/mysql:5.7 # (ಉ্)
  16. Step Λద੾ͳ·ͱ·ΓͰ۠੾Δ # ͻͱ·ͱ·Γͷॲཧ͝ͱʹ Step Λ࡞໊ͬͯલΛ͚ͭΔ - run: name: ؀ڥߏங

    command: | python -m venv .venv source .venv/bin/activate pip install -r requirements.txt - run: name: ίʔυͷ੩తղੳ command: | source .venv/bin/activate pep8 . pyflakes . - run: name: ςετ command: | source .venv/bin/activate unittest . # test
  17. Step Λద੾ͳ·ͱ·ΓͰ۠੾Δ # ྑ͘ͳ͍ྫ: શ෦·ͱΊΔ - run: name: CI command:

    | python -m venv .venv source .venv/bin/activate pip install -r requirements.txt pep8 . pyflakes . unittest . # ྑ͘ͳ͍ྫ: 1ίϚϯυͣͭશ෦෼͚Δ - run: pip install -r requirements.txt - run: pep8 . - run: pyflakes . - run: unittest .
  18. Cache Λదٓར༻͢Δ ϥΠϒϥϦͷΠϯετʔϧύεͳͲΛΩϟογϡ͓ͯ͘͠ͱศར (ൃలฤͰ΋঺հ) - restore_cache: # Ωϟογϡ͋Ε͹͔ͦ͜ΒσΟϨΫτϦͷ಺༰Λ෮ݩ key: requirements-{{

    checksum "requirements.txt" }} - run: # ࣮ߦ͞ΕΔ͕σΟϨΫτϦ͕෮ݩ͞Ε͍ͯΕ͹࣮࣭తʹԿ΋ى͖ͳ͍ name: setup venv and pip install command: | python -m venv .venv source .venv/bin/activate pip install -r requirements.txt - save_cache: # σΟϨΫτϦͷ಺༰ΛΩϟογϡͱͯ͠อଘ key: requirements-{{ checksum "requirements.txt" }} paths: - .venv
  19. Workflow, Job, Step ʹ͍ͭͯͷ͓͞Β͍ • Circle CI ͷॲཧ͸ Workflow >

    Job > Step ʹ֊૚Խ͞ΕΔ • Workflow: 1ͭҎ্ͷ Job Λ௚ྻɾฒྻɾબ୒ɾ෼ذ࣮ͯ͠ߦ • Job: 1ͭҎ্ͷ Step Λ௚ྻ࣮ߦ • Step: ॲཧͷ࠷΋খ͍͞୯Ґ • checkout: ίʔυΛϨϙδτϦ͔ΒऔΓग़͢ • run, deploy: ίϚϯυ (γΣϧεΫϦϓτ) Λ࣮ߦ • savecache, restorecache: σΟϨΫτϦΛΩϟογϡͱͯ͠อଘɾಡࠐ
  20. Job: ઃఆ߲໨Ұཡ jobs: build: docker: # ࢖༻͢Δ Docker Πϝʔδ -

    image: circleci/ruby:2.4.1 environment: # ؀ڥม਺ FOO: bar parallelism: 2 # ฒྻ౓ resource_class: medium # CPU, RAM ͷεϖοΫ working_directory: /my-app # ϫʔΩϯάσΟϨΫτϦ branches: # ϒϥϯνϑΟϧλ only: - master steps: ...
  21. Job: parallelism ʹΑΔฒྻԽ 1. parallelism Λ 2 Ҏ্ʹ͢Δ 2. ฒྻ࣮ߦ͍ͨ͠ॲཧΛ

    circleci tests ίϚϯυͰฒྻԽ͢Δ parallelism: 2 steps: - run: command: | circleci tests glob "spec/**/*_spec.rb" > list.txt circleci tests split list.txt | xargs bundle exec rspec
  22. Job: circleci tests ͷղઆ جຊతʹ͸ϑΝΠϧͷϦετͷऩूͱ෼ׂΛ͢Δ͚ͩͷίϚϯυ $ circleci tests glob "spec/**/*_spec.rb"

    > list.txt $ cat list.txt spec/test1_spec.rb spec/test2_spec.rb spec/test3_spec.rb spec/test4_spec.rb $ circleci tests split list.txt spec/test1_spec.rb spec/test2_spec.rb spec/test3_spec.rb spec/test4_spec.rb
  23. Step: run ͷઃఆ߲໨ run: name: rspec # Webίϯιʔϧʹදࣔ͞ΕΔεςοϓ໊ command: bundle

    exec rspec # ίϚϯυ಺༰ shell: bash -lc # ίϚϯυΛ࣮ߦ͢ΔγΣϧ environment: # ؀ڥม਺ FOO: bar working_directory: /my-app # ϫʔΩϯάσΟϨΫτϦ background: false # όοΫάϥ΢ϯυ࣮ߦ͢Δ͔൱͔ no_output_timeout: 1m # Ұఆ࣌ؒԿ΋ग़ྗ͕ͳ͚Ε͹λΠϜΞ΢τ when: # (always, on_success, on_fail) Ͳ͏͍͏৔߹ʹ࣮ߦ͢Δ͔
  24. Step: save_cache, restore_cache (࠶ܝ) ϥΠϒϥϦͷΠϯετʔϧͳͲͷ࣌ؒΛ୹ॖ - restore_cache: # Ωϟογϡ͋Ε͹͔ͦ͜ΒσΟϨΫτϦͷ಺༰Λ෮ݩ key:

    requirements-{{ checksum "requirements.txt" }} - run: # ࣮ߦ͞ΕΔ͕σΟϨΫτϦ͕෮ݩ͞Ε͍ͯΕ͹࣮࣭తʹԿ΋ى͖ͳ͍ name: setup venv and pip install command: | python -m venv .venv source .venv/bin/activate pip install -r requirements.txt - save_cache: # σΟϨΫτϦͷ಺༰ΛΩϟογϡͱͯ͠อଘ key: requirements-{{ checksum "requirements.txt" }} paths: - .venv
  25. Step: persist_to_workspace, attach_workspace ͋Δ Job ͷ੒Ռ෺Λޙଓͷผͷ Job Ͱར༻͢Δ persist_to_workspace: root:

    dist paths: '*.tar.gz' attach_workspace: at: dist Workflow Ͱෳ਺ͷ Job Λ૊Έ߹ΘͤΔ৔߹ʹ࢖͏
  26. Workflow ͱ͸ʁ ෳ਺ͷ Job Λ૊Έ߹ΘͤͯෳࡶͳॲཧϑϩʔΛ࣮ݱ͢Δػೳ • ௚ྻ࣮ߦɾฒྻ࣮ߦ • લͷ Job

    ͷ੒Ռ෺Λར༻ • Git ͷ branch, tag ʹΑΔϑΟϧλϦϯά • Manual Approval (ਓʹΑΔঝೝ) • Nightly Scheduling (git push ʹΑΒͳ͍ఆظ࣮ߦ)
  27. Workflow: ෳ਺ͷ Job ͷఆٛ version: 2 jobs: build: docker: ...

    steps: ... test: docker: ... steps: ... workflows: version: 2 build-and-test: jobs: # ͜ͷ··ͩͱ build ͱ test ͸ฒྻ࣮ߦ͞ΕΔ - build - test
  28. Workflow: Job ͷ௚ྻ࣮ߦ requires Ͱґଘ͢Δ Job Λࢦఆ͢Δ͜ͱͰ௚ྻ࣮ߦͰ͖Δ workflows: version: 2

    build-and-test: jobs: - build - test: requires: # build ͕ऴΘ͔ͬͯΒ test Λ࣮ߦ - build
  29. Workflow: લͷ Job ͷ੒Ռ෺Λར༻ build ͷ੒Ռ෺Λ test Ͱ࢖͏ = persist_to_workspace,

    attach_workspace jobs: build: steps: - persist_to_workspace: # dist/ ҎԼͷ *.tar.gz ϑΝΠϧΛอଘ root: dist paths: '*.tar.gz' test: steps: - attach_workspace: # อଘ͞Εͨ dist/ ҎԼͷϑΝΠϧΛಡࠐ at: dist
  30. Workflow: Git ͷ branch, tag ʹΑΔϑΟϧλϦϯά ಛఆͷ໊લͷ branch ΍ tag

    ͷ৔߹͚ͩ Job Λ࣮ߦ͍ͨ͠৔߹ workflows: version: 2 test-and-deploy: jobs: - test - deploy: requires: # test ͕ऴΘ͔ͬͯΒ deploy Λ࣮ߦ - test filters: branches: # master ͷ৔߹ͷΈ deploy Λ࣮ߦ only: /^master$/ # ਖ਼نදݱ
  31. Workflow: Manual Approval ਓʹΑΔঝೝεςοϓΛڬΉ workflows: version: 2 lint-and-test: jobs: -

    lint # lint ͸ຖճ࣮ߦ - approve-test: # test ʹਐΉ͜ͱΛखಈͰঝೝ type: approval requires: - lint - test: # test ͸͕͔͔࣌ؒΔͷͰঝೝΛඞཁʹ͢Δ requires: - approve-test
  32. Workflow: Nightly Scheduling ಛఆͷ Workflow Λఆظతʹ࣮ߦ workflows: version: 2 health:

    triggers: - schedule: # ຖ೔ 00:00 ʹ࣮ߦ cron: "0 0 * * *" jobs: # ੬ऑੑͷνΣοΫͳͲ - check-vulnerability