Django deployment to Google App Engine with Github Action



Introduction

Github action was released by Github as a feature to enable automation and customization of the development to deployment workflows for software development teams. Github actions ease the flow of execution, testing and possibly deployment of repository residing on Github. Also, Github actions provide the flexibility of creating custom flows and steps that are available in standards Continuous Integration (CI) and Continuous Deployment (CD) solutions.

Additionally, using Github actions,process or flow of reviewing codes, triaging issues and managing repository branches and updating dependent libraries in projects become very easy and seamless. The full release note and information is available here https://github.com/features/action

Deploying Django To Google App Engine.

Let's explore how Github actions can be used to automate deployment of a Django application hosted on Github to Google App Engine. There are different approaches to setting up Github actions for your repository and a repository can have multiple actions setup and hooked to different events. A Github action could be set up to listen to pull request merge events to a main branch, and then proceed to build, test and deploy to any of the cloud providers of choice.

In order to ensure a Django app can be deployed from Github to Google app engine, there are certain considerations and setup procedures. The root folder of your Django application should contain the following files main.py, requirements.txt and app.yaml

See a sample App.yaml file below

            # App.yaml file

            # https://cloud.google.com/appengine/docs/standard/python3/config/appref

            runtime: python39
            instance_class: F2

            env_variables:
             DJANGO_SETTINGS_MODULE: "settings"
             DB_NAME: "db_name"
             DB_USERNAME: "db_user"
             DB_PASSWORD: "db_password"
             DB_HOST: "/cloudsql/double-media-xxxx:europe-west1:db_name"
             DB_PORT: 3306
             DB_TIMEOUT: 5000

            handlers:
            # This configures Google App Engine to serve the files in the app's static
            # directory.

            - url: /static
             static_dir: static/

            # This handler routes all requests not caught above to your main app. It is
            # required when static routes are defined, but can be omitted (along with
            # the entire handlers section) when there are no static files defined.

            - url: /.*
             secure: always
             redirect_http_response_code: 301
             script: auto
         


Then proceed to create .github/workflows folder in your repository root folder and create a github action to subscribe to the push event on the main branch, see the sample action file below.

                # .github/workflows/deploy

                # https://cloud.google.com/python/django/appengine
                # https://github.com/actions-hub/gcloud:


                name: deploy-app-to-gcp
                on:
                 push:
                   branches: [ main]
                   paths:
                     - 'app/**'
                jobs:
                 deploy:
                   name: Deploy
                   runs-on: ubuntu-latest

                   steps:
                     - uses: actions/checkout@v1
                     - uses: actions-hub/gcloud@master
                       env:
                         PROJECT_ID: ${{secrets.GCLOUD_PROJECT_PROD_ID}}
                         APPLICATION_CREDENTIALS: ${{secrets.GCLOUD_GITHUB_CREDENTIALS}}
                       with:
                         args: app deploy ./app.yaml

  


The github action posted above has depends on two other actions, the first to checkout the latest code and then deploy the Django app to Google app engine, using the credentials and project id that can be set in Github secrets.




Share this page on


  0 People Like(s) This Page   Permalink  

 Click  To Like This Page

comments powered by Disqus

page