Search code examples
google-cloud-platformcontinuous-integrationgoogle-cloud-build

Make a curl request in Cloud Build CI/CD pipeline


I have a server where our test cases run for all API, which is on the compute engine of GCP. How can I connect it from cloud build CI/CD pipeline so that the CI/CD stage passes only on 200 response status code from the server?

GCP says to create a custom build step (here). Docs are not very clear


Solution

  • You have 2 solutions.

    • You can effectively create a custom step. Build a container, finish it by an ENTRYPOINT which will be invoked in the Cloud Build pipeline
    • You can perform a curl call inside any steps which contain the command, get the return code and apply a condition on it (here exit if different of 200). Here an example of code
    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
    

    Note the double $$ to prevent Cloud Build to look into Substitution variables