https://docs.github.com/en/actions/using-workflows/storing-workflow-data-as-artifacts 最近ちょこちょこgithub actionsを触ることがあるのだが、artifactsについてよくわかっていないので 公式を参考にしながらまとめていこうと思います
Workflow Aritifacts
Artifacts allow you to persist data after a job has completed, and share that data with another job in the same workflow. An artifact is a file or collection of files produced during a workflow run
Artifactsとはfileやfileのコレクションのことをartifactという artifactsはjobの終了後にデータを保持し、同じworkflow内の他のjobとデータを共有することが可能。 例えばbuildとtest終了後のデータをartifactとして保存
日本語の意味的にはartifactは生成物、人工物という意味らしい辞典
生成したファイルやファイル群をまとめた箱(物)という解釈がしっくりくる

artifacts storage
Storing artifacts uses storage space on GitHub.
保存する際のストレージはgithubのストレージサービスを使います。
- pulicリポジトリとセルフホスティングランナーの場合は無料
- privateの場合は制限がある. Managing billing for GitHub Actions 請求はアップロードされたartifactの大きさで決まる
artifacts action
githubはartifactのためのactionを用意しています
upload-artifact
https://github.com/actions/upload-artifact
This uploads artifacts from your workflow allowing you to share data between jobs and store data once a workflow is complete.
ワークフローからartifactがアップロードされて、ワークフロー終了後にジョブ間でデータが共有、保存ができます。 これを使って保存、共有したいファイル、ディレクトリを指定しgithub storageにアップロードすることができます。
サンプルの内容は
- リポジトリをチェックアウト
- artifactディレクトリ内にworld.txtを作成
- そのartifactの名前とパスを指定。
これでfileがgithub storageにアップロードされます
:::: details 特定のファイルをアップロードするサンプル
steps:
- uses: actions/checkout@v2
- run: mkdir -p path/to/artifact
- run: echo hello > path/to/artifact/world.txt
- uses: actions/upload-artifact@v3
with:
name: my-artifact
path: path/to/artifact/world.txt
::::
::::details 試してみる コードは上のサンプルを使用します。

pushしactionが起動しているか見てみます。無事実行されました。履歴

保存したartifactを確認してみましょう。左上のsummaryボタンを押すことで確認できます

また、ダウンロードが可能です。「hello」が書き込まれているはずなので、確認してみます。

無事書き込まれていました

::::
download-artifact
https://github.com/actions/download-artifact 特定のディレクトリだけダウンロードしたり、特定のファイルだけダウンロードしたりできるaction
早速試してみる 先ほど試したupload artifactに追加し、ダウンロードしてみます
:::: details 試してみる
下のupload.ymlに追加download-artifact@3を追加します

追加後のファイルはこんな感じ
name: artifacts upload workflow
on:
push:
branches: [ "master" ]
jobs:
upload:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: mkdir -p path/to/artifact
- run: echo hello > path/to/artifact/world.txt
- uses: actions/upload-artifact@v3
with:
name: my-artifact
path: path/to/artifact/world.txt
// ここから追加内容
- uses: actions/download-artifact@v3
with:
name: my-artifact
- name: Display structure of downloaded files
run: ls -R
これでpushすると、downloadされているのがわかりますgithub

:::: これだけだとイマイチなので他のjobで使えるように設定したいと思います。 そこで使うのがdownload-path。version3から導入されました
The download-path step output contains information regarding where the artifact was downloaded to. This output can be used for a variety of purposes such as logging or as input to other actions. Be aware of the extra directory that is created if downloading all artifacts (no name specified).
このdownload-pathの出力にはartifactのダウンロード先に関する情報が含まれていて、
この出力をactionの入力に使用します。
今から試すのは作成したartifactをdownload-pathで呼び出し、中身を表示する。
やることはdownload-artifactにidを設定し、${{steps.(id名).outputs.download-path}}で指定する
:::: details 試してみる 使うコードはこんな感じ download-artifactで設定したidを”Echo download path” stepで使用しています。
name: artifacts upload workflow
on:
push:
branches: [ "master" ]
jobs:
upload:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: mkdir -p path/to/artifact
- run: echo hello > path/to/artifact/world.txt
- uses: actions/upload-artifact@v3
with:
name: my-artifact
path: path/to/artifact/world.txt
- uses: actions/download-artifact@v3
id: test
with:
name: 'my-artifact'
path: path/to/artifacts
- name: 'Echo download path'
run: echo ${{steps.test.outputs.download-path}}
実行結果はこんな感じ
pathがちゃんと表示されているのがわかります履歴

次に先ほど作成したmy-artifactに含まれているworld.txtを引っ張ってきて、中身のhelloが表示されるか確認しましょう。download-pathを指定した後にfile名を指定します
追加コード
name: artifacts upload workflow
on:
push:
branches: [ "master" ]
jobs:
upload:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: mkdir -p path/to/artifact
- run: echo hello > path/to/artifact/world.txt
- uses: actions/upload-artifact@v3
with:
name: my-artifact
path: path/to/artifact/world.txt
- uses: actions/download-artifact@v3
id: test
with:
name: 'my-artifact'
path: path/to/artifacts
- name: 'Echo download path'
run: echo ${{steps.test.outputs.download-path}}
// 追加分
- name: 'Cat world.txt'
run: cat ${{steps.test.outputs.download-path}}/world.txt
中身のhelloが表示されています履歴
保存したartifactを呼び出し、中身を表示することができました。
::::
キャッシュとartifactsについて
Artifacts and caching are similar because they provide the ability to store files on GitHub, but each feature offers different use cases and cannot be used interchangeably.
似たものを提供していますが、別々のユースケースで使うのが良いらしいです。 パッケージ管理システムからの依存関係の構築などジョブ間やワークフローの実行間で頻繁に変更されてないファイルを再利用する場合はキャッシュを使用するべきとのこと
artifactはビルドされたバイナリやビルドログなど、ワークフローの実行が終了した後に表示するためにジョブによって生成されたファイルを保存する場合は、アーティファクトを使用するべきとのこと
推奨の使い道であって他に使い道がないか試そうと思います。それはまた別記事で書きます
まとめ
今回自分はartifactを使って何かをしたいというよりもどんな時に使えそうかな、artifactってなんぞ?を少しでも解消したいと思い記事にしました。 これからもgithub actions周りで記事にしたいコンテンツを共有していきたいと思います