1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

GitpodでGitのEmail情報を自動でマスクする

Last updated at Posted at 2023-06-14

はじめに

NTTテクノクロス株式会社の渡邉洋平です。

Gitpodを使ってGitHubのプロジェクトを使うとき、以下の様にEmailアドレスを隠す操作をしている人、いますか。

git config --global user.email hogehoge@example.com

わたしはAWS CDKのコントリビュートなどでGitpodを数十回以上利用していますが、毎回変えていました……10か月ほど……手動で……

この無駄な操作を何とかしようと一念発起したら20分ほどで解決したので共有します。

Bad

https://gitpod.io/user/account にて設定できそうですが、少なくとも私の環境ではうまく行かず、GitHubに登録したメールアドレスが設定されたままでした。

Good

設定値を色々見ていると、https://gitpod.io/user/preferences にDotfilesを設定する欄があります。Gitpod的には、ここで設定するのが良さそうです。

Dotfiles?

Dotfiles自体は.bashrc、.vimrcのような、ドットから始まるコンフィグ類など管理するリポジトリを指しているようです。正直、私は詳しくないのですが、セットアップスクリプトなども指す文化という理解です。

※参考:

Gitpodでは環境設定用にこのDotfiles、つまりセットアップ用の資材を設定するオプションがあります。

リポジトリのURLを設定し、以下の条件に沿ったスクリプトを実行することもできます。

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

120秒の実行制限こそありますが、今回の目的は果たせそうですので、こちらを使っていきましょう。

Dotfileの準備

Gitpod公式で用意されたリポジトリをforkすれば基本形はできます。
https://github.com/gitpod-samples/demo-dotfiles-with-gitpod

このリポジトリのsetup.shを読むと、scripts/配下のシェルスクリプトが実行対象であることがわかります。

setup.sh
#!/bin/bash

TMPDIR=$(mktemp -d)

CURRENT=$PWD

cd $TMPDIR

for script in ~/.dotfiles/scripts/*; do
  bash "$script"
done

cd $CURRENT

rm -rf $TMPDIR

なのでこのように、GitHubの公開用Emailアドレスでマスクする処理を書いていきます。

/scripts/mask_email.sh
#!/bin/bash

GIT_AUTHOR_EMAIL=xxxxxxxx@users.noreply.github.com 

echo "$GIT_AUTHOR_EMAIL"

git config user.email $GIT_AUTHOR_EMAIL
git config --global user.email $GIT_AUTHOR_EMAIL

実行

Dotfiles設定の後にGitpodを起動すると、無事にマスク用のメールアドレスが確認できました。

qiita_dotfile1.jpg

debug

設定したDotfilesが上手く動かない場合、以下の情報などからデバッグを始められます。

# Gitpod環境にダウンロードされたDotfiles
ls -a /home/gitpod/.dotfiles
.  ..  .git  LICENSE  README.md  scripts  setup.sh

# Dotfilesの実行ログ
$ cat /home/gitpod/.dotfiles.log
# installation script candidate /home/gitpod/.dotfiles/install.sh is not available
# installation script candidate /home/gitpod/.dotfiles/install is not available
# installation script candidate /home/gitpod/.dotfiles/bootstrap.sh is not available
# installation script candidate /home/gitpod/.dotfiles/bootstrap is not available
# installation script candidate /home/gitpod/.dotfiles/script/bootstrap is not available
# executing installation script candidate /home/gitpod/.dotfiles/setup.sh
xxxxxxxx@users.noreply.github.com
fatal: not in a git directory

その他、公式の記述も必読です。
https://www.gitpod.io/docs/configure/user-settings/dotfiles

まとめ

という訳で、Gitpodのセットアップをしたい時はDotfilesが役に立つことがわかりました。

余談ですが、GitHub Codespacesでも同様の仕組みでセッティングできそうです。

本例以外にもgit-secretsなど、Gitpod生活に役立ちそうなものはDotfilesで導入しましょう。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?