Tech

Firebaseのpostdeployを設定してgitのタグをdeploy時に自動で付ける

まえがき

個人サイトレベルなので何か合ったらちまちまdeployしてしまうものの、どのコミットでdeployしたかが適当にやってると分からなくなりそうで、かつ手動は面倒くさいので作りました。
postdeploy hook を使うと幸せという話が主題です。

手順

scripts/create-deploy-tag.js というのを作りました
deploy/20180425-124403 こういうフォーマットで付きます)

#!/usr/bin/env node

const { execSync } = require('child_process');

(() => {
  const pad2 = n => (n < 10 ? `0${n}` : n)
  const date = new Date()
  const times = []

  times.push(date.getFullYear().toString())
  times.push(pad2(date.getMonth(), 1))
  times.push(pad2(date.getDate()))
  times.push('-')
  times.push(pad2(date.getHours()))
  times.push(pad2(date.getMinutes()))
  times.push(pad2(date.getSeconds()))

  const tagName = `deploy/${times.join('')}`
  const cmd = `git tag ${tagName} && git push origin ${tagName}`
  execSync(cmd)

  process.stdout.write(`created tag ${tagName}`)
})()

chmod 755 scripts/create-deploy-tag.js をして実行権限を付けます

最後にfirebase.jsonに postdeploy 設定を追加します

firebase.json

  "hosting": {
    "postdeploy": [
      "scripts/create-deploy-tag.js"
    ],

以上でdeployすると勝手にtagが追加されるので楽になりました。ちゃんちゃん。

参考

デプロイフック等…
firebaseのドキュメントなんかしっくりこなくて大変な気が…。

share