在 Cloud Build CI/CD 管道中发出curl 请求

Dar*_*aik 2 continuous-integration google-cloud-platform google-cloud-build

我有一台服务器,我们的测试用例在 GCP 的计算引擎上运行所有 API。如何从云构建 CI/CD 管道连接它,以便 CI/CD 阶段仅传递来自服务器的200响应状态代码?

GCP 表示创建自定义构建步骤(此处)。文档不是很清楚

gui*_*ere 8

你有2个解决方案。

  • 您可以有效地创建自定义步骤。构建一个容器,通过ENTRYPOINT将在 Cloud Build 管道中调用的容器来完成它
  • 您可以在包含该命令的任何步骤中执行curl 调用,获取返回代码并对其应用条件(此处如果与200 不同则退出)。这是代码示例
steps:
        - name: gcr.io/cloud-builders/gcloud
          entrypoint: "bash"
          args:
                  - "-c"
                  - |
                      RESPONSE=$(curl -i <YOUR URL> | grep HTTP | cut -d' ' -f2)
                      if [ "200" != "$$RESPONSE" ]; then exit 1; fi
Run Code Online (Sandbox Code Playgroud)

请注意双重$$以防止 Cloud Build 查看替换变量

  • RESPONSE=$(curl -o /dev/null -s -w "%{http_code}" &lt;YOUR URL&gt;) 将返回状态代码 - 不需要 grep 和 cut (2认同)