$30 off During Our Annual Pro Sale. View Details »

Automation with Wercker and Container Builder

Automation with Wercker and Container Builder

Automate not only your app but also your build env.

Keishin Yokomaku

March 24, 2017
Tweet

More Decks by Keishin Yokomaku

Other Decks in Technology

Transcript

  1. Automation with Wercker and Container Builder Keishin Yokomaku / shibuya.apk

    #13
  2. Automation with Wercker and Container Builder About Me ▸ Keishin

    Yokomaku ▸ Drivemode, Inc. / Principal Engineer ▸ KeithYokoma: GitHub / Twitter / Qiita / Tumblr / Stack Overflow ▸ Books: Mobile App Dev Guide / Android Academia / Grimoire of Android ▸ Fun: Gymnastics / Cycling / Photography / Motorsport ▸ Today’s Quote: “Power is everything.” 2
  3. Automation with Wercker and Container Builder 3

  4. 4 “Wercker͸͍͍ͧ”

  5. Automation with Wercker and Container Builder Wercker? ▸ Docker-native CI/CD

    Automation Platform ▸ ࣗಈͰ Docker ίϯςφΛςετɺσϓϩΠͰ͖ΔαʔϏε ▸ Docker ΠϝʔδΛ pull ͖ͯͯ͠ίϯςφΛ্ཱͪ͛Δ ▸ ίϯςφ಺ͰίϚϯυΛ࣮ߦ͢Δ ▸ ύΠϓϥΠϯͰςετͱσϓϩΠΛ෼཭Ͱ͖Δ ▸ ςετ͕ਖ਼ৗऴྃͨ͠ͱ͖ͷΈσϓϩΠ͕࣮ߦ͞ΕΔ 5
  6. Automation with Wercker and Container Builder Wercker for Android CI

    ▸ Docker Πϝʔδ͑͋͞Ε͹Ϗϧυ͕Ͱ͖Δ ▸ Android ΞϓϦΛϏϧυ͢ΔͨΊʹඞཁͳ΋ͷ͕͋Ε͹… ▸ Android SDK & NDK ▸ Java ▸ etc… 6
  7. Automation with Wercker and Container Builder Wercker for Android CD

    ▸ Android ΞϓϦͷσϓϩΠ => apk ͷ഑৴ ▸ apk Λ഑৴ͯ͘͠ΕΔαʔϏεʹσϓϩΠ͢Ε͹… ▸ DeployGate ▸ fastlane ▸ etc… 7
  8. 8 “׬શʹཧղͨ͠”

  9. Automation with Wercker and Container Builder Steps 1.Dockerfile ͔Β Docker

    ΠϝʔδΛϏϧυ 2.Ϗϧυͨ͠ΠϝʔδΛ dockerhub ͳͲͷϨδετϦʹొ࿥ 3.Ϗϧυ͍ͨ͠ϦϙδτϦʹwercker.yml Λ഑ஔ 4.Wercker ͷઃఆ 9
  10. Automation with Wercker and Container Builder Building a Docker image

    ▸ Dockerfile Λͭ͘Ζ͏(ྫ) ▸ Ubuntu Λϕʔεʹ ▸ Android SDK ͱ Java ΛΠϯετʔϧͯ͠ ▸ ඞཁͳ؀ڥม਺Λ੔͑ͨΠϝʔδ ▸ e.g. http://bit.ly/2ngAz0S 10
  11. Automation with Wercker and Container Builder Pushing the image to

    a registry ▸ Wercker ͕ѻ͑ΔϨδετϦ ▸ Docker Hub ▸ Google Container Registry ▸ Amazon ECR ▸ Private Registry(quay.io) ▸ ϓϥΠϕʔτϦϙδτϦʹ͠·͠ΐ͏ (http://bit.ly/2nuwkz9) 11
  12. Automation with Wercker and Container Builder Build an image using

    ‘docker-machine' (mac) $ docker-machine start image-builder (mac) $ docker-machine ssh image-builder (docker-machine) $ ls Dockerfile (docker-machine) $ docker build -t account/repository:tag . (docker-machine) $ docker login (docker-machine) $ docker push account/repository:tag (docker-machine) $ exit (mac) $ docker-machine stop image-builder 12
  13. Automation with Wercker and Container Builder Configure wercker.yml ▸ Android

    ΞϓϦͷϦϙδτϦʹ YAML ϑΝΠϧΛஔ͘ ▸ ໊લ͸ඞͣ wercker.yml ▸ ϏϧυɺσϓϩΠͦΕͧΕͷϋογϡ಺ʹϏϧυʹඞཁͳखॱΛॻ͘ ▸ ϏϧυͱσϓϩΠͰҟͳΔ Docker ΠϝʔδΛ࢖༻Մೳ 13
  14. Automation with Wercker and Container Builder Configure wercker.yml build: box:

    id: account/repository username: $USERNAME password: $PASSWORD tag: tag steps: - script: name: assemble code: | ./gradlew —stacktrace —project-cache-dir=$WERCKER_CACHE_DIR assemble - script: name: test code: | ./gradlew —stacktrace —project-cache-dir=$WERCKER_CACHE_DIR test lint 14
  15. Automation with Wercker and Container Builder Configure wercker.yml build: box:

    id: account/repository username: $USERNAME password: $PASSWORD tag: tag steps: - script: name: assemble code: | ./gradlew —stacktrace —project-cache-dir=$WERCKER_CACHE_DIR assemble - script: name: test code: | ./gradlew —stacktrace —project-cache-dir=$WERCKER_CACHE_DIR test lint 15
  16. Automation with Wercker and Container Builder Configure wercker.yml build: box:

    id: account/repository username: $USERNAME password: $PASSWORD tag: tag steps: - script: name: assemble code: | ./gradlew —stacktrace —project-cache-dir=$WERCKER_CACHE_DIR assemble - script: name: test code: | ./gradlew —stacktrace —project-cache-dir=$WERCKER_CACHE_DIR test lint 16
  17. Automation with Wercker and Container Builder Tips ▸ 1ͭͷεςοϓ͸10෼ͰλΠϜΞ΢τ ▸

    ίϚϯυͷ࣮ߦ͕௕͘ͳΔͱ͖͸෼͚Δ ▸ Wercker ͷ Web ΠϯλϑΣʔεͰͷग़ྗ͕͔ͳΓγϯϓϧ ▸ ࠔͬͨͱ͖ʹ࢖͍͍ͨ৘ใ͸ echo Ͱग़ྗ͢ΔεςοϓΛ࡞Δ ▸ ϩάग़ྗ͕௕͍ͱ Web ΠϯλϑΣʔε͕ܹॏͳͷͰదٓௐ੔ 17
  18. Automation with Wercker and Container Builder Saving artifacts ▸ after-steps

    ʹखॱΛॻ͘ ▸ ؀ڥม਺ʹอଘ৔ॴ͕ఆٛͯ͋͠Δ ▸ $WERCKER_REPORT_ARTIFACTS_DIR ▸ ೖΕ͍ͨ΋ͷ͚ͩ cp ͢Δ ▸ อଘͨ͠੒Ռ෺͸ Web ͔Βμ΢ϯϩʔυͰ͖Δ ▸ ৔ॴ͕෼͔ΓͮΒ͍ͷͰ஫ҙ 18
  19. Automation with Wercker and Container Builder Saving artifacts 19

  20. Automation with Wercker and Container Builder Saving artifacts after-steps: -

    script: name: save artifacts code: | ls -la ./app/build/outputs/ cp -r ./app/build/outputs/* ${WERCKER_REPORT_ARTIFACTS_DIR} cp -r ./app/build/reports/* ${WERCKER_REPORT_ARTIFACTS_DIR} 20
  21. Automation with Wercker and Container Builder Saving artifacts after-steps: -

    script: name: save artifacts code: | ls -la ./app/build/outputs/ cp -r ./app/build/outputs/* ${WERCKER_REPORT_ARTIFACTS_DIR} cp -r ./app/build/reports/* ${WERCKER_REPORT_ARTIFACTS_DIR} 21
  22. Automation with Wercker and Container Builder Connect Wercker with GitHub

    22
  23. Automation with Wercker and Container Builder Connect Wercker with GitHub

    ▸ ϦϙδτϦΛબΜͰ… ▸ ΦʔφʔΛબΜͰ… ▸ σϓϩΠΩʔΛ௥Ճͯ͠… ▸ public ͳϓϩδΣΫτͷ৔߹͸ https ܦ༝ͰΞΫηε͢ΔઃఆͰ΋ OK ▸ ׬ྃʂ 23
  24. 24

  25. Automation with Wercker and Container Builder ӡ༻͢Δͱݟ͑ͯ͘Δ Wercker ͷखؒ ▸

    SDK ͷΞοϓσʔτ΍αϙʔτϦϙδτϦͷΞοϓσʔτ ▸ Docker ΠϝʔδΛ࡞Γ௚͢͜ͱʹͳΔ͕…ʁ ▸ ຖճ docker-machine ͰϏϧυ…ʁ ▸ docker-machine ͱ Android Emulator ͸ಉ࣌ʹಈ͔ͤͳ͍ͧ…ʁ ▸ Πϝʔδ࡞Δ͚ͩͰ 10 ෼Ҏ্͔͔ΔΜ͚ͩͲ…ʁ ▸ λάͷ؅ཧΛࣗ෼Ͱߟ͑Δͷ໘౗͍͘͞Μ͚ͩͲ…ʁ 25
  26. 26

  27. 27 “ͦ͜ͰContainer BuilderͰ͢Α”

  28. Automation with Wercker and Container Builder Container Builder? ▸ ͜Ε͸Կʁ

    ▸ Google ͷαʔϏε ▸ Dockerfile ΛΘͨ͢ͱࣗಈͰ Docker ΠϝʔδΛ࡞ͬͯϨδετϦʹొ࿥ͯ͠ ͘ΕΔ͍͢͝΍ͭ 28
  29. Automation with Wercker and Container Builder Container Builder? ▸ ͜Ε͸Կʁ

    ▸ Google ͷαʔϏε ▸ Dockerfile ΛΘͨ͢ͱࣗಈͰ Docker ΠϝʔδΛ࡞ͬͯϨδετϦʹొ࿥ͯ͠ ͘ΕΔ͍͢͝΍ͭ 29
  30. Automation with Wercker and Container Builder Container Builder ▸ Πϝʔδͷ

    TAG ͷ؅ཧํ๏ΛઃఆͰ͖Δ ▸ ݻఆͰ latest ͷ·· ▸ git ͷίϛοτ ID Λλάʹ͢Δ ▸ etc… ▸ GitHub ͱ࿈ܞͰ͖Δ 30
  31. Automation with Wercker and Container Builder Workflow of updating Docker

    image ▸ Dockerfile Λฤू ▸ ίϛοτΛ push ▸ Container Builder ͕ϏϧυΛ࢝ΊΔ ▸ ׬ྃͨ͠Β৽͍͠λάΛ wercker.yml ʹઃఆ͢Δ 31
  32. 32 “׬શʹཧղͨ͠”

  33. Automation with Wercker and Container Builder Configure Container Builder ▸

    Google Cloud Console ͔Β Container Registry Λ։͘ ▸ Build triggers ͔Β GitHub ͷϦϙδτϦΛબ୒ͯ͠τϦΨʔΛઃఆ ▸ Ͳͷϒϥϯνͷ push ͰϏϧυΛ࢝ΊΔ͔ ▸ Dockerfile ͷ৔ॴ ▸ Πϝʔδ໊ 33
  34. Automation with Wercker and Container Builder Configure Container Builder ▸

    Dockerfile ͷϏϧυ ▸ 10෼Ҏ্͔͔ΔͱλΠϜΞ΢τ͢Δ ▸ cloudbuild.yaml Λ͔ͭͬͯௐ੔͢ΔΑ͏มߋ 34
  35. Automation with Wercker and Container Builder cloudbuild.yaml timeout: 40m steps:

    - name: gcr.io/cloud-builders/docker args: ['build', '--tag=gcr.io/$PROJECT_ID/$REPO_NAME:$COMMIT_SHA', '.'] images: ['gcr.io/$PROJECT_ID/$REPO_NAME:$COMMIT_SHA'] 35
  36. Automation with Wercker and Container Builder cloudbuild.yaml timeout: 40m steps:

    - name: gcr.io/cloud-builders/docker args: ['build', '--tag=gcr.io/$PROJECT_ID/$REPO_NAME:$COMMIT_SHA', '.'] images: ['gcr.io/$PROJECT_ID/$REPO_NAME:$COMMIT_SHA'] 36
  37. Automation with Wercker and Container Builder cloudbuild.yaml timeout: 40m steps:

    - name: gcr.io/cloud-builders/docker args: ['build', '--tag=gcr.io/$PROJECT_ID/$REPO_NAME:$COMMIT_SHA', '.'] images: ['gcr.io/$PROJECT_ID/$REPO_NAME:$COMMIT_SHA'] 37
  38. Automation with Wercker and Container Builder cloudbuild.yaml timeout: 40m steps:

    - name: gcr.io/cloud-builders/docker args: ['build', '--tag=gcr.io/$PROJECT_ID/$REPO_NAME:$COMMIT_SHA', '.'] images: ['gcr.io/$PROJECT_ID/$REPO_NAME:$COMMIT_SHA'] 38
  39. Automation with Wercker and Container Builder cloudbuild.yaml timeout: 40m steps:

    - name: gcr.io/cloud-builders/docker args: ['build', '--tag=gcr.io/$PROJECT_ID/$REPO_NAME:$COMMIT_SHA', '.'] images: ['gcr.io/$PROJECT_ID/$REPO_NAME:$COMMIT_SHA'] 39
  40. Automation with Wercker and Container Builder Change wercker.yml ▸ Google

    Container Registry ͔ΒҾͬுͬͯ͘ΔΑ͏ʹ͢Δ ▸ JSON Ͱೝূ͢ΔํࣜʹͳΔ ▸ username ͸ඞͣ _json_key ▸ ೝূ৘ใͷೖͬͨ JSON ͸؀ڥม਺ʹೖΕ͓ͯ͘ ▸ gcr.io ΛϨδετϦʹࢦఆ͢Δ 40
  41. Automation with Wercker and Container Builder Change wercker.yml build: box:

    id: account/repository username: $USERNAME password: $PASSWORD tag: tag box: id: gcr.io/<PROJECT_ID>/<IMAGE_NAME> username: _json_key passwrod: $GCR_JSON_KEY_FILE registry: https://gcr.io tag: <TAG> 41
  42. Automation with Wercker and Container Builder $GCR_JSON_KEY_FILE as Environment variable

    in Wercker CI { “type”: “service_account”, “project_id”: “PROJECT_ID”, “private_key_id”: “hogefugapiyofoobarbaz”, “private_key”: “——-BEGIN PRIVATE KEY——-\nhogehoge\n——-END PRIVATE KEY——-” “client_email”: “foobar@hogehoge.iam.gserviceaccount.com” …… } 42
  43. Automation with Wercker and Container Builder Wercker + Container Builder

    ▸ Ϗϧυ؀ڥΛ Docker Πϝʔδʹ٧ΊࠐΊΔ ▸ ϏϧυɾσϓϩΠ؀ڥʹԿ͕ඞཁ͔ίʔυʹམͱͤΔ ▸ ίʔυʹམͱͨ͠ Dockerfile ΛϦϏδϣϯ؅ཧͰ͖Δ ▸ ΠϝʔδΛ࢖ͬͯ CI/CD ͕Ͱ͖Δ ▸ ΠϝʔδΛϗετͯ͘͠ΕΔ৔ॴ͔Β pull ͢Δ͚ͩͰ࢖͑Δ 43
  44. Automation with Wercker and Container Builder Wercker + Container Builder

    ▸ CI ؀ڥΛ CI/CD ͢Δ ▸ खݩͷ PC ͷϦιʔεΛ࢖ΘͣʹΠϝʔδΛϏϧυͰ͖Δ ▸ Docker ΠϝʔδͷϦϏδϣϯ؅ཧΛࣗಈԽͰ͖Δ ▸ ؀ڥΛม͑Δ࣌͸ TAG Λม͑Δ͚ͩ 44
  45. Automation with Wercker and Container Builder Keishin Yokomaku / shibuya.apk

    #13