KINTO Tech Blog
PlatformEngineering

GitHub Actionsのワークフローの起動タイミングなどをまとめてみた

Jumpei Shimamura
Jumpei Shimamura
Cover Image for GitHub Actionsのワークフローの起動タイミングなどをまとめてみた

はじめに

こんにちは。プラットフォームGのPlatformEngneeringチームでPlatformEngineeringの考え方をベースにツール周りの開発・運用・展開の役割(とチームリーダーのようなことをしている)島村です。

この記事は、KINTOテクノロジーズのAdventCalendar2023の技術側1日目の記事です。

CICDツールとして、GitHubに付属しているGitHub Actions。
特にPushと手動(WorkFlow_dispatch)をよく使うと思いますが、これが有効になったり動いたりするのはどういう時なのか、たまに混乱しませんか?

私はします。メンバーとどうだっけ?と話をしたこともよくあります。

なので整理しようというのが本記事です。

背景

PushしたときとかMergeしたときとか手動とか、起動するEventsのうち、

  • on.push
  • on.workflow_dispatch

の際の挙動を整理してまとめたいと思います。

テスト準備

ケース

on.push

  • default(main)ブランチにMergeしなくても動くか
  • default(main)ブランチにMergeしたあと、Pushのブランチを変更するとどうか
  • Pushの際に他のブランチに影響がないか

on.workflow_dispatch

  • deafult(main)ブランチにMergeしなくても動くか
  • Merge後に別ブランチを作成して修正、Pushしたら反映されるか

基本コード(echo.yaml)

echo.yaml
name: "TestWorkFlow"

on:
  push:
    branches:
      - main
      - 'test/**'
  workflow_dispatch:

jobs:
  TestWorkflow:
    name: "TestWorkFLow Echo"
    runs-on: ubuntu-latest
    steps:
    - name: Echo Branch
      run: |
        echo "Branch:${{ GITHUB_REF_NAME }}"

結果まとめ

一枚っぺらのイラストにまとめたのがこちら。
result

詳細

