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
- Hexo Installed Locally: Ensure Hexo is installed and your blog is set up.
- Kubernetes Cluster: A running Kubernetes cluster with
kubectlaccess. - Docker Registry: A registry to store your container images.
- GitHub Repository: Store your Hexo blog project in a GitHub repository.
- cert-manager: Installed in your Kubernetes cluster for TLS certificates.
Step 1: Containerize Your Hexo Blog
-
Generate Hexo Static Files:
hexo generate -
Create a Dockerfile:
FROM nginx:alpine COPY public /usr/share/nginx/html EXPOSE 80 -
Build and Push the Docker Image:
docker build -t <registry>/hexo-blog:latest . docker push <registry>/hexo-blog:latest
Step 2: Kubernetes Configuration
Deployment YAML
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: regcredService YAML
apiVersion: v1
kind: Service
metadata:
name: hexo-blog
namespace: <namespace>
spec:
selector:
app: hexo-blog
ports:
- protocol: TCP
port: 80
targetPort: 80
type: ClusterIPIngress YAML
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-tlsStep 3: GitHub Actions Workflow
Create a .github/workflows/deploy.yml file in your repository:
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
-
Check the Pods:
kubectl get pods -n <namespace> -
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!