1
votes

Goal:

Using Cloud Build configuration - build an app into an artifact that can be deployed to app engine.

Solution that's not working:

This is cloudbuild.yaml:

steps:
- name: 'gcr.io/cloud-builders/go'
  args: ['get']
  env: ['PROJECT_ROOT=project-name']
- name: 'gcr.io/cloud-builders/go'
  args: ['build', '.']
  env: ['PROJECT_ROOT=project-name']
- name: 'gcr.io/cloud-builders/gcloud'
  args: ['app', 'deploy']
timeout: '1600s'

artifacts:
  objects:
    location: 'gs://project-artifacts/'
    paths: ['project-name']

App Engine config app.yaml:

runtime: go
api_version: go1

handlers:
- url: /.*
  script: _go_app

And finally main.go: https://github.com/GoogleCloudPlatform/golang-samples/blob/master/appengine/helloworld/hello.go

I do understand that the deploy step is now very naive and doesn't even need the previous 2 steps. Consulting the cloud app help, I wasn't able to see whether the deploy step can accept external artifact.

The deploy step fails with:

failed analyzing /workspace: cannot find package "google.golang.org/appengine" in any of:
    ($GOROOT not set)
    /builder/home/go/src/google.golang.org/appengine (from $GOPATH)
GOPATH: /builder/home/go

I'm looking for 2 solutions:

  1. Ideally being able to build artifacts into project-artifacts bucket and deploy them using the deploy step
  2. If that's not possible I at least need to know how to run gcloud components install app-engine-go within the deploy container so it doesn't fail with missing dependency.
1
Mmmm..... Interesting question. I've not tried using Cloud Build with App Engine but you've compelled me to try it. From experience, I've found cloud-builders/go difficult to configure. I'll have a go and report back! - DazWilkin
Here's an example that uses Cloud Build to deploy an App Engine sample app (gist.github.com/DazWilkin/05984055d6e80cfcab6258b3da85e30a). As mentioned, I find the Go build challenging but, this simply mirrors my local go structure on the Cloud Build container and then does the deploy from the src directory. - DazWilkin
IIUC to upload directories using artifacts you'd either need to add a zip step and upload the zip or enumerate all the files. Alternatively, you may wish to drop artifacts and add a gsutil build step to recursively copy your artifacts. - DazWilkin
The gist you shared helped me to make the deployment work. But I run the deployment from my project folder that is already deep in the go src folder. That means that all my sources are in /workspace in the container. I hacked it temporarily by moving my sources to ./app subfolder. Thanks so much for spending time trying this out! - UltraMaster

1 Answers

2
votes

Use a GOPATH outside of your build directory (/workspace). For example, /gopath.

Additionally, because by default only /workspace is preserved between Cloud Build steps, you must put your GOPATH in a volume.

The following complete example works for me:

steps:

- name: 'gcr.io/cloud-builders/go'
  args: ['get', '-d', './...']
  env: ['GOPATH=/gopath']
  volumes:
  - name: 'go'
    path: '/gopath'

- name: 'gcr.io/cloud-builders/gcloud'
  args: ['app', 'deploy']
  env: ['GOPATH=/gopath']
  volumes:
  - name: 'go'
    path: '/gopath'

Additionally, I had to grant the App Engine Admin role to the Cloud Build service account in IAM for the gcloud app deploy step to succeed within Cloud Build.