Daz Wilkin
Aug 31, 2018 · 3 min read

A developer on Stackoverflow sought a way to deploy App Engine apps using Cloud Build. It’s an interesting question. A caveat is that App Engine Standard deployments include a build step. Let’s explore…

Setup

PROJECT=[[YOUR-PROJECT]]
BILLING=[[YOUR-BILLING]]
REGION=[[YOUR-REGION]]
BUCKET=[[YOUR-BUCKET]]
gcloud projects create ${PROJECT}gcloud beta billing projects link ${PROJECT} \
--billing-account=${BILLING}
# Enable App Engine (Admin) and Cloud Build services
for SERVICE in appengine cloudbuild
do
gcloud services enable ${SERVICE}.googleapis.com \
--project=${PROJECT}
done
# Create App Engine (Standard)
gcloud app create \
--region=${REGION} \
--project=${PROJECT}
# Create a GCS Bucket for Artifacts
gsutil mb \
-c regional \
-l ${REGION} \
-p ${PROJECT} \
gs://${BUCKET}

Golang

I find the workspace options for the Golang builder confusing. So, given that my local directory structure always looks like this:

echo ${GOPATH}/..├── cloudbuild.yaml
└── go
├── bin
├── pkg
└── src
└── github.com
└── DazWilkin
└── hellohenry
├── app.yaml
└── hellohenry.go

NB The inclusion of the cloudbuild.yaml file in the root of this working directory.

I’m going to get the builder to mirror this structure.

I used something similar to Google’s sample helloworld app. In my case the Golang source and the app.yaml are in a sub-directory of go/src called ${APP}. (== github.com/DazWilkin/hellohenry). This will become clearer when we review the cloudbuild.yaml:

tree ${GOPATH}/src/${APP}├── app.yaml
└── hellohenry.go

Cloud Build

Eventually Cloud Build will need permission to deploy App Engine apps. Let’s grab the Cloud Build service account and grant it appengine.deployerand appengine.serviceAdmin:

NUM=$(gcloud projects describe $PROJECT \
--format="value(projectNumber)") && echo ${NUM}
FOR role in appengine.deployer appengine.serviceAdmin
do
gcloud projects add-iam-policy-binding ${PROJECT} \
--member=serviceAccount:${NUM}@cloudbuild.gserviceaccount.com \
--role=roles/${ROLE}
done

And, lastly we can define the cloudbuild.yaml:

NB For clarity, I’ve intentionally included SUBSTITUTIONS in this cloudbuild.yaml and the values for my configuration. You will need to either remove these then include them in the gcloud build command, and replace the values with your own.

Here is why I find it easier to replicate the file structure on the Cloud Build container. Running from the working directory, the go directory is copied to the container’s /workspace directory. We can then mirror our local configuration by configuring the builder to use GOPATH=go (== /workspace/go) which avoids weirdness with package imports etc.

After running the go get step, we run gcloud app deploy. You may wonder why we’re missing the go build. It is not needed because gcloud app deploy takes the artifacts and builds the solution for us before deployment. You may add a go build step but it is redundant and the binary deployed to App Engine would not be identical.

To run the build, from your working directory:

gcloud builds submit . \
--config=cloudbuild.yaml \
--project=${PROJECT} \
--substitutions=_APP=${APP},_BUCKET=${BUCKET}

NB If you define the substitutions in the cloudbuild.yaml, you do not need to duplicate them here. One or the other.

Cloud Build: build history

And then:

App Engine Console

And:

curl https://${PROJECT}.appspot.com/Hello Henry!

And to check the artifacts:

Google Cloud Storage: [Bucket] Browser

Conclusion

A quick-hit to help describe how to build and deploy Golang apps using Cloud Build to Google App Engine.

That’s all!

Google Cloud Platform - Community

A collection of technical articles published or curated by Google Cloud Platform Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.

 by the author.

Daz Wilkin

Written by

Google Cloud Platform - Community

A collection of technical articles published or curated by Google Cloud Platform Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade