@actions/artifact

中文文档
6.3.1发布于 2026/10/06MITTypeScriptES Module构建来源已验证近 30 天下载 383.2万

Actions artifact lib

原始使用文档
文档目录10 节
  1. v2 - What's New
  2. Improvements
  3. Breaking changes
  4. Quick Start
  5. Examples
  6. Upload and Download
  7. Delete an Artifact
  8. Downloading from other workflow runs or repos
  9. Speeding up large uploads
  10. Additional Resources

@actions/artifact

Interact programmatically with Actions Artifacts.

This is the core library that powers the @actions/upload-artifact and @actions/download-artifact actions.

v2 - What's New

[!IMPORTANT] @actions/artifact v2+, upload-artifact@v4+, and download-artifact@v4+ are not currently supported on GHES yet. The previous version of this package can be found at this tag and on npm.

The release of @actions/artifact@v2 (including upload-artifact@v4 and download-artifact@v4) are major changes to the backend architecture of Artifacts. They have numerous performance and behavioral improvements.

Improvements

  1. All upload and download operations are much quicker, up to 80% faster download times and 96% faster upload times in worst case scenarios.
  2. Once uploaded, an Artifact ID is returned and Artifacts are immediately available in the UI and REST API. Previously, you would have to wait for the run to be completed before an ID was available or any APIs could be utilized.
  3. Artifacts can now be downloaded and deleted from the UI before the entire workflow run finishes.
  4. The contents of an Artifact are uploaded together into an immutable archive. They cannot be altered by subsequent jobs. Both of these factors help reduce the possibility of accidentally corrupting Artifact files. (Digest/integrity hash coming soon in the API!)
  5. This library (and actions/download-artifact) now support downloading Artifacts from other repositories and runs if a GITHUB_TOKEN with sufficient actions:read permissions are provided.

Breaking changes

  1. Firewall rules required for self-hosted runners.

    If you are using self-hosted runners behind a firewall, you must have flows open to Actions endpoints. If you cannot use wildcard rules for your firewall, see the GitHub meta endpoint for specific endpoints.

    e.g.

    curl https://api.github.com/meta | jq .domains.actions
    
  2. Uploading to the same named Artifact multiple times.

    Due to how Artifacts are created in this new version, it is no longer possible to upload to the same named Artifact multiple times. You must either split the uploads into multiple Artifacts with different names, or only upload once.

  3. Limit of Artifacts for an individual job.

    Each job in a workflow run now has a limit of 10 artifacts.

Quick Start

Install the package:

npm i @actions/artifact

Import the module:

// ES6 module
import {DefaultArtifactClient} from '@actions/artifact'

// CommonJS
const {DefaultArtifactClient} = require('@actions/artifact')

Then instantiate:

const artifact = new DefaultArtifactClient()

ℹ️ For a comprehensive list of classes, interfaces, functions and more, see the generated documentation.

Examples

Upload and Download

The most basic scenario is uploading one or more files to an Artifact, then downloading that Artifact. Downloads are based on the Artifact ID, which can be obtained in the response of uploadArtifact, getArtifact, listArtifacts or via the REST API.

const {id, size} = await artifact.uploadArtifact(
  // name of the artifact
  'my-artifact',
  // files to include (supports absolute and relative paths)
  ['/absolute/path/file1.txt', './relative/file2.txt'],
  {
    // optional: how long to retain the artifact
    // if unspecified, defaults to repository/org retention settings (the limit of this value)
    retentionDays: 10
  }
)

console.log(`Created artifact with id: ${id} (bytes: ${size}`)

const {downloadPath} = await artifact.downloadArtifact(id, {
  // optional: download destination path. otherwise defaults to $GITHUB_WORKSPACE
  path: '/tmp/dst/path',
})

console.log(`Downloaded artifact ${id} to: ${downloadPath}`)

Delete an Artifact

To delete an artifact, all you need is the name.

const {id} = await artifact.deleteArtifact(
  // name of the artifact
  'my-artifact'
)

console.log(`Deleted Artifact ID '${id}'`)

It also supports options to delete from other repos/runs given a github token with actions:write permissions on the target repository is supplied.

const findBy = {
  // must have actions:write permission on target repository
  token: process.env['GITHUB_TOKEN'],
  workflowRunId: 123,
  repositoryOwner: 'actions',
  repositoryName: 'toolkit'
}


const {id} = await artifact.deleteArtifact(
  // name of the artifact
  'my-artifact',
  // options to find by other repo/owner
  { findBy }
)

console.log(`Deleted Artifact ID '${id}' from ${findBy.repositoryOwner}/ ${findBy.repositoryName}`)

Downloading from other workflow runs or repos

It may be useful to download Artifacts from other workflow runs, or even other repositories. By default, the permissions are scoped so they can only download Artifacts within the current workflow run. To elevate permissions for this scenario, you must specify options.findBy to downloadArtifact.

const findBy = {
  // must have actions:read permission on target repository
  token: process.env['GITHUB_TOKEN'],
  workflowRunId: 123,
  repositoryOwner: 'actions',
  repositoryName: 'toolkit'
}

await artifact.downloadArtifact(1337, {
  findBy
})

// can also be used in other methods

await artifact.getArtifact('my-artifact', {
  findBy
})

await artifact.listArtifacts({
  findBy
})

Speeding up large uploads

If you have large files that need to be uploaded (or file types that don't compress well), you may benefit from changing the compression level of the Artifact archive. NOTE: This is a tradeoff between artifact upload time and stored data size.

await artifact.uploadArtifact('my-massive-artifact', ['big_file.bin'], {
  // The level of compression for Zlib to be applied to the artifact archive.
  // - 0: No compression
  // - 1: Best speed
  // - 6: Default compression (same as GNU Gzip)
  // - 9: Best compression
  compressionLevel: 0
})

Additional Resources

版本与发布记录

@actions/artifact 共发布 54 个版本,首次发布于 2020/02/21;过去 12 个月发布了 11 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
6.3.1latest2026/10/061 个月前
6.3.02026/10/061 个月前
1.1.3v1-longlived2026/03/167 个月前
6.2.12026/03/117 个月前
6.2.02026/02/268 个月前
6.1.02026/01/319 个月前

关于 @actions/artifact 的常见问题

@actions/artifact 是什么?

Actions artifact lib

如何安装 @actions/artifact?

在项目目录运行 npm install @actions/artifact,或使用 pnpm add / yarn add / bun add。

@actions/artifact 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@actions/artifact 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@actions/artifact 可以商用吗?

@actions/artifact 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@actions/artifact 的下载量有多少?

根据 npm 官方下载统计,@actions/artifact 在 2026/10/05 – 2026/11/03 这 30 天内被下载 3,832,498 次,日均约 127,750 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@actions/artifact 还在维护吗?

最新版本 6.3.1 发布于 2026/10/06(1 个月前);至今共发布 54 个版本,其中过去 12 个月发布了 11 个;首次发布于 2020/02/21。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。