on.push

  • Mergeしなくても、以下の条件にマッチするならPushしたタイミングで動く
    • 構文などのミスがないこと(ただ、構文ミスがあったとしてもFailで表示される)
    • pushしたブランチとworkflowのon.pushのブランチの条件が合致している
  • on.push.branchesをtest/**のままで"feature/trigger-test"でブランチを切ってpushしても動かない
    • on.push.branchesを"feature/xx"に変更してpushしたら、feature/trigger-testのWorkflowが動く
  • 以下の2つのブランチがある場合の挙動
    • on.push.branchesを"feature/xx"としているtest/trigger-test
    • on.push.branchesを"test/xx"としているfeature/another-trigger
      • 何か修正してtrigger-testでPushしても、feature/another-triggerのWorkFlowは動かない
      • 何か修正してfeature/another-triggerでPushしても、trigger-testのWorkFlowは動かない
  • on系パラメータはその発生したブランチにあるファイルの内容を見て起動するかを判断している

on.workflow_dispatch

  • defaultブランチにファイルが存在しないと、手動で動かすボタンは表示されない
  • Mergeした後に新しくブランチを作成してInputsを追加した
    • 手動起動の場合、ブランチを選択できるので、新しいブランチを選ぶとInputsが増える
  • pushと同じで、ブランチにあるファイルの内容を反映している

検証

on.push

default(main)ブランチにMergeしなくても動くか

まずは、echo.ymlを作成してPushする。
vscode

junpei@:test-trigger-repo$ git add .github/
junpei@:test-trigger-repo$ git commit -m "create workflow"
[test/trigger-test 43be511] create workflow
 1 file changed, 17 insertions(+)
 create mode 100644 .github/workflows/echo.yml
junpei@:test-trigger-repo$ git push origin test/trigger-test
・・・・・・
Total 5 (delta 0), reused 0 (delta 0), pack-reused 0
remote:
remote: Create a pull request for 'test/trigger-test' on GitHub by visiting:
remote:      https://github.com/junpeishimamura-kinto/test-trigger-repo/pull/new/test/trigger-test
remote:
To https://github.com/junpeishimamura-kinto/test-trigger-repo.git
 * [new branch]      test/trigger-test -> test/trigger-test
junpei@:test-trigger-repo$

Mainブランチ

想定だと動かないかなと思っていたが、Actionが走る。

Actionsが走る

Mergeした後にPushのブランチを変更するとどうか

トリガー条件を「test/**」としたmainブランチから「feature/trigger-test」を作成しても動かない=当然。

ワークフローのファイルを

echo.yaml
on:
  push:
    branches:
      - main
      - 'feature/**'
  workflow_dispatch:

と「feature/trigger-test」で修正してPushしたら、もちろん動く。

他のブランチへの影響はないか

mainブランチから、「test/trigger-test-other-branch」を作成して、適当なファイルを作成する

vscode

junpei@:test-trigger-repo$ git status
On branch test/trigger-test-other-branch
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        test-branch.txt

nothing added to commit but untracked files present (use "git add" to track)
junpei@:test-trigger-repo$ git add test-branch.txt
junpei@:test-trigger-repo$ git commit -m "create test-branch.txt"
git p[test/trigger-test-other-branch 0c738b1] create test-branch.txt
 1 file changed, 1 insertion(+)
 create mode 100644 test-branch.txt
junpei@:test-trigger-repo$ git push origin test/trigger-test-other-branch
・・・・
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote:
remote: Create a pull request for 'test/trigger-test-other-branch' on GitHub by visiting:
remote:      https://github.com/junpeishimamura-kinto/test-trigger-repo/pull/new/test/trigger-test-other-branch
remote:
To https://github.com/junpeishimamura-kinto/test-trigger-repo.git
 * [new branch]      test/trigger-test-other-branch -> test/trigger-test-other-branch
junpei@:test-trigger-repo$

でPushしたら、もちろん動く。この場合のワークフローファイルは、「test/trigger-test-other-branch」のものでMainではない。
Actionsが走る

追加で、mainブランチから「feature/another-trigger」を切る。この時、前に検証していた「feature/trigger-test」ブランチは残している。

junpei@:test-trigger-repo$ git switch -c feature/another-trigger
Switched to a new branch 'feature/another-trigger'
junpei@:test-trigger-repo$ git branch
* feature/another-trigger
  feature/trigger-test
  main
  test/trigger-test
  test/trigger-test-other-branch
junpei@:test-trigger-repo$

で、ファイルとかを作って、「feature/another-trigger」のワークフローの条件は変更せず(test/**)でPushする。

Actionsは走らない

Actionsは走らない。操作ブランチにあるワークフローの条件に合致するかどうか?が基準となっている模様。

on.workflow_dispatch

deafult(main)ブランチにMergeしなくても動くか

on.pushで作成したecho.ymlをMainブランチにマージしなくても手動できどうできるか?

ボタンがない

workflow_dispatchがYamlにあったとしても、Mainブランチにマージしないとボタンが表示されず動かせない。
ボタンがないので、指定するブランチを切り替えるとういうこともできなさそう。

Merge後に別ブランチを作成して修正、Pushしたら反映されるか

よく使うので、検証しませんでした。が、反映されます。
RunWorkflowのボタンを押すと、ブランチ指定ができるので、選ぶと、変更点などが反映されたものが動かせます。

所感

on.pushにおける、「defaultブランチにMergeしなくても動くか」以外は想定通りでした。PushしただけではGitHub Actionsとして登録されない=Mergeして初めて登録されると認識していたので、整理して認識が違っていたのを把握できて良かったです。今回はBranch条件としましたが、Tagもイベント駆動の種別が同じなので、同様の挙動をすると思います。

本当はCircle.CIとか他のCICDサービスとの比較もしたいと思っていたのですが、多分、同じ形で動くのかなと思っています。GitHub Actionsと違い、SaaS系CICDツールはイベント駆動でソースコードを引っ張ってくるので、イベントが発生したブランチにあるものを判断するんじゃないかな?と。

さいごに

PlatformEngneeringチームは、社内向けの横断ツールを統制して必要なものを開発しています。
Platformグループの他チームが作ったものを受け入れたり、必要なものを新規作成や既存のものをマイグレーションしたりしています。CDKにも手を出そうとしてるので、ManagedService以外にもプログラミングも行い始めました。
こういった活動に少しでも興味を持ったり話を聞いてみたいと思った方は、お気軽にご連絡いただければと思います。

Facebook

関連記事 | Related Posts

We are hiring!

【プラットフォームエンジニア】プラットフォームG/東京・大阪

プラットフォームグループについてAWS を中心とするインフラ上で稼働するアプリケーション運用改善のサポートを担当しています。

【スクラッチ開発エンジニア】プラットフォームG/東京・大阪

プラットフォームグループについてAWS を中心とするインフラ上で稼働するアプリケーション運用改善のサポートを担当しています。