Search code examples
githubgoogle-cloud-build

Google cloud build trigger on push to event of github


I am new to Cloud build and trying to understand how and what a cloud build trigger on Github does. I have written a simple cloudbuild.yaml file expecting cloud build would display all the local branches on the build machine.

steps:

  - name: bash

    script: |

      #!/usr/bin/env bash

      git branch -a

I can see from the build log that a moment trigger gets executed it creates a local git repo. PLease see below build log line Initialized empty Git repository in /workspace/.git/ So cant we run git commands using bash ? Please help me understand what's wrong with my understanding.

Thank you in advance


Solution

  • I can see from the build log that a moment trigger gets executed it creates a local git repo.

    The reason why the build log shows that an empty Git repository is initialized is because Cloud Build clones the repository from GitHub before it runs the bash script. The bash script will then run in the context of the cloned repository.

    You might have also got the git: command not found error after this as the image where you run the git commands does not have git installed hence you need to modify your cloudbuild.yaml file to use a base image that includes Git. You can use an image that has both Git and Bash pre-installed. You can find them here : git installed images

    Updated cloudbuild.yaml :

    steps:
      - name: 'gcr.io/cloud-builders/git'
        entrypoint: 'bash'
        args:
          - '-c'
          - |
            #!/bin/bash
            git fetch --all
            git branch -a
    

    Instead of the script we have to define the bash commands in the args.