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

GitHub Actions Deep Dive using PHP / PHPerKaigi...

GitHub Actions Deep Dive using PHP / PHPerKaigi 2022

Ken’ichiro Oyama

April 10, 2022
Tweet

More Decks by Ken’ichiro Oyama

Other Decks in Technology

Transcript

  1. • https://github.com/k1LoW/octocov • ίʔυϝτϦΫεπʔϧ…ίʔυΧόϨοδɺCode to Test Ratioɺςετ࣮ߦ࣌ؒ • ଟ਺ͷϓϩάϥϛϯάݴޠͱɺओཁͳΧόϨοδϨϙʔτϑΥʔϚοτʹରԠ •

    GitHub Actionsͷactionͱͯ͠ίʔυϝτϦΫεΛूܭɺP/RίϝϯτɺϨϙʔτ • CLIπʔϧͱͯ͠ίʔυΧόϨοδΛूܭɻදࣔ • ΧόϨοδόοδੜ੒ octocov 5 5 octocov
  2. octocov • octocov͸ίʔυϝτϦΫεΛࢦఆͷσʔλετΞʹอ࣋͢Δ͜ͱ͕Ͱ͖Δ • GitHub ArtifactsɺϩʔΧϧɺGitHubϦϙδτϦɺS3ɺGCSɺBigQuery • σʔλετΞͷ৘ใΛ࢖ͬͯϝτϦΫεͷࠩ෼ΛPRʹίϝϯτ • ผ్octocovΛ

    “CentralϞʔυ” ͱͯ͠ಈ͔͢͜ͱͰσʔλετΞʹ͋Δ֤Ϧϙδτ Ϧ͔Βૹ৴͞ΕͨίʔυϝτϦΫεΛऔಘ͠ɺҰཡදࣔ΍Ұׅͨ͠όοδੜ੒ͳͲ ͕Մೳ ίʔυϝτϦΫεσʔλͷૹ৴ɾऔಘɾ׆༻ 6
  3. GitHub Actions GitHub Actions makes it easy to automate all

    your software work fl ows, now with world-class CI/CD. Build, test, and deploy your code right from GitHub. Make code reviews, branch management, and issue triaging work the way you want. https://github.com/features/actions GitHub ActionsΛ࢖༻͢ΔͱɺϫʔϧυΫϥεͷCI/CD؀ڥͰ͢΂ͯͷι ϑτ΢ΣΞϫʔΫϑϩʔΛ؆୯ʹࣗಈԽͰ͖·͢ɻ GitHub͔Β௚઀ίʔ υΛϏϧυɺςετɺσϓϩΠͰ͖ɺίʔυϨϏϡʔɺϒϥϯν؅ཧɺ IssueͷτϦΞʔδΛ๬Μͩ௨Γʹػೳͤ͞·͢ɻ 8
  4. 9 Cons Pros • GitHubϓϥοτϑΥʔϜͱ౷߹͞ Ε͍ͯΔͨΊɺGitHubҎ֎Ͱ͸ ࢖͍ͮΒ͍ • GitHubϦϙδτϦͱ౷߹͞Ε͍ͯ ΔͨΊ͙͢ʹར༻ՄೳɻϦϙδτ

    ϦͷίʔυͷCI/CDʹ࠷ద • GitHub ActionsͷϓϥάΠϯͱ͍ ͑ΔAction౳΋GitHubϦϙδτϦ ʹ͋ΔίʔυΛར༻͢ΔͨΊ͙͢ ʹར༻͕ՄೳʢʮϓϥάΠϯͷΠ ϯετʔϧʯͱ͍͏Α͏ͳεςο ϓ͕ෆཁʣ GitHub ActionsͷPros/Cons GitHub Actions
  5. GitHub ActionsΛཧղ͢ΔͨΊͷྑ͍ϦϑΝϨϯε 10 GitHub Actions • ެࣜυΩϡϝϯτ • https://docs.github.com/ja/actions •

    ඇৗʹΘ͔Γ΍͍͢ͷͰΦεεϝ • ࣌ʑӳޠΑΓ΋৘ใྔ͕গͳ͍৔߹͕͋Δͷͱɺϖʔδ಺ϦϯΫ ͕ޮ͔ͳ͍ͷͰɺՄೳͳΒ https://docs.github.com/en/actions ΋֬ ೝ͍ͯͩ͘͠͞ʢϖʔδ্෦ͷϓϧμ΢ϯͰมߋՄೳͰ͢ʣ
  6. • ϫʔΫϑϩʔ ( Workflow ) … A workflow is a

    configurable automated process that will run one or more jobs. • δϣϒ ( Job ) … A job is a set of steps in a workflow that execute on the same runner. • εςοϓ ( Step ) … Jobͷߏ੒ཁૉɻ೚ҙͷॲཧΛ࣮ߦ͢Δ୯Ґɻ 12 GitHub ActionsͷΞʔΩςΫνϟ ίϯϙʔωϯτ : Workflow / Job / Step ※ https://docs.github.com/en/actions/learn-github-actions/understanding-github-actions ΑΓൈਮ
  7. • Πϕϯτ ( Event ) … An event is a

    specific activity in a repository that triggers a workflow run. • ΞΫγϣϯ ( Action ) … An action is a custom application for the GitHub Actions platform that performs a complex but frequently repeated task. • ϥϯφʔ ( Runner ) … A runner is a server that runs your workflows when they're triggered. Each runner can run a single job at a time. 13 GitHub ActionsͷΞʔΩςΫνϟ ίϯϙʔωϯτ : Event / Action / Runner ※ https://docs.github.com/en/actions/learn-github-actions/understanding-github-actions ΑΓൈਮ
  8. • GitHub Actions͸EventΛτϦΨʔʹWorkflowΛىಈ͢Δɻ • Workflow͸1ͭ΋͘͠͸ෳ਺ͷJobΛ࣮ߦ͢Δɻ • Job͸1ͭ΋͘͠͸ෳ਺ͷStepͰߏ੒͞Ε͍ͯΔɻ • Step͸೚ҙͷॲཧΛ࣮ߦ͢Δ୯Ґɻ •

    Jobຖʹ1ͭͷRunnerͷ্Ͱ࣮ߦ͞ΕΔɻ • WorkflowͷதͰΑ࣮͘ࢪ͞ΕΔॲཧ͸Actionͱ͍͏1ͭͷॲཧͷ·ͱ·Γͱͯ͠੾Γग़͠ ࠶ར༻Ͱ͖ΔΑ͏ͳ࢓૊Έ͕͋ΔɻAction͸StepͰݺͼग़͢͜ͱͰ࢖༻͢Δɻطʹଟ͘ͷ Action͕GitHubϚʔέοτϓϨΠεͰެ։͞Ε͓ͯΓɺ·ͨ৽͘͠࡞੒͢Δ͜ͱ΋Մೳɻ 14 GitHub ActionsͷΞʔΩςΫνϟ GitHub ActionsͷΞʔΩςΫνϟ
  9. • 1YAML = 1Workflow • ԿͷEventΛτϦΨʔʹ͢Δ͔͸ on: ηΫγϣϯͱରԠ • WorkflowʹͿΒԼ͕ΔJob͸

    jobs: ηΫγϣϯͱରԠ • JobʹͿΒԼ͕ΔStep͸ jobs.<job_id>.steps: ηΫγϣϯͱରԠ • ԿͷRunnerͷ্ͰJobΛಈ͔͔͢͸ jobs.<job_id>.runs-on: ηΫγϣϯͱରԠ • Action͸ uses: Ͱݺͼग़ͤ·͢ɻjobs.<job_id>.steps[*].uses: ηΫγϣϯͱରԠ • jobs.<job_id>.uses: Ͱ͸ Reusable Workflow ΋ݺͼग़͠Մೳ 16 GitHub ActionsͷΞʔΩςΫνϟ ֤ίϯϙʔωϯτͱ .github/workflows ʹஔ͘YAMLϑΝΠϧͷؔ܎
  10. Re: GitHub Actions GitHub Actions makes it easy to automate

    all your software work fl ows, now with world-class CI/CD. Build, test, and deploy your code right from GitHub. Make code reviews, branch management, and issue triaging work the way you want. https://github.com/features/actions GitHub ActionsΛ࢖༻͢ΔͱɺϫʔϧυΫϥεͷCI/CD؀ڥͰ͢΂ͯͷι ϑτ΢ΣΞϫʔΫϑϩʔΛ؆୯ʹࣗಈԽͰ͖·͢ɻ GitHub͔Β௚઀ίʔ υΛϏϧυɺςετɺσϓϩΠͰ͖ɺίʔυϨϏϡʔɺϒϥϯν؅ཧɺ IssueͷτϦΞʔδΛر๬Ͳ͓Γʹػೳͤ͞·͢ɻ 18
  11. 21 ϫʔΫϑϩʔΤϯδϯͱͯ͠ͷGitHub Actions • on: • needs: • jobs.<job_id>.needs: •

    if: • jobs.<job_id>.if: • jobs.<job_id>.steps[*].if: • uses: • jobs.<job_id>.steps[*].uses: • jobs.<job_id>.uses: DAGΛ࣮ݱ͢ΔͨΊͷϫʔΫϑϩʔߏจ
  12. on: 22 ϫʔΫϑϩʔΤϯδϯͱͯ͠ͷGitHub Actions • DAGʹ͓͚Δ࢝఺ʢͱͳΔ௖఺ʣʹͳΔߏจ • ίʔυͷPush΍Pull RequestҎ֎ʹ΋Issue΍Projectɺ DiscussionͳͲGitHubϓϥοτϑΥʔϜͷϦϙδτϦʹؔ܎͢Δ

    ཁૉͷ͞·͟·ͳΠϕϯτΛड͚औΕΔ • ఆظ࣮ߦ͕Ͱ͖Δ on.schedule: ͱϫʔΫϑϩʔΛ௚઀࣮ߦͰ͖ Δ repository_dispatch: ΍ workflow_dispatch: ͸རศੑ͕ߴ͍ • GitHubϓϥοτϑΥʔϜؔ܎ͳ͘ϫʔΫϑϩʔΛ࣮ߦͰ͖Δ
  13. 23 ϫʔΫϑϩʔΤϯδϯͱͯ͠ͷGitHub Actions • jobs.<job_id>.needs: • ͦͷJobΛ࣮ߦ͢ΔͨΊʹඞཁͳJobΛྻڍͰ͖Δ • = JobΛ௚ྻʹܨ͛Δ͜ͱ͕Ͱ͖Δߏจ

    • ௨ৗ͸Runnerͷ਺͕ڐ͢ݶΓฒྻ࣮ߦ • = DAGʹ͓͚ΔϑΝϯΠϯɺϑΝϯΞ΢τΛ࣮ݱͰ͖Δߏจ needs:
  14. • Expressions • PHPer͸ςϯϓϨʔτΤϯδϯ಺ͰόΠϯυͨ͠஋ΛࢀরͰ͖ͨΓɺҰ෦ؔ਺ ͕࢖͑ͨΓ͢ΔΞϨΛΠϝʔδ͍ͯͩ͘͠͞ • Context΍؀ڥม਺ΛݺͿͨΊʹ࢖༻ • if: ηΫγϣϯͰ͸৚݅ࣜΛॻͨ͘Ίʹ࢖͏

    • Context • ࠓ࣮ߦ͍ͯ͠ΔWorkflowपลͷίϯςΩετ৘ใΛऔಘͰ͖Δ 26 ϫʔΫϑϩʔΤϯδϯͱͯ͠ͷGitHub Actions DAGͷ੍ޚΛ͢ΔͨΊͷExpressions, Context
  15. • if: ${{ github.event_name == ‘pull_request’ }} • Event͕pull_requestͩͬͨ৔߹ʹtrueɻҎ߱ͷλεΫΛ࣮ߦ •

    run: echo '${{ toJSON(github) }}’ • “github” ίϯςΩετΛJSONܗࣜͰඪ४ग़ྗʹग़ྗ • if: ${{ always() && contains(needs.*.result, ‘success') }} • ௚લͷJobͷগͳ͘ͱ΋1͕ͭ੒ޭ͍ͯ͠Δ৔߹ʹtrueɻҎ߱ͷJobΛ࣮ߦ 27 ϫʔΫϑϩʔΤϯδϯͱͯ͠ͷGitHub Actions Example: Expressions, Context
  16. • Workflow commands • ඪ४ग़ྗ΁ग़ྗ͢Δ࣌ʹҰఆͷϑΥʔϚοτͰग़ྗ͢Δͱ஋Λอ࣋͢Δ͜ͱ͕Մೳ • run: echo '::set-output name=SELECTED_COLOR::green’

    • SELECTED_COLORͱ͍͏໊લͰ green ͱ͍͏จࣈྻ͕อ࣋͞ΕΔ • ${{ steps.<step_id>.outputs.SELECTED_COLOR }} ͰऔಘՄೳ • Artifacts • δϣϒؒͰσʔλΛڞ༗͢Δ͜ͱ͕Մೳ • جຊతʹ actions/upload-artifactɺactions/download-artifactΛ࢖༻͢Δ 30 ϫʔΫϑϩʔΤϯδϯͱͯ͠ͷGitHub Actions DAGͷ֤௖఺͔Β࣍ͷ௖఺΁஋Λ౉ͨ͢Ίʹ࢖͑Δػೳ
  17. • ओʹ੒Ռ෺ΛӬଓԽ͢ΔͨΊʹػೳ͢ΔArtifactsͱɺҰ࣌తͳΩϟογϡΛWorkflowؒͰڞ༗ͯ͠WorkflowΛߴ଎Խ ͢ΔͨΊͷCachingͱ͍͏ػೳ͕͋Δ • ͦΕͧΕ actions/(upload/download)-artifactͱactions/cacheͱ͍͏ActionͰػೳ͕ఏڙ͞Ε͍ͯΔɻ • ࣮͸্هͷػೳΛ࣮ݱ͢ΔͨΊͷʮArtifactͷμ΢ϯϩʔυʯҎ֎ͷAPI͸υΩϡϝϯτʹܝࡌ͞Ε͍ͯͳ͍ • Artifacts΋Caching΋GitHub

    ActionsͷRunner্Ͱ࢖͏͜ͱͷΈΛ૝ఆ͍ͯ͠ΔͨΊͩͱࢥΘΕΔ • ࢓૊Έͱͯ͠͸Runner্Ͱ͚ͩఏڙ͞ΕΔ ACTIONS_RUNTIME_TOKEN Λ࢖ͬͯΞοϓϩʔυઌͷҰ࣌URLΛऔ ಘ͠ɺͦͷҰ࣌URLʹରͯ͠Ξοϓϩʔυ͢Δ • https://github.com/actions/toolkit/blob/d1abf7dc74ea38ed6c5552491cdf8b695b813722/packages/cache/ src/internal/cacheHttpClient.ts#L35-L38 • https://github.com/actions/toolkit/blob/d1abf7dc74ea38ed6c5552491cdf8b695b813722/packages/artifact/ src/internal/utils.ts#L215-L219 31 ϫʔΫϑϩʔΤϯδϯͱͯ͠ͷGitHub Actions ༨ஊ: ArtifactsͱCaching
  18. 32 ϫʔΫϑϩʔΤϯδϯͱͯ͠ͷGitHub Actions • ؀ڥม਺ GITHUB_* • GITHUB_EVENT_PATHʹ͋ΔJSONϑΝΠϧ಺ʹΠϕϯτΛ ϑοΫͨ͠ࡍͷϦΫΤετϖΠϩʔυ͕͋Δ •

    ্هΛ࢖͏͜ͱͰStepͷॲཧ্ͰWorkflowͷίϯςΩετʹ ͦͬͨಈ͖Λ͢Δ͜ͱ͕Ͱ͖Δ Step಺Ͱ͸؀ڥม਺͔ΒGitHub Actionsͷ৘ใΛऔಘՄೳ
  19. 34 ϫʔΫϑϩʔΤϯδϯͱͯ͠ͷGitHub Actions • https://github.com/k1LoW/github-script-php • actions/github-scriptͷPHP൛Action • Step಺Ͱ uses:

    Ͱݺͼग़͢͜ͱͰಛʹԿ΋༻ҙͤͣʹPHPεΫϦ ϓτΛॻ͘͜ͱ͕Ͱ͖Δʢsetup-php΋͍Βͳ͍ʣ • طʹೝূࡁΈͷ knpkabs/github-api ͷΫϥΠΞϯτΠϯελϯε ͕ $github ʹηοτࡁΈɻͳͲ actions/github-scriptʹ͍ۙಈ͖ Λ͢Δ • PHPerKaigi2022ͷͨΊʹ࡞Γ·ͨ͠ ೚ҙͷStepͰPHPεΫϦϓτΛ࣮ߦͰ͖ΔAction
  20. Action 38 An action is a custom application for the

    GitHub Actions platform that performs a complex but frequently repeated task. https://docs.github.com/en/actions/learn-github-actions/ understanding-github-actions Action͸ɺGitHub Actions ϓϥοτϑΥʔϜͷͨΊͷΧελϜ ΞϓϦέʔγϣϯͰɺෳࡶ͕ͩසൟʹ܁Γฦ͞ΕΔλεΫΛ࣮ ߦ͢Δ΋ͷͰ͢ɻ
  21. 39 Action • Runner͸VM͕ͩJobͷ࣮ߦΛऴ͑ΔͨͼʹॳظԽ͞ΕΔ • Provide a clean instance for

    every job execution. • Runner΋VMͷ؀ڥ΋OSSͱͯ͠ެ։͞Ε͍ͯΔ • https://github.com/actions/runner • https://github.com/actions/virtual-environments RunnerʢGitHub-hosted runnersʣ
  22. 40 Action • Jobͷ࣮ߦ؀ڥ͸େ͖͘3௨Γ͋Δ 1. શͯͷStepΛ௚઀Runner্Ͱ࣮ߦ͢Δ 2. શͯͷStepΛ1छྨͷDockerΠϝʔδΛ࢖ͬͨDocker on Runner্ͰVMͷҰ෦σΟϨΫτϦΛϚ΢ϯτͨ͠͏͑Ͱ࣮

    ߦ͢Δ 3. Ұ෦ͷStepΛRunner্ͰɺҰ෦·ͨ͸શͯͷStepΛ1छྨ Ҏ্ͷDockerΠϝʔδΛ࢖ͬͨDocker on Runner্ͰVM ͷҰ෦σΟϨΫτϦΛϚ΢ϯτͨ͠͏͑Ͱ࣮ߦ͢Δ Runner্ͷJobͷ࣮ߦ؀ڥ
  23. 44 Action • Actionͷ࣮૷ʹΑͬͯ͸3छྨʹ෼͔ΕΔɻ3छྨͱ΋VM্ʹAction ͷϦϙδτϦ͕Clone͞ΕΔͷ͸ಉ͡ 1. JavaScript action • جຊͷ࣮૷ํ๏ɻJavaScriptͰॲཧΛॻ͘ɻRunner্ʹ͋

    ΔNodeΛ࢖ͬͯͦͷJavaScriptΛ࣮ߦ͢Δ 2. Docker container action • ࢦఆ͞ΕͨDockerfileΛDocker on Runnerͱ࣮ͯ͠ߦ͢Δ 3. Composite action • action.ymlʹهࡌ͞Εͨ steps: ΛॱʹRunner্Ͱ࣮ߦ͢Δ Actionͷछྨ
  24. Action 45 ͲͷλΠϓͷActionͰ΋೚ҙͷDockerΠϝʔδΛ࢖༻Ͱ͖Δ • DockerΛ࢖༻͢ΔActionΛ࢖༻͢Δ͜ͱͰ3ͭ໨ͷJobͷ࣮ߦ؀ڥ ͷύλʔϯΛ࣮ݱ͍ͯ͠Δ • Runnerʹ͸dockerd͕͍Δͷ͔ͩΒͲ͏ͱͰ΋ͳΔɻ 1. JavaScript

    action • JavaScriptͰDockerίϚϯυΛ࣮ߦ͢Ε͹ྑ͍ 2. Docker container action • DockerΠϝʔδΛ؆୯ʹ࢖༻͢ΔActionͱߟ͑ΒΕΔ 3. Composite action • steps: ্ͰDockerίϚϯυΛ࣮ߦ͢Ε͹ྑ͍