Alternative Architecture DOJO

オルターブースのクラウドネイティブ特化型ブログです。

GitHub Codespacesの環境をdotfilesでカスタマイズする

こんにちは、MLBお兄さんこと松村です。
TDL*1 を終えたニューヨーク・ヤンキースは突然の絶不調。勝率ガタ落ちです。未だ地区首位であることだけが救いです。涙


今回は GitHub Codespaces に自分の dotfiles をインストールする手順を紹介します。

GitHub Codespaces とは

GitHub Codespaces とは、 GitHub のサイト上で Visual Studio Code の開発環境が利用できる機能です。
現在は GitHub Team や GitHub Enterprise Cloud 、ベータ版を使用している個人ユーザーもいます。

github.co.jp

qiita.com

GitHub Codespaces を起動すると Linux VM が実行され、プリインストールされた Visual Studio Code を利用することができます。
起動時に指定した GitHub リポジトリを編集することができます。

dotfiles とは

dotfiles とは .bashrc.gitconfig など、ドット . で始まるファイル群を管理するリポジトリのことです。
メインが Windows PC な私にとってはそこまで馴染みがあるわけではないのですが、WSL2 を使うということもあり、自分用の dotfiles リポジトリを作っています。

dotfiles.github.io

Codespaces に dotfiles を使用する

自分のアカウント設定から Codespaces の設定画面に移り、使用したい dotfiles リポジトリを指定するだけで設定完了です。

その他、Codespaces のパーソナライズについては下記ドキュメントに記載されています。

https://docs.github.com/en/codespaces/customizing-your-codespace/personalizing-github-codespaces-for-your-account

指定した dotfiles リポジトリは Codespaces 環境の作成時に自動的にクローンされ、リポジトリに含まれる下記のいずれかのファイルが実行されます。

  • install.sh
  • install
  • bootstrap.sh
  • bootstrap
  • script/bootstrap
  • setup.sh
  • setup
  • script/setup

正しく dotfiles リポジトリが展開されたことを確認するには、Codespaces の Visual Studio Code で「作成ログ (Creation Log)」を確認すると良いです。
VSCode のコマンドパレットからログを確認することができます。

dotfiles の展開に失敗した場合

例えばシェルファイルが実行形式のパーミッションでない場合は、このような作成ログとなります。(最後の方に載っています)

2022-08-23 13:18:20.097Z: $ # Clone & install dotfiles
2022-08-23 13:18:20.109Z: Cloning into '/workspaces/.codespaces/.persistedshare/dotfiles'...
2022-08-23 13:18:20.872Z: Error: /workspaces/.codespaces/.persistedshare/dotfiles/install.sh not executable
2022-08-23 13:18:20.890Z: 
2022-08-23 13:18:20.903Z: 
2022-08-23 13:18:20.915Z: Exit code 126
2022-08-23 13:18:20.927Z: Stop (1508 ms): Run in container: # Clone & install dotfiles

dotfiles の展開に成功した場合

正しく dotfiles リポジトリを展開できた場合は、シェルファイルの実行結果が作成ログに出力されます。
※ログに出力される内容はリポジトリ構成によって異なります。

2022-08-23 13:45:48.822Z: $ # Clone & install dotfiles
2022-08-23 13:45:48.841Z: Cloning into '/workspaces/.codespaces/.persistedshare/dotfiles'...
2022-08-23 13:45:50.041Z: Executing script /workspaces/.codespaces/.persistedshare/dotfiles/install.sh
2022-08-23 13:45:50.064Z: + cp -f ./wsl/.bash_profile /home/codespace/.bash_profile
2022-08-23 13:45:50.090Z: + cp -f ./wsl/.bashrc /home/codespace/.bashrc
2022-08-23 13:45:50.125Z: + cp -f ./wsl/.profile /home/codespace/.profile
2022-08-23 13:45:50.146Z: + cp -f ./wsl/.bash_aliases /home/codespace/.bash_aliases
2022-08-23 13:45:50.175Z: + cp -f ./wsl/.gitconfig /home/codespace/.gitconfig
2022-08-23 13:45:50.191Z: + cp -f ./wsl/.editorconfig /home/codespace/.editorconfig
2022-08-23 13:45:50.207Z: 
2022-08-23 13:45:50.224Z: 
2022-08-23 13:45:50.240Z: Stop (2525 ms): Run in container: # Clone & install dotfiles

そして環境が起動した段階で dotfiles が適用されているため、自分が普段使っている設定が反映されている状態となります。

azure-static-webapps-samples [main]$ ls
total 32
drwxrwxrwx+ 7 codespace root 4096 Aug 24 00:46 .
drwxr-xrwx+ 5 codespace root 4096 Aug 24 00:46 ..
drwxrwxrwx+ 4 codespace root 4096 Aug 24 00:46 api
drwxrwxrwx+ 2 codespace root 4096 Aug 24 00:46 app
drwxrwxrwx+ 8 codespace root 4096 Aug 24 00:47 .git
drwxrwxrwx+ 3 codespace root 4096 Aug 24 00:46 .github
-rw-rw-rw-  1 codespace root    0 Aug 24 00:46 .gitignore
-rw-rw-rw-  1 codespace root  436 Aug 24 00:46 readme.md
drwxrwxrwx+ 2 codespace root 4096 Aug 24 00:46 .vscode
azure-static-webapps-samples [main]$ 
azure-static-webapps-samples [main]$ git st
On branch main
Your branch is up to date with 'origin/main'.

nothing to commit, working tree clean

dotfiles リポジトリを作っている人にとっては便利な機能ではないでしょうか?
皆さんも使いやすい Codespaces 環境を作りましょう~

*1:トレードデッドライン