JavaScriptを有効にしてください

[Hugo] 自動投稿したい3(Github Actionsで記事ファイルを更新する)

 ·  ☕ 3 分で読めます

今回やること

以下を定期的に行う設定を作成する。

  • 公開可能な記事データを抽出
  • 更新日が最も古いもののdateを更新

コミット部分は別で対応。

前提

yamlファイルに設定を書く

プロジェクトのルートディレクトリに.github/workflows/ディレクトリを作成。
この中の.yamlファイルに記載した内容が実行される。

クイックスタート
https://docs.github.com/ja/actions/quickstart

構文
https://docs.github.com/ja/actions/using-workflows/workflow-syntax-for-github-actions

イベントトリガー(スケジュール)
https://docs.github.com/ja/actions/using-workflows/events-that-trigger-workflows#schedule

一旦以下の内容で作成

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
name: 'publish-draft-false'
on:
  schedule:
    - cron: '0 15 * * 6'

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          submodules: true
          fetch-depth: 0

      - name: Setup Hugo
        uses: peaceiris/actions-hugo@v2
        with:
          hugo-version: '0.96.0'
          extended: true

      - name: Run publish command
        run: |
          hugo list future |\
          cut -d ',' -f 1 |\
          xargs -r ls -tr |\
          head -1 |\
          xargs -r sed -i "s/9999-12-31T00:00:00+09:00/$(TZ=Asia/Tokyo date -Iseconds)/"          

Run publish commandについては後で修正する。
usesで公開されているアクションを使用することができる。
https://docs.github.com/ja/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsuses
リポジトリを取ってきたり、必要なもののインストールは既存のものを使用すると簡単にできる。

actions/checkout@v3
https://github.com/actions/checkout
リポジトリ操作関連。READMEに使用例が記載されている。

peaceiris/actions-hugo@v2
https://github.com/peaceiris/actions-hugo
hugoセットアップのアクション。

動作確認

上記の内容だと処理されたかがわからないので、最終行に以下を追加して確認する。

git diff --name-only

変更されたファイルが表示された。

❯ act

セットアップ部分は省略

[publish-draft-false/deploy] ⭐ Run Main Run publish command
[publish-draft-false/deploy]   🐳  docker exec cmd=[bash --noprofile --norc -e -o pipefail /var/run/act/workflow/2] user= workdir=
| content/ja/posts/github-actions.md
[publish-draft-false/deploy]   ✅  Success - Main Run publish command
[publish-draft-false/deploy] 🏁  Job succeeded

実際にはact実行時と本番環境間でsedの挙動が違ってエラーになったが、一旦ローカルでは確認できた。

問題点

別のマシン上でコマンドが実行されるので、更新日時は当てにならないことに気づいた。
環境毎に出力して確認したのが以下。

ローカル

❯ hugo list future | cut -d ',' -f 1 | xargs  -r ls -trl
-rw-r--r--  1 piyo  staff  5957  1  3 04:57 content/ja/posts/publish_script.md
-rw-r--r--  1 piyo  staff   116  1  3 13:37 content/ja/posts/hoge.md
-rw-r--r--  1 piyo  staff   117  1  3 13:37 content/ja/posts/hoge3.md
-rw-r--r--  1 piyo  staff   117  1  3 13:38 content/ja/posts/hoge2.md

act

| -rw-r--r-- 1 root root 5957 Jan  2 19:57 content/ja/posts/publish_script.md
| -rw-r--r-- 1 root root  116 Jan  3 04:37 content/ja/posts/hoge.md
| -rw-r--r-- 1 root root  117 Jan  3 04:37 content/ja/posts/hoge3.md
| -rw-r--r-- 1 root root  117 Jan  3 04:38 content/ja/posts/hoge2.md

本番

-rw-r--r-- 1 runner docker 5957 Jan  3 04:53 content/ja/posts/publish_script.md
-rw-r--r-- 1 runner docker  117 Jan  3 04:53 content/ja/posts/hoge3.md
-rw-r--r-- 1 runner docker  117 Jan  3 04:53 content/ja/posts/hoge2.md
-rw-r--r-- 1 runner docker  116 Jan  3 04:53 content/ja/posts/hoge.md

actの使い方の問題かもしれないが、上記のような結果だった。
とりあえずgit logを使用してコミットでソートする方向に変更する。

修正

lsを使用していた部分をgit logを使用するように変更。
出力に合わせて、sortcutを追加。

      - name: Run publish command
        run: |
          hugo list future |\
          cut -d ',' -f 1 |\
          xargs -n 1 -I {} git log -1 --format='%cd {}' --date=iso {} |\
          sort |\
          cut -d ' ' -f 4 |\
          head -1 |\
          xargs -r sed -i "s/9999-12-31T00:00:00+09:00/$(TZ=Asia/Tokyo date -Iseconds)/"

sort時点の出力はこのようになっている。

2023-01-03 04:56:11 +0900 content/ja/posts/publish_script.md
2023-01-03 13:44:14 +0900 content/ja/posts/hoge.md
2023-01-03 13:44:27 +0900 content/ja/posts/hoge2.md
2023-01-03 13:44:38 +0900 content/ja/posts/hoge3.md

流れ

  • git logを使用して各ファイルの最新コミットを取得。
  • フォーマットを整えて、日付でソートできるようにする。
  • 最初のファイル(一番コミット日が古いもの)のみ更新する。

記事の順番が重要な場合にはコミットの順序で調整する必要があるが、これでやりたいことはできた。

git logのドキュメント
https://git-scm.com/docs/git-log

あとは変更をコミットするstepを追加することで完成しそう。


書いた人
keee
Webエンジニア

目次