2

I need to have the git history in cloudbuild to detect which apps in my nx monorepo had changes. My current approach to getting the history is the following:

steps:
  - id: 'create github ssh configurations'
    name: 'gcr.io/cloud-builders/git'
    secretEnv: ['SSH_KEY']
    entrypoint: 'bash'
    args:
    - -c
    - |
      echo "$$SSH_KEY" >> /root/.ssh/id_rsa
      chmod 400 /root/.ssh/id_rsa
      cp known_hosts.github /root/.ssh/known_hosts
      git config --global user.email "a@b.com"
    volumes:
    - name: 'ssh'
      path: /root/.ssh

  - id: 'fetch more history'
    name: 'gcr.io/cloud-builders/git'
    script: |
      #!/usr/bin/env bash
      git remote set-url origin git@github.com:<user>/<repo>.git
      git fetch
      git checkout -b deploy-branch
      git add -A
      git commit -m "dep commit"
      git checkout main
      git pull
      git checkout deploy-branch
      git rebase main -X theirs
    volumes:
    - name: 'ssh'
      path: /root/.ssh

  - other steps


availableSecrets:
  secretManager:
  - versionName: projects/<>/secrets/<>/versions/latest
    env: 'SSH_KEY'

How can I avoid having all those git commands, especially in the second step? I would like to primarily use this cloudbuild everytime there is a merge to main. If I can also use it for manual deployments its a bonus.

sev
  • 1,500
  • 17
  • 45

0 Answers0