Deploying with Git

Git is a popular version control system that is widely used for collaborating on software projects and can be used in conjunction with ScienceOps to keep track of versions of models that are deployed.

Below, we'll show how to:

  • Configure a git repository to trigger a deployment to the production ScienceOps instance when a change to the master branch is pushed
  • And otherwise deploy to the 'Dev' instance on any other push

Setting up the repository

First, lets configure our repo to use a githook. Githooks are what they sound like: hooks....for git!

Below is a simple repository structure:

├── .git
│   └── hooks
└── gitmodel.py

In the /hooks/ folder, we'll add a file called pre-push and make it executable:

$ cd ./git/hooks
$ touch pre-push
$ chmod +x pre-push

# New directory structure
├── .git
│   └── hooks
│       └── pre-push
└── gitmodel.py

Next, lets open the pre-push file and add the following code which will:

  • Get the name of the branch we're on (current_branch)
  • Check if our current_branch = 'master'
  • If it is master, deploy with our master environment variables
  • If it is not master, deploy with our dev environment variables
#!/bin/sh

protected_branch='master'

current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')

if [[ $current_branch = $protected_branch ]]; then
    echo 'pushing to production'
    export USERNAME='kermit'
    export APIKEY='1234567890abcdefb'
    export URL='https://master-url.scienceops.com/'
    python gitmodel.py
else
    echo 'pushing to dev'
    export USERNAME='kermit-dev'
    export APIKEY='abcdefb1234567890'
    export URL='https://dev-url.scienceops.com/'
    python gitmodel.py
fi
exit 0
from yhat import Yhat, YhatModel , preprocess
import os
USERNAME = os.environ["USERNAME"]
APIKEY = os.environ["APIKEY"]
URL = os.environ["URL"]

class HelloWorld(YhatModel):
    @preprocess(in_type=dict, out_type=dict)
    def execute(self, data):
        me = data['name']
        greeting = "Hello " + str(me) + "!"
        return { "greeting": greeting }

yh = Yhat(USERNAME, APIKEY, URL)
yh.deploy("Gitmodel", HelloWorld, globals(),True)

You'll likely not be exporting environment variables from within this script. The export statements below are more realistically something like below, where we grab them from our environment:

export USERNAME=$PROD_USERNAME
export APIKEY=$PROD_APIKEY
export URL=$PROD_URL.

results matching ""

    No results matching ""