4

I am playing around with Google Cloud Build. In my usecase I have to change a file during the build and commit it into git.

I sketched the build steps like below and they do work. But that is a lot of ceremony for doing a simple git commit.

Do you have any suggestions how to shorten that stuff?

steps:
- name: alpine
  args: ["touch", "some-file.txt"]
- name: gcr.io/cloud-builders/git
  args: [ "config", "--global", "user.name", "batman" ]
- name: gcr.io/cloud-builders/git
  args: [ "config", "--global", "user.email", "batman@gotham.city" ]
- name: gcr.io/cloud-builders/git
  args: [ "add", "-A" ]
- name: gcr.io/cloud-builders/git
  args: [ 'commit', '-m', 'batmans commit' ]
- name: gcr.io/cloud-builders/git
  args: [ 'push', 'https://source.developers.google.com/p/$PROJECT_ID/r/my-repo', 'master']
Hubert Ströbitzer
  • 1,745
  • 2
  • 15
  • 19
  • Do you really need to do `git config` on your username/email each time? This is typically something which you'd do only once. Beyond this, yes, you need all those steps, because you'd be doing the same things directly from the Git Bash. – Tim Biegeleisen Aug 24 '18 at 05:28
  • Yes I have to config `user.name` and `user.email` - the commit step fails otherwise. Setting those values via environment variables would be awesome, but I haven't found anything like that. – Hubert Ströbitzer Aug 24 '18 at 05:39

2 Answers2

4

Push to Private Google repositories

Try to put by command like this:

steps:
- name: 'gcr.io/cloud-builders/git'
  entrypoint: 'bash'
  args:
  - '-c'
  - |
        touch ./some-file.txt
        git config --global user.name batman
        git config --global user.email batman@gotham.city
        git add -A
        git commit -m 'batmans commit'
        git push 'https://source.developers.google.com/p/$PROJECT_ID/r/my-repo' master

Or put them in script file like this:

steps:
- name: gcr.io/cloud-builders/git
  args: ["sh", "git.sh"]

Or change alpine to python like this:

steps:
- name: python
  args: ["sh", "git.sh"]

Or write it using the config elements like this:

steps:
- name: python
  entrypoint: /bin/sh
  args: ['-c', './git.sh]

Then put all of your build steps in the git.sh file:

#!bin/sh
sh ./some-file.txt
git config --global user.name batman
git config --global user.email batman@gotham.city
git add -A
git commit -m 'batmans commit'
git remote set-url origin 'https://source.developers.google.com/p/$PROJECT_ID/r/my-repo'
git push origin master

Push to Private GitHub repositories

If you want to push the repo to Github you can run the builder with kms service along with ssh-agent and or expect. Then simulate the git interaction to private GitHub repositories like this:

steps:
- name: 'gcr.io/cloud-builders/gcloud'
  args: ['kms', 'decrypt', '--location', 'global', 
         '--keyring', 'my-keyring','--key', 'github-key', 
         '--ciphertext-file', 'id_rsa.enc', '--plaintext-file', '/root/.ssh/id_rsa'] 
  volumes:
  - name: 'ssh'
    path: /root/.ssh

- name: 'python'
  entrypoint: 'bash'
  args: ['-c', './git.sh]
  volumes:
  - name: 'ssh'
    path: /root/.ssh

Similar with above you can put steps also resource in the git.sh file:

#!bin/sh
chmod 600 /root/.ssh/id_rsa
cat <<EOF >/root/.ssh/config
Hostname github.com
IdentityFile /root/.ssh/id_rsa
EOF
mv known_hosts /root/.ssh/known_hosts
apt-get update
apt-get --assume-yes install expect
git config --global <github:user.name>
git config --global <github:user.email>
eval `ssh-agent` && expect agent_builder && ssh-add -l
cd /path/to/my-repo
git add .
git commit -m 'batmans commit'
git remote set-url origin 'git@github.com:my-user/my-repo.git'
git push origin master

The file agent_builder contains the following codes:

#!/usr/bin/expect -f
spawn ssh-add /root/.ssh/id_rsa
expect "Enter passphrase for /root/.ssh/id_rsa:"
send "my-passphrase\n";
expect "Identity added: /root/.ssh/id_rsa (/root/.ssh/id_rsa)"
interact

When you set mirror configuration then the commits that you push to the GitHub repository are copied, or mirrored, back into Google repository hosted in Cloud Source Repositories.

eQ19
  • 9,880
  • 3
  • 65
  • 77
1

There is no quick way to drastically reduce this cloudbuild.yml size. You can eventually merge git add -A with git commit -am but if you have new files, it won't work. The only way is to put a homemade script in your repository or in a dedicated docker image that do these steps in one command.

steps:
- name: alpine
  args: ["touch", "some-file.txt"]
- name: hubertstr/gitcommiter:v0.1
  args: [ "-m", "batmans commit", "-u", "batman", "-e", "batman@gotham.city"]
Yann C.
  • 1,315
  • 12
  • 17