Skip to content

Commit

Permalink
ci(pulish): add github workflows for publish models to server (#14)
Browse files Browse the repository at this point in the history
Co-authored-by: liuke <[email protected]>
  • Loading branch information
gpulost and liuke authored Mar 21, 2021
1 parent d02427e commit 540ecd2
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
44 changes: 44 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# This is a basic workflow to help you get started with Actions

name: CI

# Controls when the action will run.
on:
# Triggers the workflow on push or pull request events but only for the master branch
push:
branches: [ master ]
pull_request:
branches: [ master ]

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
build:
# The type of runner that the job will run on
runs-on: ubuntu-latest

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2

- uses: actions/setup-python@v2
with:
python-version: '3.8' # Version range or exact version of a Python version to use, using SemVer's version range syntax
architecture: 'x64' # optional x64 or x86. Defaults to x64 if not specified

- name: Run Install requirements
run: pip3 install -r scripts/requirements.txt

- name: Run Build Dist
run: python3 scripts/generate_data.py --source=models --output=dist

- name: Run Publish to Server
run: python3 scripts/publish.py
env:
ENDPOINT: ${{ secrets.ENDPOINT }}
HEADERAUTHNAME: ${{ secrets.HEADERAUTHNAME }}
HEADERAUTHVALUE: ${{ secrets.HEADERAUTHVALUE }}
69 changes: 69 additions & 0 deletions scripts/publish.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import os
import json

import requests
from loguru import logger

MainDir = os.path.dirname(os.path.abspath(os.path.dirname(__file__)))


class Config:
Endpoint = None
HeaderAuthName = None
HeaderAuthValue = None
ModelDistDir = os.path.join(MainDir, "dist/api/v1")
ModelJsonPath = "models.json"
ModelDetailPath = "models"


def load_config():
Config.Endpoint = os.environ["ENDPOINT"]
Config.HeaderAuthName = os.environ["HEADERAUTHNAME"]
Config.HeaderAuthValue = os.environ["HEADERAUTHVALUE"]


def load_models():
info = json.loads(open(os.path.join(Config.ModelDistDir, Config.ModelJsonPath), "rb").read())
tags = [
{
"key": key,
"value": info["tags"][key],
} for key in info["tags"]
]

model_ids = [
_["id"] for _ in info["models"]
]
return model_ids, tags


def load_model_details():
models_ids, tags = load_models()
dir = os.path.join(Config.ModelDistDir, Config.ModelDetailPath)
models = []
for model_id in models_ids:
models.append(json.loads(open(os.path.join(dir, f"{model_id}.json"), "rb").read()))

return models, tags


def sync_server():
models, tags = load_model_details()
headers = {
Config.HeaderAuthName: Config.HeaderAuthValue,
}

for model in models:
logger.info(f"model: {json.dumps(model)}")
resp = requests.post(f"{Config.Endpoint}/api/v1/models", json=model, headers=headers)
logger.info(f"resp.code: {resp.status_code}, resp.content: {resp.content}")

for tag in tags:
logger.info(f"tag: {json.dumps(tag)}")
resp = requests.post(f"{Config.Endpoint}/api/v1/tags", json=tag, headers=headers)
logger.info(f"resp.code: {resp.status_code}, resp.content: {resp.content}")


if __name__ == "__main__":
load_config()
sync_server()
2 changes: 2 additions & 0 deletions scripts/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ black==19.10b0
mistune==2.0.0a1
attrs==19.1.0
PyYAML==5.2
requests==2.25.1
loguru==0.5.3

0 comments on commit 540ecd2

Please sign in to comment.