GitHub Action中用curl下载Artifact报错:Could not resolve host: .
GitHub Action curl下载Artifact报错解决
问题场景
配置了如下GitHub Action流水线:
name: MyPipeline on: workflow_dispatch: permissions: # Required for release contents: write jobs: build: runs-on: windows-latest outputs: artifact-url: ${{ steps.artifact-upload-step.outputs.artifact-url }} steps: # Other steps ommitted - name: Upload Artifact id: artifact-upload-step uses: actions/upload-artifact@v4 with: name: MyArtifact path: ./output/ if-no-files-found: error publish: needs: build runs-on: ubuntu-latest steps: # Use API because download artifact action will unzip the artifact, but we want to keep it zipped - name: Download artifact using curl run: > curl -L --no-progress-meter --fail-with-body -H "Accept: application/vnd.github+json" -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" -H "X-GitHub-Api-Version: 2022-11-28" -O "./output/MyArtifact.zip" "${{ needs.build.outputs.artifact-url }}" shell: bash
执行时出现错误:Could not resolve host: .,同时伴随404 Not Found。即使确认artifact-url是正确的可访问地址,问题依然存在。改用GitHub API URL后,又出现403错误和相同的主机解析失败问题。
错误输出示例:
Run curl -L --no-progress-meter --fail-with-body -H "Accept: application/vnd.github+json" -H "Authorization: ***" -H "X-GitHub-Api-Version: 2022-11-28" -O "./output/MyArtifact.zip" "https://github.com/MyOrg/MyRepo/actions/runs/10293145930/artifacts/1788063174" curl -L --no-progress-meter --fail-with-body -H "Accept: application/vnd.github+json" -H "Authorization: ***" -H "X-GitHub-Api-Version: 2022-11-28" -O "./output/MyArtifact.zip" "https://github.com/MyOrg/MyRepo/actions/runs/10293145930/artifacts/1788063174" shell: /usr/bin/bash --noprofile --norc -e -o pipefail {0} env: VERSION: 1.0.0.12 DOTNET_ROOT: /usr/share/dotnet curl: (6) Could not resolve host: . curl: (22) The requested URL returned error: 404 Not Found
错误原因
- 命令换行参数拆分错误:使用
>进行多行命令书写时,bash对换行和空格的解析出现问题,导致-O "./output/MyArtifact.zip"被错误拆分,curl将路径中的.识别为独立的主机名参数,引发解析失败。 - Artifact URL类型错误:
upload-artifact@v4输出的artifact-url是网页跳转地址,并非直接的zip文件下载地址,直接curl该URL会返回404(因为返回的是HTML页面而非文件)。 - 权限不足:改用API URL时,未配置足够的权限,导致GITHUB_TOKEN无法访问artifact下载接口,触发403错误。
解决方案
1. 修正build作业输出,新增artifact-id
upload-artifact@v4会输出artifact-id,用它拼接API下载URL更可靠,修改build作业:
build: runs-on: windows-latest outputs: artifact-url: ${{ steps.artifact-upload-step.outputs.artifact-url }} artifact-id: ${{ steps.artifact-upload-step.outputs.artifact-id }} # 新增artifact-id输出 steps: # 其他步骤省略 - name: Upload Artifact id: artifact-upload-step uses: actions/upload-artifact@v4 with: name: MyArtifact path: ./output/ if-no-files-found: error
2. 修正publish作业的curl命令与权限配置
publish: needs: build runs-on: ubuntu-latest permissions: contents: write # 保留原有发布权限 actions: read # 新增读取artifact的权限,解决403问题 steps: - name: 创建输出目录 run: mkdir -p ./output # 确保输出目录存在,避免写入失败 shell: bash - name: Download artifact using curl run: | curl -L --no-progress-meter --fail-with-body \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ -H "X-GitHub-Api-Version: 2022-11-28" \ -o "./output/MyArtifact.zip" \ "${{ github.api_url }}/repos/${{ github.repository }}/actions/artifacts/${{ needs.build.outputs.artifact-id }}/zip" shell: bash
关键修改点说明
- 用
|定义多行命令,每行末尾加\确保参数正确拼接,避免bash错误拆分参数,解决主机解析失败问题。 - 将
-O改为-o,明确指定输出文件路径(-O会自动使用URL中的文件名,这里手动指定更可控)。 - 新增
actions: read权限,确保GITHUB_TOKEN有权限调用artifact下载API,解决403错误。 - 使用
artifact-id拼接官方API下载地址,该地址直接返回zip文件,避免网页跳转导致的404问题。
内容的提问来源于stack exchange,提问作者Red Riding Hood
相关产品推荐
相关产品推荐

