Home » Software Development » Google Cloud Build – Hello World

About Biju Kunjummen

Biju Kunjummen

Google Cloud Build – Hello World

I have been exploring Google Cloud Build recently and this post is a simple introduction to this product. You can think of it as a tool that enables automation of deployments. This post though will not go as far as automating deployments, instead just covering the basics of what it involves in getting a pipeline going. A follow up post will show a continuous deployment pipeline for a java application. 

Steps

The basic steps to set-up a Cloud Build in your GCP project is explained here. Assuming that the Cloud Build has been set-up, I will be using this github project to create a pipeline.

Cloud pipeline is typically placed as a yaml configuration in a file named by convention as “cloudbuild.yaml”. The pipeline is described as a series of steps, each step runs in a docker container and the name of the step points to the docker image. So for eg. a step which echo’s a message looks like this:

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

Here the name “bash” points to the docker image named “bash” in docker hub

The project does not need to be configured in Google Cloud Build to run it, instead a utility called
“cloud-build-local” can be used for running the build file. 

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

Alright, now to add a few more steps. Consider a build file with 2 steps:

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

Here the two steps will run serially, first Step A, then Step B. A sample output looks like this on my machine:

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"

Concurrent Steps

A little more complex, say if I wanted to execute a few steps concurrently, the way to do it is using waitFor property of a step.

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

Here “waitFor” value of “-” indicates the start of the build, so essentially Step A and B will run concurrently and an output in my machine looks like this:

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

One more example where Step A is executed first and then Step B and Step C concurrently:

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"

Passing Data

A root volume at path “/workspace” carries through the build, so if a step wants to pass data to another step then it can be passed through this “/workspace” folder.

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

Here Step A is writing to a file and Step B is reading from the same file.

Conclusion

This covers the basics of the steps in a Cloud Build configuration file. In a subsequent post I will be using these to create a pipeline to deploy a java based application to
Google Cloud Run.

Published on Java Code Geeks with permission by Biju Kunjummen, partner at our JCG program. See the original article here: Google Cloud Build – Hello World

Opinions expressed by Java Code Geeks contributors are their own.

(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments