[Workflow] Auto deploy system with Docker and Github Actions - sparc-software-hust/fmecg-web-server GitHub Wiki
Việc deploy trở nên cần thiết khi dự án đến giai đoạn kiểm thử và bắt đầu có người sử dụng. Chúng ta có thể deploy lên server bằng cách thủ công như sau:
- ssh vào server, tải repo lên server bằng git hoặc copy tay
- chuẩn bị các file env cho server
- khởi động database
- run lần lượt từng folder (back-end, front-end, bin)
Lặp lại các bước với mỗi lần deploy.
Với việc đẩy code lên hàng ngày, team muốn code sẽ được deploy hàng ngày lên server để hôm sau mọi người có thể test được chức năng mới. Với flow deploy thủ công đã có tại đây, chỉ cần team áp dụng thêm Docker để đóng gói các services, Github Actions để thực hiện quá trình tự động hoá việc build, deploy là đã có thể tạo 1 flow deploy hàng ngày.
Chuẩn bị
- 1 server (lấy sẵn ssh key hoặc username/password)
- Kiến thức cơ bản về Github Actions, Docker
Workflow
1. Giải thích workflow
- Bước 1: Sử dụng
webhook
github, bắt các event merge pull vàodevelopment
trong repo. - Bước 2: Tạo 1 services trong server (có thể gộp những services đang chạy) để lưu số lượng pull được merge vào
development
và schedule build Docker vào mỗi 0h27p hàng ngày. Ví dụ hôm đó team 3 có pull được merge, hàm schedule build sẽ được trigger, nếu không có pull được merge thì sẽ không build. - Bước 3: Dựa trên flow trên viết jobs cho
Github Actions
, file.yaml
được lưu trong./github/workflows
. - Bước 4: Tận hưởng thành quả.
2. File workflow hoàn chỉnh cho Github Actions
name: Automatic deploy server
on: workflow_dispatch
jobs:
docker-build:
runs-on: ubuntu-latest
strategy:
fail-fast: true
# liệt kê các services Docker cần build
matrix:
include:
- dockerfile: ./server-web/Dockerfile
image: thaidmfinnick/fmecg-node-app
context: ./server-web
- dockerfile: ./frontend_web/Dockerfile
image: thaidmfinnick/fmecg-react-app
context: ./frontend_web
- dockerfile: ./frontend_web/bin/Dockerfile
image: thaidmfinnick/fmecg-bin-login
context: ./frontend_web/bin
- dockerfile: ./proxy/Dockerfile
image: thaidmfinnick/fmecg-proxy
context: ./proxy
- dockerfile: ./server_chat/Dockerfile
image: thaidmfinnick/fmecg-chat-app
context: ./server_chat
name: Docker Build ${{ matrix.image }}
permissions:
contents: read
packages: write
# từ các services đã liệt kê ở trên lần lượt build image -> đẩy lên Docker hub
steps:
- name: Checkout Github repo
uses: actions/checkout@v4
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to DockerHub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: ${{ matrix.context }}
file: ${{ matrix.dockerfile }}
push: true
tags: ${{ matrix.image }}:latest
# nếu flow build ở trên thành công -> thực hiện ssh vào server
# -> tải các services vừa được up lên và run
run-docker-images:
needs: docker-build
runs-on: ubuntu-latest
steps:
- name: Execute ssh commands
uses: appleboy/[email protected]
with:
host: ${{ secrets.SERVER_HOST }}
username: ${{ secrets.SERVER_USERNAME }}
password: ${{ secrets.SERVER_PASSWORD }}
script: |
cd /home/finnick/fmECG_docker
git pull origin development
make app