Python Development with Cloud Build

To be used with https://cloud.google.com/build/docs/building/build-python

Documentation pages that include this code sample

To view the code sample used in context, see the following documentation:

Code sample

YAML

steps:
  # Install dependencies
  - name: python
    entrypoint: pip
    args: ["install", "-r", "requirements.txt", "--user"]

  # Run unit tests
  - name: python
    entrypoint: python
    args: ["-m", "pytest", "--junitxml=${SHORT_SHA}_test_log.xml"] 

  # Docker Build
  - name: 'gcr.io/cloud-builders/docker'
    args: ['build', '-t', 
           'us-central1-docker.pkg.dev/$PROJECT_ID/$_REPO_NAME/myimage:$SHORT_SHA', '.']

  # Docker push to Google Artifact Registry
  - name: 'gcr.io/cloud-builders/docker'
    args: ['push',  'us-central1-docker.pkg.dev/$PROJECT_ID/$_REPO_NAME/myimage:$SHORT_SHA']

  # Deploy to Cloud Run
  - name: google/cloud-sdk
    args: ['gcloud', 'run', 'deploy', 'helloworld', 
           '--image=us-central1-docker.pkg.dev/$PROJECT_ID/$_REPO_NAME/myimage:$SHORT_SHA', 
           '--region', 'us-central1', '--platform', 'managed', 
           '--allow-unauthenticated']

# Save test logs to Google Cloud Storage
artifacts:
  objects:
    location: gs://$_BUCKET_NAME/
    paths:
      - ${SHORT_SHA}_test_log.xml
# Store images in Google Artifact Registry
images:
  - us-central1-docker.pkg.dev/$PROJECT_ID/$_REPO_NAME/myimage:$SHORT_SHA

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.