GCP cloudbuild git push trigger

1.3k Views Asked by At

I have a trigger to run a build job everytime there's a push to a specific branch of my repository.

If I try to run the build job "manually" (without the trigger) with the command:

# Submit the build job
_cmd = f"gcloud builds submit --no-source --config {config['build']['cloudbuild']} --substitutions {substitutions}"
subprocess.run(_cmd, shell=True, check=True)

it works as expected and completes successfully without any issue. However, if I perform a git push to my repository to do it with the trigger, after the trigger starts the build job and detects the complete structure from my cloudbuild YAML file, it breaks execution on the first step with an error message:

The first step:

steps:
# Clone repo to Cloud Build environment
- name: 'gcr.io/cloud-builders/git'
  args: ['clone',
         '--branch',"$_BRANCH_NAME",
         '${_REPO_URL}', '.',
         '--depth', '1',
         '--verbose']
  id: 'Clone Repo'

The error message:

fatal: destination path '.' already exists and is not an empty directory.

Do you know what the problem might be ?

Thanks in advance!


EDIT:

Tried to clear the directory before the git clone, but still the same result:

steps:
# Clear Cloud Build environment
- name: 'gcr.io/cloud-builders/git'
  args: ['rm', '-rf', '.']
  id: 'Clear Cloud Build environment'
  
# Clone repo to Cloud Build environment
- name: 'gcr.io/cloud-builders/git'
  args: ['clone',
         '--branch',"$_BRANCH_NAME",
         '${_REPO_URL}', '.',
         '--depth', '1',
         '--verbose']
  waitFor: ['Clear Cloud Build environment']
  id: 'Clone Repo'
2

There are 2 best solutions below

2
On

When you run your job locally, you use the --no-source to not upload file and run the Cloud Build with an empty /workspace directory

When you launch the Cloud Build with trigger, your /workspace directory already contain the sources, and your clone target directory isn't empty.

You can:

  • Either clone in another directory and then move the content to the /workspace.
  • Or perform a rm -rf in your /workspace before running the clone command
0
On

Apparently the problem was solved by changing the destination directory of the git clone and updating the dir tag on the remaings steps:

steps:
# Clone repo to Cloud Build environment
- name: 'gcr.io/cloud-builders/git'
  args: ['clone',
         '--branch',"$_BRANCH_NAME",
         '${_REPO_URL}', 'new_workspace/',    # new directory
         '--depth', '1',
         '--verbose']
  id: 'Clone Repo'

# Create the image for the component 1
- name: 'gcr.io/cloud-builders/docker'
  args: ['build',
         '-t', 'gcr.io/$_PROJECT_ID/comp_1:$_IMG_TAG', '.']
  dir: 'new_workspace/implementation/pipeline/components/comp_1'  # update with new directory
  id: 'Build Component Image'