GitHub上Docker容器构建失败:找不到PR合并引用
GitHub Actions合并PR后Docker构建失败排查
报错详情
#0 building with "builder-d99a4367-77ea-4ed0-93ed-c4feadc1693d" instance using docker-container driver #1 [internal] load git source https://github.com/***/repo.git#refs/pull/9/merge #1 0.121 Initialized empty Git repository in /var/lib/buildkit/runc-overlayfs/snapshots/snapshots/1/fs/ #1 ERROR: repository does not contain ref refs/pull/9/merge, output: "" ------ > [internal] load git source https://github.com/***/repo.git#refs/pull/9/merge: 0.121 Initialized empty Git repository in /var/lib/buildkit/runc-overlayfs/snapshots/snapshots/1/fs/ ------ ERROR: failed to solve: failed to read dockerfile: failed to load cache key: repository does not contain ref refs/pull/9/merge, output: "" Error: buildx failed with: ERROR: failed to solve: failed to read dockerfile: failed to load cache key: repository does not contain ref refs/pull/9/merge, output: ""
现有工作流配置
# This workflow will install Python dependencies, run tests and lint with a single version of Python # For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python name: Python application on: push: branches: [ "main" ] pull_request: branches: [ "main" ] permissions: contents: read jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Set up Python 3.11 uses: actions/setup-python@v4 with: python-version: "3.11" - name: Install dependencies run: | python -m pip install --upgrade pip pip install flake8 pytest if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - name: Lint with flake8 run: | # stop the build if there are Python syntax errors or undefined names flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics #- name: Test with pytest # run: | # pytest docker: runs-on: ubuntu-latest steps: - name: Set up QEMU uses: docker/setup-qemu-action@v3 run: echo "::debug::QEMU set up completed" - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 run: echo "::debug::Docker Buildx set up completed" - name: Login to Docker Hub uses: docker/login-action@v3 with: username: ${{ secrets.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_TOKEN }} run: echo "::debug::Logged in to Docker Hub" - name: Log environment variables run: | echo "::debug::GITHUB_REF: $GITHUB_REF" echo "::debug::GITHUB_SHA: $GITHUB_SHA" echo "::debug::GITHUB_REPOSITORY: $GITHUB_REPOSITORY" - name: Build and push uses: docker/build-push-action@v5 with: push: true tags: xxxxxxx/yyyyyy:latest run: echo "::debug::Started building and pushing Docker image"
问题原因
- Docker构建Job未检出代码:
dockerjob里缺少actions/checkout@v4步骤,Buildx无法获取本地代码上下文,只能尝试从Git仓库拉取指定ref。但PR合并后,GitHub会自动删除refs/pull/9/merge这类临时PR ref,导致拉取失败。 - 构建上下文未明确指定:默认情况下
docker/build-push-action会尝试基于Git上下文构建,在push到main分支的场景下,这个逻辑会错误引用已被删除的PR ref。
修复方案
1. 添加代码检出步骤
在docker job的所有步骤最开头添加代码检出操作:
- uses: actions/checkout@v4
2. 明确指定构建上下文(推荐)
修改Build and push步骤,指定使用当前目录作为构建上下文,避免歧义:
- name: Build and push uses: docker/build-push-action@v5 with: context: . push: true tags: xxxxxxx/yyyyyy:latest
3. 可选:限制Docker构建仅在push事件触发
如果不需要在PR打开时运行Docker构建,可以给docker job添加执行条件,只在push到main分支时触发:
docker: if: github.event_name == 'push' runs-on: ubuntu-latest steps: # ... 原有步骤内容
内容的提问来源于stack exchange,提问作者Andrea
相关产品推荐
相关产品推荐

