Deploying a Hexo Blog on Kubernetes with GitHub Actions

In this post, we’ll cover the steps to deploy a Hexo blog on Kubernetes using GitHub Actions. This setup automates the build, containerization, and deployment of the blog to your Kubernetes cluster.

Prerequisites

  1. Hexo Installed Locally: Ensure Hexo is installed and your blog is set up.
  2. Kubernetes Cluster: A running Kubernetes cluster with kubectl access.
  3. Docker Registry: A registry to store your container images.
  4. GitHub Repository: Store your Hexo blog project in a GitHub repository.
  5. cert-manager: Installed in your Kubernetes cluster for TLS certificates.

Step 1: Containerize Your Hexo Blog

  1. Generate Hexo Static Files:

    1
    hexo generate
  2. Create a Dockerfile:

    1
    2
    3
    FROM nginx:alpine
    COPY public /usr/share/nginx/html
    EXPOSE 80
  3. Build and Push the Docker Image:

    1
    2
    docker build -t <registry>/hexo-blog:latest .
    docker push <registry>/hexo-blog:latest

Step 2: Kubernetes Configuration

Deployment YAML

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
apiVersion: apps/v1
kind: Deployment
metadata:
name: hexo-blog
namespace: <namespace>
labels:
app: hexo-blog
spec:
replicas: 2
selector:
matchLabels:
app: hexo-blog
template:
metadata:
labels:
app: hexo-blog
spec:
containers:
- name: hexo-blog
image: <registry>/hexo-blog:latest
imagePullPolicy: Always
ports:
- containerPort: 80
imagePullSecrets:
- name: regcred

Service YAML

1
2
3
4
5
6
7
8
9
10
11
12
13
apiVersion: v1
kind: Service
metadata:
name: hexo-blog
namespace: <namespace>
spec:
selector:
app: hexo-blog
ports:
- protocol: TCP
port: 80
targetPort: 80
type: ClusterIP

Ingress YAML

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: hexo-blog-ingress
namespace: <namespace>
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
cert-manager.io/cluster-issuer: "letsencrypt-prod"
spec:
ingressClassName: nginx
rules:
- host: <your-domain>
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: hexo-blog
port:
number: 80
tls:
- hosts:
- <your-domain>
secretName: hexo-blog-tls

Step 3: GitHub Actions Workflow

Create a .github/workflows/deploy.yml file in your repository:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
name: Deploy Hexo Blog

on:
push:
branches:
- main

jobs:
build-and-deploy:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '18'

- name: Install Hexo
run: |
npm install
npm install -g hexo-cli
hexo generate

- name: Build Docker image
run: |
docker build -t <registry>/hexo-blog:latest .
echo "${{ secrets.REGISTRY_PASSWORD }}" | docker login <registry> -u "${{ secrets.REGISTRY_USERNAME }}" --password-stdin
docker push <registry>/hexo-blog:latest

- name: Deploy to Kubernetes
env:
KUBECONFIG: ${{ secrets.KUBECONFIG }}
run: |
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
kubectl apply -f ingress.yaml
kubectl rollout restart deployment hexo-blog -n <namespace>

Step 4: Verify the Deployment

  1. Check the Pods:

    1
    kubectl get pods -n <namespace>
  2. Access the Blog:
    Navigate to https://<your-domain> in your browser.


Conclusion

By automating the build and deployment of your Hexo blog using GitHub Actions, you save time and ensure consistency in your releases. This setup is scalable and can be extended with monitoring and logging for production environments.

Let me know if you have any questions or need further assistance!