Get Git History in Google Cloud Build Step

Viewed 780

I'm using Google Cloud Build to run CI for my Nx workspace. Here's the cloudbuild.yaml file:

    steps:
      - name: 'gcr.io/cloud-builders/docker'
        id: Test_Affected_Projects
        entrypoint: 'sh'
        args: [
            '-c',
            'docker build --build-arg NPM_TOKEN=$$NPM_TOKEN --file ./test/Dockerfile.test-runner -t mha-test-runner .']
        secretEnv: ['NPM_TOKEN']
    # Remove the docker image
    secrets:
    - kmsKeyName: /path/to/key
      secretEnv:
        NPM_TOKEN: some_key_value

(There are currently two steps, but I removed the second for brevity. The second step just removes the created docker image.)

Now the command inside the Docker image here runs all the tests for the Nx workspace. The thing is, Nx has a great command where only the affected libraries will be tested. But for the command to run, the git history of the project needs to be available.

I've tried to get the git history in the cloud build context, but I haven't been able to get it working. This is the step I added to try and get everything working:

steps:
  - name: 'gcr.io/cloud-builders/git'
    args: ['fetch', '--unshallow']
  - name: 'gcr.io/cloud-builders/docker'
    id: Test_Affected_Projects
    entrypoint: 'sh'
    args: [
        '-c',
        'docker build --build-arg NPM_TOKEN=$$NPM_TOKEN --file ./test/Dockerfile.test-runner -t mha-test-runner .']
    secretEnv: ['NPM_TOKEN']
# Remove the docker image
secrets:
- kmsKeyName: /path/to/key
  secretEnv:
    NPM_TOKEN: some_key_value

That new first command, which should get the git history, fails. The error message says that it's not a git repo, so the command fails.

My question is: how can I get the git history in the cloud build context so that I can use it with different commands in the build/testing process?

1 Answers

I think the reason this isn't working is that you need to store the github credentials in the cloud build environment.

I believe this guide can help.

will allow you to do so, and then you will be able to call the git fetch --unshallow as you already have.

Related