Productivity Tools

Tools written by an IT business manager, a passionate developer.

Deploy python app app to cloud run from git repository

This tutorial shows how to publish python web application to the Cloud Run using Cloud Build.


Let us assume that we have the simplest python application.

Please notice the line. After deploying app to Cloud Run GCP performs health check on the port provided in the GCP config so you need to open app on this port. The easiest way to do it is to read the GCP configuration.

from flask import Flask
from flask_restful import Api
import os

from Resources.CardResource import CardResource

def create_app():
    return app

def register_resources(app):
    api.add_resource(CardResource, '/Card')

if __name__=="__main__":
    app=create_app()"PORT", 8080)),host='',debug=True)

Card resource

from flask import request
from flask_restful import Resource
from http import HTTPStatus

class CardResource(Resource):
    def get(self):
        return {'data':'pawel'}, HTTPStatus.OK



Let us add cloudbuild.yaml file with the content

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

Docker fle

FROM python:3.10-slim
COPY . ./
RUN pip install --no-cache-dir -r requirements.txt
CMD ["python", ""]

Cloud build

Go to Cloud build in the console. Enable API and ceate new Activator.

New activator

Run it

Run cloud run

Go to history and validate the step


Build step

In the build step we need to name application. Name should contain path to the Artifact Registry what is why we need perform couple steps before build even that right now we won’t be using elements which we use.

Enable Artificat Registry


Create a new repository for our image:


Please remember the path of our container repository. We will need it (we even have small copy button on the right).


Build step

## Docker Build
- name: ''
  args: ['build', '-t',
         '${PROJECT_ID}/apiimage/image:${SHORT_SHA}', '.']

Go to Cloud run and valide if it is working

Run cloud run

As we can see two steps finished with sucess.

Two steps working

Until now we restored requirements, and we build the application. Now we need to store it into Artifact Registry

Artifact Registry

We already enabled the API. Let us add new step to our yaml file. We are using the same image (‘’) to invoke command. Our command is push as a parameter we are giving name of the image which we build. We are omitting the sha part as it is only a tag (metadata) to the file.

## Docker push to Google Artifact Registry
- name: ''
  args: ['push',  '${PROJECT_ID}/apiimage/image']

Let us again run the workflow.

Three steps

After sucessfull invocation go to Artifact Registry and check if you have your image there.


When you click on it, you can see the versions of the images. In the Tag column we have the sha of commit


[] does not have permission to access namespaces instance [pttechbricksapi-347809] (or it may not exist): Permission ‘iam.serviceaccounts.actAs’ denied on service account (or it may not exist).

Deploy to Cloud Run

- name: google/cloud-sdk
  args: [ 'gcloud', 'run', 'deploy', 'mycl-${SHORT_SHA}',
          '--region', 'us-central1', '--platform', 'managed',
          '--allow-unauthenticated' ]

If all steps will finish with sucess,

you should see the new app in the Cloud Run

Cloud Run


The last step was most difficult for me if you face **Permission "" denied on resource ** validate if you have correct permissions in the Settings enabled.


Additional resources