GoogleContainerTools / kaniko

Build Container Images In Kubernetes
Apache License 2.0
14.32k stars 1.4k forks source link

Kaniko unable to download repo when a Pull Request is created #3011

Open Rollson opened 5 months ago

Rollson commented 5 months ago

I have workflow file to build and push image to Github Container Registry using Github action. The workflow successfully builds and publishes the container image on a push request, however when i open a PR i have this error after kaniko attempts to download the repo:

  Run cat <<EOF > /kaniko/.docker/config.json
  Run '/home/runner/k8s/index.js'
  Enumerating objects: 772, done
  Counting objects:   0% (1/147)
  Counting objects:   1% (2/147)
  .
  .
  .
  Counting objects: 100% (147/147), done.
  Compressing objects:   1% (1/54)
  .
  .
  .
  Compressing objects: 100% (54/54), done.
  Total 772 (delta 127), reused 108 (delta 93), pack-reused 625
  Error: error resolving source context: object not found

my workflow file looks like this:

name: Build with kaniko 
on:
  push:
    branches: [ "*" ]
    paths:
      - ".github/workflows/build-kaniko.yml"
  pull_request:
    branches: [ "main" ]
    paths:
      - ".github/workflows/build-kaniko.yml"

env:
  KANIKO_CACHE_ARGS: "--cache=true --cache-copy-layers=true --cache-ttl=24h"

jobs:
  build:
    runs-on: arc-runner-set 
    container:
      image: gcr.io/kaniko-project/executor:v1.20.0-debug
    permissions: 
      contents: read 
      packages: write 

    steps:
      - name: Build with Kaniko
        run: |
          cat <<EOF > /kaniko/.docker/config.json
          {
            "auths": {
              "ghcr.io": {
                "auth": "$(echo -n "$GIT_USERNAME:$GIT_PASSWORD" | base64 -w0)"
              }
            }
          }
          EOF

          /kaniko/executor --dockerfile="./app/Dockerfile" \
            --context="${{ github.repositoryUrl }}#${{ github.ref }}#${{ github.sha }}"  \
            --destination="$GH_REGISTRY/$GH_IMAGE_NAME:$(echo ${GITHUB_SHA} | head  -c 7)" \
            ${{ env.KANIKO_CACHE_ARGS }} \
            --push-retry 5 
        env: 
          GIT_USERNAME: ${{ github.actor }}
          GIT_PASSWORD: ${{ secrets.GITHUB_TOKEN }}
          GH_REGISTRY: "ghcr.io"
          GH_IMAGE_NAME: "${{ github.repository }}/nginx"

Any idea on how to fix this error when my workflow is triggered via PR? Thanks

JeromeJu commented 5 months ago

Thanks for submitting this @Rollson .

The error comes from resolveSourceContext. It might be helpful if you could help check the subpath of $context and its access?

Rollson commented 5 months ago

@JeromeJu Sorry i did not really understand what you asked me to do.

However this is my git folder structure:

.github
app
 Dockerfile
 index.html

and i specify the path to the dockerfile as --dockerfile="./app/Dockerfile"

JeromeJu commented 5 months ago

Could we check if --context="${{ github.repositoryUrl }}#${{ github.ref }}#${{ github.sha }}" is set correctly? If so, is this pointing to the correct context?

Rollson commented 5 months ago

@JeromeJu i only have this error when i create a PR to my main branch but a push to my feature-branch builds and publishes the image. see context below: pull event context: --context="git://github.com/my-org/my-repo.git#refs/pull/12/merge#3e...1b7" push event context: --context="git://github.com/my-org/my-repo.git#refs/heads/my-branch#57...511"

bahamoth-nc commented 3 months ago

It have been reproduced only github PR.

my actions workflow is below..

on:
  pull_request:
    branches: [ 'master' ]
...
--context="${{ github.repositoryUrl }}#${{ github.ref }}#${{ github.sha }}"
--verbosity=debug
...

my log is below...

DEBU[0000] Getting source context from git://github.com/my-org/my-repo.git#refs/pull/9/merge#f5f78774abc84b13fe5382238569aa3337e04ca0
DEBU[0000] Getting source from reference refs/pull/9/merge
Enumerating objects: 982, done.
Counting objects: 100% (217/217), done.
Compressing objects: 100% (104/104), done.
Total 982 (delta 117), reused 178 (delta 92), pack-reused 765
Error: error resolving source context: object not found

According to the logs, the checkout from git was successful, but an error occurred when extracting the subPath from the context because the build context location could not be found.

https://github.com/GoogleContainerTools/kaniko/blob/main/cmd/executor/cmd/root.go#L440

hae-andrew-young commented 1 month ago

Having the same issue here. Can confirm that it only happens on pull_request events. It works on push events.

Rollson commented 1 month ago

@JeromeJu, hello. please any update on this issue regarding a fix or solution? Thanks

BlackTurtle123 commented 2 weeks ago

I would also be interested