1

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'
Amorim95
  • 51
  • 4

2 Answers2

3

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
guillaume blaquiere
  • 66,369
  • 2
  • 47
  • 76
  • thanks for your reply :) I'm not sure if this is what you were suggesting, but I edited my question to show what I tried to do. But the result was the same. It still shows the same error. I'll try the other suggestion and I'll give some feedback asap – Amorim95 Dec 17 '20 at 16:52
  • 1
    Normal! Remove the dot . at the end of the args like this `args: ['rm', '-rf']` – guillaume blaquiere Dec 17 '20 at 19:56
0

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'

Amorim95
  • 51
  • 4