【Github】Github Actions ~ Artifact ~

■ はじめに

Github Actions の アーティファクト (Artifact) についてまとめる

目次

【1】Artifact
 1)Artifactに関する例
【2】Artifact に関するアクション
 1)actions/upload-artifact アクション
 2)actions/download-artifact アクション
【3】サンプル
 例1:Hello World
【4】トラブル
 1)artifactをダウンロード時にエラー「Artifact not found」

【1】Artifact

* Github Actions で生成された成果物

cf Artifact = 技能(art)によって作り出したもの、人工物、加工品

https://docs.github.com/ja/actions/using-workflows/storing-workflow-data-as-artifacts

1)Artifact に関する例

* ビルドした後の実行ファイル(jar/war and so on)
* Unit test 結果

etc...

【2】Artifact に関するアクション

1)actions/upload-artifact アクション

* Artifactとしてファイルをアップロードする

https://github.com/actions/upload-artifact


https://github.com/actions/upload-artifact?tab=readme-ov-file#upload-an-individual-file

steps:
- run: mkdir -p path/to/artifact
- run: echo hello > path/to/artifact/world.txt
- uses: actions/upload-artifact@v4
  with:
    name: my-artifact
    path: path/to/artifact/world.txt

Inputs
https://github.com/actions/upload-artifact?tab=readme-ov-file#inputs

Items Explanations
name Artifact 名
path ダウンロード先のパス
if-no-files-found もしファイルがなかった場合(warn(Default)/error/ignore)
retention-days 保存期間(1-90) cf. retention=保持
compression-level 圧縮レベル(0 to 9, Default: 6)
overwrite 上書きするか(true/false(Default))

2)actions/download-artifact アクション

* Artifactをダウンロードする

https://github.com/actions/download-artifact


https://github.com/actions/download-artifact?tab=readme-ov-file#download-single-artifact

steps:
- uses: actions/download-artifact@v4
  with:
    name: my-artifact
    path: your/destination/dir
- name: Display structure of downloaded files
  run: ls -R your/destination/d

Inputs
https://github.com/actions/download-artifact?tab=readme-ov-file#inputs

Items Explanations
name Artifact 名
path ダウンロード先のパス

【3】サンプル

例1:Hello World

name: Sample

on:
  push:
    branches: [ "develop" ]
  pull_request:
    branches: [ "develop" ]
  workflow_dispatch:
    inputs:
      chosen-hello-world:
        required: true
        type: choice
        options:
        - Hello
        - World

jobs:
  job1:
    runs-on: ubuntu-latest
    steps:
      - run: mkdir -p path/to/artifact
      - run: echo "hello World" > path/to/artifact/hello.txt
      - uses: actions/upload-artifact@v4
        with:
          name: hello-artifact
          path: path/to/artifact/hello.txt
          if-no-files-found: warn
          retention-days: 1
          overwrite: true
  job2:
    needs: job1 # ★重要★
    runs-on: ubuntu-latest
    steps: 
      - run: mkdir -p path/out/artifact
      - uses: actions/download-artifact@v4
        with:
          name: hello-artifact
          path: path/out/artifact
      - run: cat path/out/artifact/hello.txt

【4】トラブル

1)artifactをダウンロード時にエラー「Artifact not found」

* 

エラー内容

Run actions/download-artifact@v4
Downloading single artifact
Error: Unable to download artifact(s): Artifact not found for name: hello-artifact
 Please ensure that your artifact is not expired
 and the artifact was uploaded using a compatible version of toolkit/upload-artifact.
 For more information, visit the GitHub Artifacts FAQ: 
   https://github.com/actions/toolkit/blob/main/packages/artifact/docs/faq.md

原因

* 「needs」がなかったため

解決案

# 「needs: job1」を追加した

  job2:
    needs: job1 # ★追加★
    runs-on: ubuntu-latest

関連記事

Github ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2019/07/18/234652
Github Actions ~ 基礎知識編 ~
https://dk521123.hatenablog.com/entry/2021/11/04/142835
Github Actions ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2022/06/16/151443
Github Actions ~ 基本編 ~
https://dk521123.hatenablog.com/entry/2023/12/22/195715
Github Actions ~ ワークフロー制御 ~
https://dk521123.hatenablog.com/entry/2024/01/28/004128
Github Actions ~ あれこれ編 ~
https://dk521123.hatenablog.com/entry/2023/12/21/155224
Github Actions ~ エラー処理 ~
https://dk521123.hatenablog.com/entry/2024/01/01/232057
Github Actions ~ Self-hosted runners / 入門編 ~
https://dk521123.hatenablog.com/entry/2023/12/18/204119
Github Actions ~ Self-hosted runners / あれこれ編 ~
https://dk521123.hatenablog.com/entry/2024/02/07/002736