😸
Shopify themeのGithub連携した際のsubtreeでの運用 2 Github Action
はじめに
前回subtreeを使ったshopify themeの運用を検討したところ、コマンド操作が複雑そうだなとなりました。
なので今回Github Actionを利用して自動化したいと思います。
やりたいこと
やりたいことは前回の記事の最後で出てきた下記2つを自動化することです。
- mainブランチの変更をdeployブランチに切り出しShopifyに反映
 - Shopify管理画面からの変更時にdeployブランチをmainブランチに取り込む
 
結果
さっそくですがGithub Actionはこちらです。
前回手動でやっていたことをyamlで書きました。
mainブランチの変更をdeployブランチに切り出しShopifyに反映
下記をmainブランチの .github/workflows 内へ
deployFromMain.yaml
name: deployFromMain
on:
  push:
    branches:
      - "main"
jobs:
  deploy:
    runs-on: macos-latest
    permissions:
      contents: "write"
      id-token: "write"
    steps:
      - uses: actions/checkout@v3
        with:
          ref: deploy
          fetch-depth: 0
      - uses: actions/checkout@v3
        with:
          ref: main
          fetch-depth: 0
      - name: "subtree split"
        run: "git subtree split --prefix=deploy --rejoin --onto deploy -b deploy-${{ github.run_id }}"
      - run: "git checkout deploy-${{ github.run_id }}"
      - run: "git checkout deploy"
      - run: "git branch"
      - name: "merge"
        run: "git merge deploy-${{ github.run_id }}"
      - name: push
        run: |
          git remote set-url origin https://github-actions:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}
          git config --global user.name "${GITHUB_ACTOR}"
          git config --global user.email "${GITHUB_ACTOR}@users.noreply.github.com"
          git push
      - run: "git checkout main"
      - run: "git subtree merge --prefix=deploy --squash deploy"
      - name: push
        run: |
          git remote set-url origin https://github-actions:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}
          git config --global user.name "${GITHUB_ACTOR}"
          git config --global user.email "${GITHUB_ACTOR}@users.noreply.github.com"
          git push
Shopify管理画面からの変更時にdeployブランチをmainブランチに取り込む
下記をdeployブランチの .github/workflows 内へ
subtreeMergeFromDeploy.yaml
name: subtreeMergeFromDeploy
on:
  push:
    branches:
      - "deploy"
jobs:
  deploy:
    runs-on: macos-latest
    permissions:
      contents: "write"
      id-token: "write"
    steps:
      - uses: actions/checkout@v3
        with:
          ref: main
          fetch-depth: 0
      - run: "git fetch origin deploy"
      - run: "git branch"
      - uses: actions/checkout@v3
        with:
          ref: deploy
          fetch-depth: 0
      - run: "git branch"
      - run: "git checkout main"
      - run: "git branch"
      - run: "git subtree merge --prefix=deploy --squash deploy"
      - name: push
        run: |
          git remote set-url origin https://github-actions:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}
          git config --global user.name "${GITHUB_ACTOR}"
          git config --global user.email "${GITHUB_ACTOR}@users.noreply.github.com"
          git push
補足
deploy-${{ github.run_id }}"
run: "git subtree split --prefix=deploy --rejoin --onto deploy -b deploy-${{ github.run_id }}"
subtree split する際に新たにブランチを作らないといけないので、github.run_idを使用してユニークなブランチを作成しています。
fetch-depth: 0
checkout時にデフォルトでは1つ分のコミットしか取得しないようでした。
そうすると過去にsplitしたコミットが見つからずmerge出来ないようでした(多分)
ですので、fetch-depth: 0 を指定しすべてのコミットを取得するようにしています。
履歴が長くなってくると動作が遅くなるかもです。
git push
GitHub Action内でのgit pushのやり方はこちらを参考にさせていただきました
結局
ここまでやりましたがsubtree使わない方が運用がシンプルでいいんじゃないか?という気持ちは変わらないです。
Discussion