Search code examples
google-cloud-platformgitlabgoogle-cloud-runcicdgoogle-cloud-build

Getting error "Already have image (with digest): gcr.io/cloud-builders/docker" while trying Gitlab CICD


I am trying to use Gitlab CI/CD with Cloud Build and Cloud Run to deploy a Flask application.

I am getting an error

starting build "Edited"
FETCHSOURCE
Fetching storage object: gs://Edited
Copying gs://Edited
\ [1 files][  2.1 GiB/  2.1 GiB]   43.5 MiB/s                                   
Operation completed over 1 objects/2.1 GiB.
BUILD
Starting Step #0
Step #0: Already have image (with digest): gcr.io/cloud-builders/docker
Step #0: unable to prepare context: unable to evaluate symlinks in Dockerfile path: lstat /workspace/Dockerfile: no such file or directory
Finished Step #0
ERROR
ERROR: build step 0 "gcr.io/cloud-builders/docker" failed: step exited with non-zero status: 1
--------------------------------------------------------------------------------
Cleaning up project directory and file based variables 00:00
ERROR: Job failed: exit code 1

My .gitlab-ci.yml

image: aft/ubuntu-py-dvc
stages:
  - deploy

deploy:
  stage: deploy
  tags:
    - fts-cicd
  image: aft/ubuntu-py-gcloudsdk-dvc
  services:
    - docker:dind
  script:


    - echo $dvc > CI_PIPELINE_ID.json
    - echo $GCP_LOGIN > gcloud-service-key.json
    - dvc remote modify --local view-model-weights credentialpath CI_PIPELINE_ID.json
    - dvc pull
    - gcloud auth activate-service-account --key-file gcloud-service-key.json
    - gcloud config set project $PROJECT_ID
    - gcloud builds submit . --config=cloudbuild.yaml

cloudbuild.yaml

steps:
# Build the container image
- name: 'gcr.io/cloud-builders/docker'
  args: ['build', '-t', 'gcr.io/$PROJECT_ID/fts-im', '.']
# Push the container image to Container Registry
- name: 'gcr.io/cloud-builders/docker'
  args: ['push', 'gcr.io/$PROJECT_ID/fts-im']
# Deploy container image to Cloud Run
- name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
  entrypoint: gcloud
  args: ['run', 'deploy', 'fts_im', '--image', 'gcr.io/$PROJECT_ID/fts_im', "--platform", "managed",  "--region","asia-northeast1", "--port", "8000","--memory", "7G", "--cpu", "2", "--allow-unauthenticated"]
images:
- gcr.io/$PROJECT_ID/fts-im

Dockerfile

FROM python:3.9.16-slim
ENV  LC_ALL=C.UTF-8
ENV  LANG=C.UTF-8



ADD . /app
COPY .* app/
WORKDIR /app
ADD . .secrets
COPY CI_PIPELINE_ID.json .secrets/CI_PIPELINE_ID.json
RUN ls -la .
RUN ls -la data/
RUN pwd
RUN ls -la .secrets
RUN pip install -r requirements.txt
CMD ["gunicorn"  , "-b", "0.0.0.0:8000", "wsgi:app"]

Trying other solutions I tried to prune dockers from the VM which was used for Runner in the CICD settings, I have experimented from a test repo and it worked completely, I am getting this error while replicating it on a new repo. with changed the name to fts_im. I haven't deleted the previous build and deployed app from cloud build and cloud run, because while using the previous repo I experimented build multiple time all successful.


Solution

  • As per this document Dockerfile should present in the same directory where the build config file is,

    Run below command check if Dockerfile present in current directory or not

    docker build -t docker-whale
    

    If Dockerfile is present in the same directory where the build config file is, then review this documentation to ensure the correct working directory has been set in the build config file.

    Make sure GitLab CI/CD is set up correctly and configured to run on the current branch.

    Also you have to specify the full path of the Dockerfile in cloudbuild.yaml file

    The name of the file should be Dockerfile and not **.**Dockerfile. The file should not have any extension. check the Dockerfile is named correctly .

    Check you have not misspelled image name, I can see 2 different image names gcr.io/$PROJECT_ID/fts-im and gcr.io/$PROJECT_ID/fts_im, I’m not sure whether they are 2 different image or you misplaced _(underscore) with -(Hyphen).