1

I'm using cloud build to clone a repository. I can confirm the repository clones successfully to the cloud build /workspace volume.

steps:
  - id: 'Clone repository'
    name: 'gcr.io/cloud-builders/git'
    args: ['clone', $_REPO_URL]
    volumes:
    - name: 'ssh'
      path: /root/.ssh

I then run the next step to confirm

  - id: 'List'
    name: 'alpine'
    args: ['ls']

and it shows me the repository is in the current directory. But when I try and cd into the directory the cd command doesn't work and throws an error:

ERROR: build step 3 "alpine" failed: starting step container failed: Error response from daemon: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "cd <repo-name>": executable file not found in $PATH: unknown

My ultimate goal is to cd into the repository and run some git commands. I use alpine later on because the git builder image doesn't allow me to use cd either.

substitutions:
  _REPO_NAME: 'test-repo'
  _REPO_URL: 'git@bitbucket.org:example/test-repo.git'
  _BRANCH_NAME: 'feature/something'

steps:
  - id: 'Clone repository'
    name: 'gcr.io/cloud-builders/git'
    args: ['clone', $_REPO_URL]
    volumes:
    - name: 'ssh'
      path: /root/.ssh

  - id: 'Check Diff'
    name: 'alpine'
    args: ['cd $_REPO_NAME', '&&', 'git checkout $_BRANCH_NAME', '&&', 'git diff main --name-only']
Ari
  • 5,301
  • 8
  • 46
  • 120

2 Answers2

6

You can use bash to run any commands you would like. Here is one example I use for one of my projects:

- name: 'gcr.io/cloud-builders/git'
  id: Clone env repository
  entrypoint: /bin/sh
  args:
  - '-c'
  - |
    git clone git@github.com:xyz/abc.git && \
    cd gitops-env-repo/ && \
    git checkout dev   
CaioT
  • 1,973
  • 1
  • 11
  • 20
4

Use dir field in your *.yaml file.

steps:
- name: string
  args: [string, string, ...]
  env: [string, string, ...]
  dir: string
  id: string
  waitFor: [string, string, ...]
  entrypoint: string
  secretEnv: string
  volumes: object(Volume)
  timeout: string (Duration format)
- name: string
  ...
- name: string
  ...
timeout: string (Duration format)
queueTtl: string (Duration format)
logsBucket: string
options:
 env: [string, string, ...]
 secretEnv: string
 volumes: object(Volume)
 sourceProvenanceHash: enum(HashType)
 machineType: enum(MachineType)
 diskSizeGb: string (int64 format)
 substitutionOption: enum(SubstitutionOption)
 dynamicSubstitutions: boolean
 logStreamingOption: enum(LogStreamingOption)
 logging: enum(LoggingMode)
 pool: object(PoolOption)
substitutions: map (key: string, value: string)
tags: [string, string, ...]
serviceAccount: string
secrets: object(Secret)
availableSecrets: object(Secrets)
artifacts: object (Artifacts)
images:
- [string, string, ...]

https://cloud.google.com/build/docs/build-config-file-schema

Legolas Bloom
  • 1,725
  • 1
  • 19
  • 17