Skip to content

Commit e1a6d6b

Browse files
committed
add github action
1 parent cdd5d01 commit e1a6d6b

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
name: Build and Deploy Classifier API to Production
2+
3+
# 1
4+
# Controls when the workflow will run
5+
on:
6+
# Triggers the workflow on push events but only for the master branch
7+
push:
8+
branches: [main]
9+
10+
# Allows you to run this workflow manually from the Actions tab
11+
workflow_dispatch:
12+
inputs:
13+
version:
14+
description: "Production"
15+
required: true
16+
#2
17+
env:
18+
IMAGE_NAME: "classifier_api"
19+
CONTAINER_NAME: "classifier_api_prod_ctr"
20+
21+
#3
22+
jobs:
23+
build_and_push:
24+
runs-on: ubuntu-latest
25+
steps:
26+
- name: Checkout the repo
27+
uses: actions/checkout@v2
28+
29+
- name: Log in to Docker Hub
30+
uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
31+
with:
32+
username: ${{ secrets.DOCKER_USERNAME }}
33+
password: ${{ secrets.DOCKER_PASSWORD }}
34+
35+
- name: Extract metadata (tags, labels) for Docker
36+
id: meta
37+
uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
38+
with:
39+
images: pawitrawarda/classifier_api
40+
41+
- name: Build and push Docker image
42+
uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc
43+
with:
44+
context: .
45+
push: true
46+
tags: pawitrawarda/classifier_api:latest
47+
labels: latest
48+
49+
deploy:
50+
runs-on: ubuntu-latest
51+
needs: build_and_push
52+
steps:
53+
- name: Deploy to VPS via SSH action
54+
uses: appleboy/ssh-action@master
55+
with:
56+
host: ${{ secrets.HOST }}
57+
username: ${{ secrets.USERNAME }}
58+
key: ${{ secrets.SSHKEY }}
59+
port: ${{ secrets.PORT }}
60+
passphrase: ${{ secrets.PASSPHRASE }}
61+
envs: IMAGE_NAME, CONTAINER_NAME
62+
script: |
63+
# Pull registry
64+
docker pull pawitrawarda/$(echo $IMAGE_NAME):latest
65+
# Stop running container
66+
docker stop $(echo $CONTAINER_NAME)
67+
# Remove old container
68+
docker rm $(echo $CONTAINER_NAME)
69+
# Run a new container from a new image
70+
docker run -d -p 8111:8123 --name $(echo $CONTAINER_NAME) pawitrawarda/$(echo $IMAGE_NAME):latest

dockerfile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,25 @@ COPY ./app ./app
1010
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8123"]
1111
# ---------- Script 1 ----------
1212

13+
# ---------- Script 1 with VENV ----------
14+
# FROM python:3.10-slim
15+
#
16+
# # Create a virtual environment
17+
# RUN python -m venv /venv
18+
# # Set the virtual environment path
19+
# ENV PATH="/venv/bin:$PATH"
20+
#
21+
# WORKDIR /app
22+
# COPY requirements.txt .
23+
# # Install dependencies in the virtual environment
24+
# RUN pip install --no-cache-dir -r requirements.txt
25+
#
26+
# COPY ./app ./app
27+
#
28+
# # Activate virtual environment and run the application
29+
# CMD ["/venv/bin/uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8123"]
30+
# ---------- Script 1 with VENV ----------
31+
1332
# ---------- Script 2 ----------
1433
# Stage 1: Build Environment
1534
# FROM python:3.10 as builder

0 commit comments

Comments
 (0)