About workflow artifacts
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. For example, you can use artifacts to save your build and test output after a workflow run has ended. 运行中调用的所有操作和工作流都具有对该运行项目的写入权限。
默认情况下,GitHub 存储 90 天内的生成日志和项目,此保持期可自定义。 有关详细信息,请参阅“Usage limits, billing, and administration”。 The retention period for a pull request restarts each time someone pushes a new commit to the pull request.
These are some of the common artifacts that you can upload:
- Log files and core dumps
- Test results, failures, and screenshots
- Binary or compressed files
- Stress test performance output and code coverage results
Storing artifacts uses storage space on GitHub. 公共存储库中标准 GitHub 托管的运行器和自托管运行器可免费使用 GitHub Actions。 请参阅“Choosing the runner for a job”。 对于专用仓库,每个 GitHub 帐户可获得一定配额的免费时间和存储以用于 GitHub 托管的运行器,具体取决于帐户的计划。 超出包含金额的任何使用量将计入你的帐户。 For more information, see 关于 GitHub Actions 的计费.
Artifacts are uploaded during a workflow run, and you can view an artifact's name and size in the UI. When an artifact is downloaded using the GitHub UI, all files that were individually uploaded as part of the artifact get zipped together into a single file. This means that billing is calculated based on the size of the uploaded artifact and not the size of the zip file.
GitHub provides two actions that you can use to upload and download build artifacts. For more information, see the upload-artifact and download-artifact actions.
To share data between jobs:
- Uploading files: Give the uploaded file a name and upload the data before the job ends.
- Downloading files: You can only download artifacts that were uploaded during the same workflow run. When you download a file, you can reference it by name.
The steps of a job share the same environment on the runner machine, but run in their own individual processes. To pass data between steps in a job, you can use inputs and outputs. For more information about inputs and outputs, see GitHub Actions 的元数据语法.
比较构件和依赖项缓存
构件与缓存类似,因为它们能够在 GitHub 上存储文件,但每项功能都提供不同的用例,不能互换使用。
- 当想要重复使用在作业或工作流运行之间不频繁更改的文件时(例如从程序包管理系统构建依赖项),请使用缓存。
- 如果要保存作业生成的文件,以在工作流运行结束后查看(例如生成的二进制文件或生成日志),请使用项目。
For more information on dependency caching, see Caching dependencies to speed up workflows.
Uploading build and test artifacts
You can create a continuous integration (CI) workflow to build and test your code. For more information about using GitHub Actions to perform CI, see 关于使用 GitHub Actions 进行持续集成.
The output of building and testing your code often produces files you can use to debug test failures and production code that you can deploy. You can configure a workflow to build and test the code pushed to your repository and report a success or failure status. You can upload the build and test output to use for deployments, debugging failed tests or crashes, and viewing test suite coverage.
You can use the upload-artifact
action to upload artifacts. When uploading an artifact, you can specify a single file or directory, or multiple files or directories. You can also exclude certain files or directories, and use wildcard patterns. We recommend that you provide a name for an artifact, but if no name is provided then artifact
will be used as the default name. For more information on syntax, see the actions/upload-artifact action.
Example
For example, your repository or a web application might contain SASS and TypeScript files that you must convert to CSS and JavaScript. Assuming your build configuration outputs the compiled files in the dist
directory, you would deploy the files in the dist
directory to your web application server if all tests completed successfully.
|-- hello-world (repository)
| └── dist
| └── tests
| └── src
| └── sass/app.scss
| └── app.ts
| └── output
| └── test
|
This example shows you how to create a workflow for a Node.js project that builds the code in the src
directory and runs the tests in the tests
directory. You can assume that running npm test
produces a code coverage report named code-coverage.html
stored in the output/test/
directory.
The workflow uploads the production artifacts in the dist
directory, but excludes any markdown files. It also uploads the code-coverage.html
report as another artifact.
name: Node CI on: [push] jobs: build_and_test: runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v4 - name: npm install, build, and test run: | npm install npm run build --if-present npm test - name: Archive production artifacts uses: actions/upload-artifact@v4 with: name: dist-without-markdown path: | dist !dist/**/*.md - name: Archive code coverage results uses: actions/upload-artifact@v4 with: name: code-coverage-report path: output/test/code-coverage.html
name: Node CI
on: [push]
jobs:
build_and_test:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: npm install, build, and test
run: |
npm install
npm run build --if-present
npm test
- name: Archive production artifacts
uses: actions/upload-artifact@v4
with:
name: dist-without-markdown
path: |
dist
!dist/**/*.md
- name: Archive code coverage results
uses: actions/upload-artifact@v4
with:
name: code-coverage-report
path: output/test/code-coverage.html
Generating artifact attestations for builds
项目证明使你能够为自己构建的软件创建不可伪造的来源和完整性保证。 反过来,使用软件的人员可以验证软件是在哪里以及如何构建的。
使用软件生成项目证明时,将创建加密签名的声明用于确立生成的来源,并包含以下信息:
- 指向与项目关联的工作流的链接。
- 项目的存储库、组织、环境、提交 SHA 以及触发事件。
- OIDC 令牌中用于确立来源的其他信息。 有关详细信息,请参阅“关于使用 OpenID Connect 进行安全强化”。
还可以生成包含关联的软件物料清单 (SBOM) 的项目证明。 将你的版本与它们中使用的开放源代码依赖项列表相关联可提供透明度,并使使用者能够遵守数据保护标准。
You can access attestations after a build run, underneath the list of the artifacts the build produced.
For more information, see Using artifact attestations to establish provenance for builds.
Configuring a custom artifact retention period
You can define a custom retention period for individual artifacts created by a workflow. When using a workflow to create a new artifact, you can use retention-days
with the upload-artifact
action. This example demonstrates how to set a custom retention period of 5 days for the artifact named my-artifact
:
- name: 'Upload Artifact' uses: actions/upload-artifact@v4 with: name: my-artifact path: my_file.txt retention-days: 5
- name: 'Upload Artifact'
uses: actions/upload-artifact@v4
with:
name: my-artifact
path: my_file.txt
retention-days: 5
The retention-days
value cannot exceed the retention limit set by the repository, organization, or enterprise.
Downloading or deleting artifacts
During a workflow run, you can use the download-artifact
action to download artifacts that were previously uploaded in the same workflow run.
After a workflow run has been completed, you can download or delete artifacts on GitHub or using the REST API. For more information, see Downloading workflow artifacts, Removing workflow artifacts, and GitHub Actions 构件的 REST API 终结点.
Downloading artifacts during a workflow run
The actions/download-artifact
action can be used to download previously uploaded artifacts during a workflow run.
注意
If you want to download artifacts from a different workflow or workflow run, you need to supply a token and run identifier. See Download Artifacts from other Workflow Runs or Repositories in the documentation for the download-artifact
action.
Specify an artifact's name to download an individual artifact. If you uploaded an artifact without specifying a name, the default name is artifact
.
- name: Download a single artifact
uses: actions/download-artifact@v4
with:
name: my-artifact
You can also download all artifacts in a workflow run by not specifying a name. This can be useful if you are working with lots of artifacts.
- name: Download all workflow run artifacts
uses: actions/download-artifact@v4
If you download all workflow run's artifacts, a directory for each artifact is created using its name.
For more information on syntax, see the actions/download-artifact action.
Validating artifacts
Every time the upload-artifact action is used it returns an output called digest
. This is a SHA256 digest of the Artifact you uploaded during a workflow run.
When the download-artifact action is then used to download that artifact, it automatically calculates the digest for that downloaded artifact and validates that it matches the output from the upload-artifact step.
If the digest does not match, the run will display a warning in the UI and in the job logs.
To view the SHA256 digest you can open the logs for the upload-artifact job or check in the Artifact output that appears in the workflow run UI.
Passing data between jobs in a workflow
You can use the upload-artifact
and download-artifact
actions to share data between jobs in a workflow. This example workflow illustrates how to pass data between jobs in the same workflow. For more information, see the actions/upload-artifact and download-artifact actions.
Jobs that are dependent on a previous job's artifacts must wait for the dependent job to complete successfully. This workflow uses the needs
keyword to ensure that job_1
, job_2
, and job_3
run sequentially. For example, job_2
requires job_1
using the needs: job_1
syntax.
Job 1 performs these steps:
- Performs a math calculation and saves the result to a text file called
math-homework.txt
. - Uses the
upload-artifact
action to upload themath-homework.txt
file with the artifact namehomework_pre
.
Job 2 uses the result in the previous job:
- Downloads the
homework_pre
artifact uploaded in the previous job. By default, thedownload-artifact
action downloads artifacts to the workspace directory that the step is executing in. You can use thepath
input parameter to specify a different download directory. - Reads the value in the
math-homework.txt
file, performs a math calculation, and saves the result tomath-homework.txt
again, overwriting its contents. - Uploads the
math-homework.txt
file. As artifacts are considered immutable inv4
, the artifact is passed a different input,homework_final
, as a name.
Job 3 displays the result uploaded in the previous job:
- Downloads the
homework_final
artifact from Job 2. - Prints the result of the math equation to the log.
The full math operation performed in this workflow example is (3 + 7) x 9 = 90
.
name: Share data between jobs on: [push] jobs: job_1: name: Add 3 and 7 runs-on: ubuntu-latest steps: - shell: bash run: | expr 3 + 7 > math-homework.txt - name: Upload math result for job 1 uses: actions/upload-artifact@v4 with: name: homework_pre path: math-homework.txt job_2: name: Multiply by 9 needs: job_1 runs-on: windows-latest steps: - name: Download math result for job 1 uses: actions/download-artifact@v4 with: name: homework_pre - shell: bash run: | value=`cat math-homework.txt` expr $value \* 9 > math-homework.txt - name: Upload math result for job 2 uses: actions/upload-artifact@v4 with: name: homework_final path: math-homework.txt job_3: name: Display results needs: job_2 runs-on: macOS-latest steps: - name: Download math result for job 2 uses: actions/download-artifact@v4 with: name: homework_final - name: Print the final result shell: bash run: | value=`cat math-homework.txt` echo The result is $value
name: Share data between jobs
on: [push]
jobs:
job_1:
name: Add 3 and 7
runs-on: ubuntu-latest
steps:
- shell: bash
run: |
expr 3 + 7 > math-homework.txt
- name: Upload math result for job 1
uses: actions/upload-artifact@v4
with:
name: homework_pre
path: math-homework.txt
job_2:
name: Multiply by 9
needs: job_1
runs-on: windows-latest
steps:
- name: Download math result for job 1
uses: actions/download-artifact@v4
with:
name: homework_pre
- shell: bash
run: |
value=`cat math-homework.txt`
expr $value \* 9 > math-homework.txt
- name: Upload math result for job 2
uses: actions/upload-artifact@v4
with:
name: homework_final
path: math-homework.txt
job_3:
name: Display results
needs: job_2
runs-on: macOS-latest
steps:
- name: Download math result for job 2
uses: actions/download-artifact@v4
with:
name: homework_final
- name: Print the final result
shell: bash
run: |
value=`cat math-homework.txt`
echo The result is $value
The workflow run will archive any artifacts that it generated. For more information on downloading archived artifacts, see Downloading workflow artifacts.
已删除的工作流运行中的工件
删除某个工作流运行时,也会从存储中删除与该运行关联的所有工件。 可以使用 GitHub Actions UI、REST API 或使用 GitHub CLI 删除工作流运行,请参阅“Deleting a workflow run”、删除工作流运行或 gh run delete。