ちぐログ

メモ書きです

タグの作成をトリガーに自動ビルドを行ったら、コミットハッシュの埋め込みが意図しないものになった話

タグ追加時に自動的にビルドして、コミットハッシュを埋め込みたい

chiguniiita.hatenablog.jp

chiguniiita.hatenablog.jp

上記2つを組み合わせて

  • タグ作成時に自動ビルドを行い
  • 成果物にタグのコミットハッシュの埋め込み

ができるようにしてみました。

埋め込みたいコミットハッシュは赤枠の部分です。

f:id:chiguniiita:20170206211404p:plain

謎のコミットハッシュが埋め込まれる…

できたアセンブリのバージョンを見ると

f:id:chiguniiita:20170206211831p:plain

埋め込みたいコミットハッシュと違う値が入ってしまいました。

git logを見ても、該当のコミットハッシュが見当たらない…

原因追及

まずはビルドログを追ってみました。

2017-02-06T12:10:33.0016940Z From https://github.com/chiguniiita/Playground
2017-02-06T12:10:33.0016940Z  * [new branch]      master     -> origin/master
2017-02-06T12:10:33.0016940Z  * [new tag]         v0.0.1     -> v0.0.1
2017-02-06T12:10:33.0246951Z ##[command]git checkout --progress --force af73e2d9cae44f6a8ad9d7499111aeecced1eb25
2017-02-06T12:10:33.0636955Z Note: checking out 'af73e2d9cae44f6a8ad9d7499111aeecced1eb25'.
2017-02-06T12:10:33.0636955Z 
2017-02-06T12:10:33.0636955Z You are in 'detached HEAD' state. You can look around, make experimental
2017-02-06T12:10:33.0636955Z changes and commit them, and you can discard any commits you make in this
2017-02-06T12:10:33.0636955Z state without impacting any branches by performing another checkout.
2017-02-06T12:10:33.0636955Z 
2017-02-06T12:10:33.0636955Z If you want to create a new branch to retain commits you create, you may
2017-02-06T12:10:33.0636955Z do so (now or later) by using -b with the checkout command again. Example:
2017-02-06T12:10:33.0636955Z 
2017-02-06T12:10:33.0636955Z   git checkout -b <new-branch-name>
2017-02-06T12:10:33.0636955Z 
2017-02-06T12:10:33.0636955Z HEAD is now at 383e09f... Merge pull request #16 from chiguniiita/issue-15

謎のコミットハッシュを使って git checkoutをしているのを発見。
HEAD is now at ~ にあるコミットハッシュが入れたい値です。
謎のコミットハッシュはどこから来た?

答えはgit show-ref

いろいろ調べていくうちにgit show-refコマンドなるものがあったので実行してみたら答えがありました。

> git show-ref
git show-ref
383e09f5b429aed37d049d2337e5be977919427f refs/heads/master
383e09f5b429aed37d049d2337e5be977919427f refs/remotes/origin/HEAD
383e09f5b429aed37d049d2337e5be977919427f refs/remotes/origin/master
af73e2d9cae44f6a8ad9d7499111aeecced1eb25 refs/tags/v0.0.1

どうやらタグのコミット時に、新たにコミットハッシュを取得していて、これが使われていた模様。

とりあえず対応

対応方法としてベストな選択かは不明ですが、git rev-parse HEADで取得した、コミットハッシュをアセンブリに埋め込めば良さそうです。

更新したスクリプトは以下。

Visual Studio Online でビルドする際に、アセンブリに Git のコミットハッシュ …

結果

意図したコミットハッシュが埋め込まれました。

f:id:chiguniiita:20170206212246p:plain