Skip to content

Commit

Permalink
Merge pull request #54 from datalab-mi/feature/get-version-in-api
Browse files Browse the repository at this point in the history
Feature/get version in api
  • Loading branch information
leihuayi authored Apr 29, 2022
2 parents 08e105a + 6c9462f commit 92673b2
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 17 deletions.
21 changes: 19 additions & 2 deletions .github/workflows/develop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Get the version
id: get_version
run: |
echo ::set-output name=VERSION::$(cat Makefile | grep $'^TAG\t\+:= ' | sed 's/TAG\t\+:= //')
- name: Build the Docker image
run: TAG=action docker-compose -f docker-compose-prod.yml build
run: TAG=${{ steps.get_version.outputs.VERSION }} docker-compose -f docker-compose-prod.yml build
- name: Login to GHCR
if: success()
uses: docker/login-action@v1
Expand All @@ -19,7 +23,8 @@ jobs:
password: ${{ secrets.GITHUB_TOKEN }}
- name: Push on GHCR
run: |
./cd/scripts/docker-push.sh develop
./cd/scripts/docker-push.sh ${{ steps.get_version.outputs.VERSION }} ${{ steps.get_version.outputs.VERSION }}
./cd/scripts/docker-push.sh ${{ steps.get_version.outputs.VERSION }} develop
- name: Delete untagged images
uses: vlaurin/action-ghcr-prune@main
with:
Expand All @@ -36,6 +41,7 @@ jobs:
container: basegun/basegun-frontend
dry-run: false
untagged: true

deploy-preprod:
uses: ./.github/workflows/deploy.yml
needs: build-push
Expand All @@ -52,3 +58,14 @@ jobs:
OS_PROJECT_NAME: ${{ secrets.OS_PROJECT_NAME }}
OS_USERNAME: ${{ secrets.OS_USERNAME }}

test:
runs-on: ubuntu-latest
needs: deploy-preprod
steps:
- name: Test ip
run : |
[[ "$(curl ${{ secrets.PREPROD_SERVER_IP }}/api/)" == "Basegun backend" ]] && echo "Test ip OK"
- name: test DNS
if: success()
run : |
[[ "$(curl preprod.basegun.fr/api/)" == "Basegun backend" ]] && echo "Test DNS OK"
4 changes: 3 additions & 1 deletion .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ jobs:
- name: Build docker image for tests
run: docker build --target test -t basegun-back:tests backend/
- name: Start container
run: docker run --rm --name basegun_back_test -d basegun-back:tests
run: |
VERSION=$(cat Makefile | grep $'^TAG\t\+:= ' | sed 's/TAG\t\+:= //')
docker run --rm -e VERSION=$VERSION --name basegun_back_test -d basegun-back:tests
- name: Run tests
run: docker exec basegun_back_test python -m unittest discover -v
12 changes: 6 additions & 6 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Get the version
id: get_version
run: echo ::set-output name=VERSION::${GITHUB_REF/refs\/tags\//}
- name: Build the Docker image
run: TAG=action docker-compose -f docker-compose-prod.yml build
run: TAG=${{ steps.get_version.outputs.VERSION }} docker-compose -f docker-compose-prod.yml build
- name: Login to GHCR
if: success()
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Get the version
id: get_version
run: echo ::set-output name=VERSION::${GITHUB_REF/refs\/tags\//}
- name: Push on GHCR
run: |
./cd/scripts/docker-push.sh ${{ steps.get_version.outputs.VERSION }}
./cd/scripts/docker-push.sh latest
./cd/scripts/docker-push.sh ${{ steps.get_version.outputs.VERSION }} ${{ steps.get_version.outputs.VERSION }}
./cd/scripts/docker-push.sh ${{ steps.get_version.outputs.VERSION }} latest
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ SHELL := /bin/bash
DOCKER := $(shell type -p docker)
DC := $(shell type -p docker-compose)
TAG := 1.2
APP_NAME := basegun
REG := ghcr.io
ORG := datalab-mi
APP_NAME := basegun
REG := ghcr.io
ORG := datalab-mi


export
Expand Down
2 changes: 1 addition & 1 deletion backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ uvicorn src.main:app --reload --host 0.0.0.0 --port 5000

### With Docker
```bash
docker run --rm -d -p 5000:5000 --name basegun_back basegun-back:dev -e PATH_IMGS=/tmp/basegun/
docker run --rm -d -p 5000:5000 --name basegun_back -e VERSION=1.2 basegun-back:dev
```
Remember afterwards to stop container `docker stop basegun_back`

Expand Down
8 changes: 8 additions & 0 deletions backend/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ def home():
return "Basegun backend"


@app.get("/version", response_class=PlainTextResponse)
def version():
if "VERSION" in os.environ:
return os.environ["VERSION"]
else:
return "-1.0"


@app.post("/upload")
async def imageupload(image: UploadFile = File(...)):
if model:
Expand Down
7 changes: 7 additions & 0 deletions backend/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ def test_home(self):
r = requests.get(self.url)
self.assertEqual(r.text, "Basegun backend")

def test_version(self):
"""Checks that the route /version sends a version"""
self.assertTrue("VERSION" in os.environ)
r = requests.get(self.url + '/version')
self.assertEqual(r.text, os.environ["VERSION"])
self.assertEqual(len(r.text.split('.')), 2) # checks version has format X.Y

def test_upload(self):
"""Checks that the file upload works properly"""
path = os.path.join(
Expand Down
8 changes: 4 additions & 4 deletions cd/scripts/docker-push.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
docker tag basegun-frontend:action-prod ghcr.io/datalab-mi/basegun/basegun-frontend:$1
docker tag basegun-backend:action-prod ghcr.io/datalab-mi/basegun/basegun-backend:$1
docker push ghcr.io/datalab-mi/basegun/basegun-frontend:$1
docker push ghcr.io/datalab-mi/basegun/basegun-backend:$1
docker tag basegun-frontend:$1-prod ghcr.io/datalab-mi/basegun/basegun-frontend:$2
docker tag basegun-backend:$1-prod ghcr.io/datalab-mi/basegun/basegun-backend:$2
docker push ghcr.io/datalab-mi/basegun/basegun-frontend:$2
docker push ghcr.io/datalab-mi/basegun/basegun-backend:$2
1 change: 1 addition & 0 deletions docker-compose-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ services:
container_name: basegun-backend
environment:
- PATH_IMGS=/app/images
- VERSION=${TAG}
image: basegun-backend:${TAG}-dev
ports:
- 5000:5000
Expand Down
1 change: 1 addition & 0 deletions docker-compose-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ services:
container_name: basegun-backend
environment:
- PATH_IMGS=/app/images
- VERSION=${TAG}
image: basegun-backend:${TAG}-prod
ports:
- 5000:5000
Expand Down

0 comments on commit 92673b2

Please sign in to comment.