How can I specify a region for the Cloud Storage buckets used by Cloud Build for a Cloud Run deployment?

前端 未结 2 417
忘掉有多难
忘掉有多难 2021-01-12 03:16

When deploying a docker container image to Cloud Run, I can choose a region, which is fine. Cloud Run delegates the build to Cloud Build, which apparently creates two bucke

相关标签:
2条回答
  • 2021-01-12 03:41

    Looks like this is only possible by doing what you're mentioning in the comments:

    1. Create a storage bucket in us-east1 as the source bucket ($SOURCE_BUCKET);
    2. Create a Artifact Registry repo in us-east1;
    3. Create the following cloudbuild.yaml:
      steps:
      - name: 'gcr.io/cloud-builders/docker'
        args: ['build', '-t', 'us-east1-docker.pkg.dev/$PROJECT_ID/my-repo/my-image', '.']
      images:
      - 'us-east1-docker.pkg.dev/$PROJECT_ID/my-repo/my-image'
      
    4. Deploy with:
      $ gcloud builds submit --config cloudbuild.yaml --gcs-source-staging-dir=gs://$SOURCE_BUCKET/source
      

    More details here: https://cloud.google.com/artifact-registry/docs/configure-cloud-build

    I think it should at least be possible to specify the Artifact Registry repo with the --tag option and have it be automatically created, but it currently rejects any domain that isn't gcr.io outright.

    0 讨论(0)
  • 2021-01-12 03:48

    As you mention, Cloud Build creates a bucket or buckets with multi region because when creating the service in Cloud Run, there are only added the needed flags and arguments to deploy the service.

    The documentation for the command gcloud builds submit mentions the following for the flag --gcs-source-staging-dir:

    --gcs-source-staging-dir=GCS_SOURCE_STAGING_DIR
    

    A directory in Google Cloud Storage to copy the source used for staging the build. If the specified bucket does not exist, Cloud Build will create one. If you don't set this field, gs://[PROJECT_ID]_cloudbuild/source is used.

    As this flag is not set, the bucket is created in multi-region and in us. This behavior also applies for the flag --gcs-log-dir.

    Now the necessary steps to use the bucket in the dual-region, region or multi-region you want is using a cloudbuild.yaml and using the flag --gcs-source-staging-dir. You can do the following:

    1. Create a bucket in the region, dual-region or multi-region you may want. For example I created a bucket called "example-bucket" in australia-southeast1.

    2. Create a cloudbuild.yaml file. This is necessary to store the artifacts of the build in the bucket you want as mentioned here. An example is as follows:

      steps:
      - name: 'gcr.io/cloud-builders/gcloud'
        args:
        - 'run'
        - 'deploy'
        - 'cloudrunservice'
        - '--image'
        - 'gcr.io/PROJECT_ID/IMAGE'
        - '--region'
        - 'REGION_TO_DEPLOY'
        - '--platform'
        - 'managed'
        - '--allow-unauthenticated'
      artifacts:
        objects:
          location: 'gs://example-bucket'
          paths: ['*']
      
    3. Finally you could run the following command:

      gcloud builds submit --gcs-source-staging-dir="gs://example-bucket/cloudbuild-custom" --config cloudbuild.yaml
      

    The steps mentioned before can adapted to your script. Please give a try :) and you will see that even if the Cloud Run service is deployed in Asia, Europe or US, the bucket specified before can be in another location.

    0 讨论(0)
提交回复
热议问题