LoginSignup
69
36

More than 3 years have passed since last update.

GitHub Actionsでキャッシュが使えるようになって嬉しい!

Last updated at Posted at 2019-11-03

概要

  • GitHub Actionsでキャッシュが出来るようになった
  • まだβ版で正式公開ではなさそう
  • CircleCIなどと似たような感じでキャッシュ周りの設定を書ける

actions/cache - https://github.com/actions/cache

何が嬉しいのか

yarn install に1分以上かかってたがそれが省略出来る。
Litner,Test...とジョブごとに分けてたら当然ジョブ毎にモジュールのインストールが必要で「ウーーン」となってたのでとても嬉しいです。

サンプル

公式のリポジトリに様々な例がありますがここではJavaScript(yarn)の場合とElixir(mix)の場合を紹介します
やってることとしては何をキャッシュするかの記述と保存する時に使われるキーの設定です

JavaScript

jobs:
  # Linterに走ってもらうジョブ
  Lint:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v1
    - name: Cache node_modules
      uses: actions/cache@preview
      with:
        path: ~/.cache/yarn
        key: ${{ runner.os }}-projectname-${{ hashFiles(format('{0}{1}', github.workspace, '/yarn.lock')) }}
        restore-keys:
          ${{ runner.os }}-projectname-
    - name: Install node_modules
      if: steps.cache.outputs.cache-hit != 'true'
      run: yarn install
    - name: Lint
      run: yarn lint

Elixir

jobs:
  Test:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v1
    - uses: actions/setup-elixir@v1.0.0
      with:
        otp-version: 22.0
        elixir-version: 1.8.0
    - name: Cache deps
      uses: actions/cache@preview
      with:
        path: deps
        key: ${{ runner.os }}-projectname-${{ hashFiles(format('{0}{1}', github.workspace, '/mix.lock')) }}
        restore-keys:
          ${{ runner.os }}-projectname-
    - name: Install deps
      if: steps.cache.outputs.cache-hit != 'true'
      run: mix deps.get
    - name: Testing
      run: mix test

ハマりポイント

hashFilesに渡すディレクトリ

package-lock.jsonとそのまま渡すと失敗します
公式のサンプルは**/package-lock.jsonといった書き方が多いですが、それらよりyarnのサンプルのようにgithub.workspaceを使ってあげるのが良いと思っています

containerオプションを指定してるとコケる

弊社プロジェクトでは以下のようにnode.js 10.0を用いて各ジョブを走らせるようにしていました

container:
  image: node:10

しかしactions/cache@previewを設定してると「Stop Containers」の後に「Post Cache node modules」が走り、CIが落ちます

Post job cleanup.
/usr/bin/docker exec  ... sh -c "cat /etc/*release | grep ^ID"
Running JavaScript Action with default external tool: node12
Error: No such container: ...
##[error]Node run failed with exit code 1

まだβ版だし仕方ないのかな..?(解決策募集)

最後に

あとは設定ファイルを綺麗に書くための仕組みが出来上がれば最高of最高です

追記

Elixirのサンプルを書いたらマージして貰えた
https://github.com/actions/cache/pull/42

69
36
1

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
69
36