在Google Cloud build中使用git容器进行git commit

时间:2018-08-24 05:23:56

标签: git docker google-cloud-platform

我正在玩Google Cloud Build。在我的用例中,我必须在构建过程中更改文件并将其提交到git中。

我绘制了如下的构建步骤,它们确实起作用。但这是做一个简单的git commit的很多仪式。

您对缩短这些内容有什么建议吗?

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']

2 个答案:

答案 0 :(得分:1)

没有快速的方法来大大减小此cloudbuild.yml的大小。您最终可以将git add -Agit commit -am合并,但是如果有新文件,它将无法正常工作。唯一的方法是将自制脚本放入您的存储库或专用的docker映像中,这些命令可以通过一个命令执行这些步骤。

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

答案 1 :(得分:0)

  

推送到私有Google存储库

尝试像这样放置by command

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

或者像这样将它们放在script file中:

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

或将alpine更改为python,如下所示:

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

或使用config elements这样写:

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

然后将所有build steps放入git.sh文件中:

#!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
  

推送到私有GitHub存储库

如果要将存储库推送到Github,可以将kms servicessh-agent and or expect一起运行构建器。然后像这样模拟git interaction to private GitHub repositories

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

与上述类似,您也可以在git.sh文件中resource放置步骤:

#!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

文件agent_builder包含以下代码:

#!/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

设置mirror configuration时,您推送到GitHub存储库的提交将被复制或镜像,重新回到托管在Cloud Source Repositories中的Google存储库中。