Traffine I/O

日本語

2023-03-04

GitHub Actionsにおけるアーティファクトとジョブ出力

Introduction

This article will delve into the intricacies of utilizing artifacts and job outputs in GitHub Actions, providing practical examples to demonstrate their versatility and effectiveness.

Sharing Data with Artifacts

Artifacts are files that you can store and retrieve during the execution of a GitHub Actions workflow. By uploading and downloading artifacts, you can share data between actions running in the same job or even across different jobs within the same workflow.

Here's an example of using artifacts to share data between actions:

yaml
jobs:
  produce_artifact:
    runs-on: ubuntu-latest
    steps:
      - name: Create artifact
        run: |
          echo "This is an example artifact" > artifact.txt

      - name: Upload artifact
        uses: actions/upload-artifact@v2
        with:
          name: my-artifact
          path: artifact.txt

  consume_artifact:
    needs: produce_artifact
    runs-on: ubuntu-latest
    steps:
      - name: Download artifact
        uses: actions/download-artifact@v2
        with:
          name: my-artifact

      - name: Display artifact content
        run: cat my-artifact/artifact.txt

In this example, we create an artifact file called artifact.txt in the produce_artifact job. We then upload the artifact using the actions/upload-artifact action. In the consume_artifact job, we download the artifact using the actions/download-artifact action and display its content.

Utilizing Job Outputs

Job outputs are a powerful way to share data between actions running in different jobs. By defining outputs at the job level, you can make the data produced by one job available to another job, enabling more complex workflows and data dependencies.

Here's an example of using job outputs to share data between jobs:

yaml
jobs:
  producer:
    runs-on: ubuntu-latest
    steps:
      - name: Produce data
        run: |
          echo "Produced data: example-data" > data.txt
          echo "data=$(cat data.txt)" >> $GITHUB_OUTPUT
        id: producer_step

    outputs:
      produced_data: ${{ steps.producer_step.outputs.data }}

  consumer:
    needs: producer
    runs-on: ubuntu-latest
    steps:
      - name: Consume data
        run: |
          echo "Received data: ${{ needs.producer.outputs.produced_data }}"

In this example, we create a file called data.txt in the producer job and set its content as an output. We then define the job output produced_data, which is set to the value of the step output. In the consumer job, we access the job output using the needs context and display the received data.

References

https://docs.github.com/en/actions

Ryusei Kakujo

researchgatelinkedingithub

Focusing on data science for mobility

Bench Press 100kg!