谷歌云建设--你好,世界

80 阅读3分钟

谷歌云建设--你好,世界

发布者::Biju Kunjummen inSoftware Development August 25th, 2021 0 Views

我最近一直在探索Google Cloud Build,这篇文章是对这个产品的简单介绍。你可以把它看成是一个实现自动化部署的工具。这篇文章并不涉及部署的自动化,而只是涵盖了它所涉及的管道的基本内容。后续的文章将展示一个java应用程序的持续部署管道。

操作步骤

这里解释了在GCP项目中设置云构建的基本步骤。假设已经建立了云构建,我将使用这个github项目来创建一个管道。

云计算管道通常作为一个yaml配置放在一个文件中,按惯例命名为 "cloudbuild.yaml"。该管道被描述为一系列的步骤,每个步骤都在一个docker容器中运行,步骤的名称指向docker镜像。因此,例如,一个响应信息的步骤看起来像这样。

steps:
  - name: 'bash'
    id: hello
    args: ['echo', 'Hello World']

这里 "bash "这个名字指向docker hub中名为 "bash "的docker镜像

该项目不需要在Google Cloud Build中配置来运行它,相反,可以使用一个名为
"cloud-build-local "的工具来运行构建文件。

git clone git@github.com:bijukunjummen/hello-cloud-build.git
cd hello-cloud-build
cloud-build-local .

好了,现在要多加几个步骤。考虑一个有两个步骤的构建文件。

steps:
  - name: 'bash'
    id: A
    args: ['echo', 'Step A']
  - name: 'bash'
    id: B
    args: ['echo', 'Step B']

在这里,这两个步骤将连续运行,首先是步骤A,然后是步骤B。在我的机器上,样本输出是这样的。

Starting Step #0 - "A"
Step #0 - "A": Already have image (with digest): bash
Step #0 - "A": Step A
Finished Step #0 - "A"
2021/08/10 12:50:23 Step Step #0 - "A" finished
Starting Step #1 - "B"
Step #1 - "B": Already have image (with digest): bash
Step #1 - "B": Step B
Finished Step #1 - "B"
2021/08/10 12:50:25 Step Step #1 - "B" finished
2021/08/10 12:50:26 status changed to "DONE"

并行步骤

再复杂一点,比如说我想同时执行几个步骤,方法是使用一个步骤的waitFor属性。

steps:
  - name: 'bash'
    id: A
    args: ['echo', 'Step A']
    waitFor:
      - "-"
  - name: 'bash'
    id: B
    args: ['echo', 'Step B']
    waitFor:
      - "-"

这里 "waitFor "的值为"-",表示构建的开始,所以基本上步骤A和B将同时运行,在我的机器上的输出是这样的。

Starting Step #1 - "B"
Starting Step #0 - "A"
Step #1 - "B": Already have image (with digest): bash
Step #0 - "A": Already have image (with digest): bash
Step #1 - "B": Step B
Step #0 - "A": Step A
Finished Step #1 - "B"
2021/08/10 12:54:21 Step Step #1 - "B" finished
Finished Step #0 - "A"
2021/08/10 12:54:21 Step Step #0 - "A" finished

还有一个例子,步骤A首先被执行,然后步骤B和步骤C同时被执行。

steps:
  - name: 'bash'
    id: A
    args: ['echo', 'Step A']
  - name: 'bash'
    id: B
    args: ['echo', 'Step B']
    waitFor:
      - "A"
  - name: 'bash'
    id: C
    args: ['echo', 'Step C']
    waitFor:
      - "A"

传递数据

在路径"/workspace "上有一个根卷,因此如果一个步骤想把数据传递给另一个步骤,可以通过这个"/workspace "文件夹传递。

steps:
  - name: 'bash'
    id: A
    entrypoint: bash
    args:
      - -c
      - |
        echo "hello" > /workspace/saved.txt
  - name: 'bash'
    id: B
    entrypoint: bash
    args:
      - -c
      - |
        cat /workspace/saved.txt

这里的步骤A正在向一个文件写入,而步骤B正在从同一个文件中读取。

总结

这涵盖了Cloud Build配置文件中步骤的基本内容。在随后的文章中,我将使用这些步骤创建一个管道,将一个基于java的应用程序部署到
Google Cloud Run

由我们JCG项目的合伙人Biju Kunjummen授权发表在Java Code Geeks上。点击这里查看原文。Google Cloud Build - Hello World

Java Code Geeks撰稿人所表达的观点属于他们自己。

2021-08-25

毕朱-昆朱门(Biju Kunjummen