You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用GitHub Actions部署Pages时deploy-pages@v4找不到工件求助

解决GitHub Actions中deploy-pages@v4找不到已上传工件的问题

问题描述

使用GitHub Actions部署项目到GitHub Pages时,deploy-pages@v4报错无法找到名为github-pages的工件,错误日志如下:

Run actions/deploy-pages@v4
Fetching artifact metadata for "github-pages" in this workflow run
Found 0 artifact(s)
Error: Fetching artifact metadata failed. Is githubstatus.com reporting issues with API requests, Pages, or Actions? Please re-run the deployment at a later time.
Error: Error: No artifacts named "github-pages" were found for this workflow run. Ensure artifacts are uploaded with actions/upload-artifact@v4 or later.
    at getArtifactMetadata (/home/runner/work/_actions/actions/deploy-pages/v4/src/internal/api-client.js:85:1)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at Deployment.create (/home/runner/work/_actions/actions/deploy-pages/v4/src/internal/deployment.js:66:1)
    at main (/home/runner/work/_actions/actions/deploy-pages/v4/src/index.js:30:1)
Error: Error: No artifacts named "github-pages" were found for this workflow run. Ensure artifacts are uploaded with actions/upload-artifact@v4 or later.

当前采用两个独立工作流:

  1. CI - Build & Test:负责构建项目并上传工件(日志显示github-pages工件已成功上传)
  2. Deploy to GitHub Pages:通过workflow_run触发,在CI完成后执行部署

问题原因

通过workflow_run触发的新工作流处于独立运行环境,无法直接访问原CI工作流上传的工件。而deploy-pages@v4默认只会查找当前工作流运行中上传的工件,因此找不到原CI工作流的产物。

解决方案

方案一:合并工作流(推荐)

将构建和部署步骤整合到同一个工作流中,避免跨工作流的工件访问问题。修改后的完整配置如下:

name: CI - Build & Test & Deploy

on:
  push:
    branches: ["master"]
  workflow_dispatch:

jobs:
  build:
    runs-on: windows-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Cache
        id: cache-web
        uses: actions/cache@v4
        with:
          path: |
            web/node_modules
          key: ${{ runner.os }}-build-${{ hashFiles('**/web/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-build-
            ${{ runner.os }}-

      - name: Setup Pages
        uses: actions/configure-pages@v3
   
      - name: wasm-pack
        uses: jetli/wasm-pack-action@v0.4.0
        with:
          version: 'latest'
        
      - run: wasm-pack build --target web --out-dir ../web/snake-game
        working-directory: ./game
      - run: npm install
        working-directory: ./web
      - run: npm run build
        working-directory: ./web

      - uses: dtolnay/rust-toolchain@1.84.0
      - uses: Swatinem/rust-cache@v2
        with:
          cache-targets: false
      - name: Tests
        continue-on-error: true
        run: cargo test --verbose
        working-directory: ./game

      - name: Install cargo-llvm-cov
        uses: taiki-e/install-action@cargo-llvm-cov
        
      - name: code coverage
        run: cargo llvm-cov --all-features --workspace --lcov --output-path lcov.info
        working-directory: ./game

      - name: push to codecov
        uses: codecov/codecov-action@v5
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
          files: ./game/lcov.info

      - name: Upload artifact
        uses: actions/upload-pages-artifact@v3
        with:
          path: "./web/dist"

  deploy:
    needs: build
    runs-on: ubuntu-latest
    permissions:
      pages: write
      id-token: write
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    steps:
      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v4

方案二:跨工作流下载工件

若需保留两个独立工作流,需在Deploy工作流中先下载原CI工作流的工件,再执行部署。修改后的Deploy工作流配置如下:

name: Deploy to GitHub Pages

on:
  workflow_run:
    workflows: ["CI - Build & Test"]
    types:
      - completed

jobs:
  deploy:
    runs-on: ubuntu-latest
    permissions:
      pages: write
      id-token: write
      actions: read  # 添加读取工件的权限
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}

    steps:
      - name: Download CI workflow artifact
        uses: dawidd6/action-download-artifact@v2
        with:
          workflow: "CI - Build & Test"
          workflow_conclusion: success
          name: github-pages
          path: ./web/dist

      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v4

内容的提问来源于stack exchange,提问作者Józef Podlecki

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 06:04:54