feat: add Docker build and deployment workflow

This commit is contained in:
2026-01-05 13:07:56 +08:00
parent bb1bd53398
commit 79fc8cff10
5 changed files with 71 additions and 1 deletions

View File

@@ -34,7 +34,23 @@ jobs:
run: |
cd ${{ gitea.workspace }}
go test -cover ./...
- name: Go build
run: |
cd ${{ gitea.workspace }}
go build -ldflags "-X main.version=1.0.0-${{ gitea.sha }}" -o helloapp .
- name: Check Docker
run: |
docker --version
- name: Build Docker image
run: |
cd ${{ gitea.workspace }}
docker build -t localhost:8180/k8s-example/k8s-example:${{ gitea.sha }} .
- name: Push Docker image to local registry
run: |
docker push localhost:8180/k8s-example/k8s-example:${{ gitea.sha }}
- name: Deploy to Kubernetes
run: |
cd ${{ gitea.workspace }}
sed -i "s|localhost:8180/k8s-example/k8s-example:latest|localhost:8180/k8s-example/k8s-example:${{ gitea.sha }}|g" .gitea/workflows/deploy.yaml
kubectl apply -f .gitea/workflows/deploy.yaml
- run: echo "🍏 This job's status is ${{ job.status }}."

2
.gitignore vendored
View File

@@ -1,2 +1,4 @@
*.exe
*.prof
certs/*
helloapp

7
Dockerfile Normal file
View File

@@ -0,0 +1,7 @@
FROM ubuntu:ci
RUN mkdir -p /var/deploy
COPY helloapp /var/deploy/helloapp
CMD ["/var/deploy/helloapp"]

38
deployment.yaml Normal file
View File

@@ -0,0 +1,38 @@
apiVersion: v1
kind: Service
metadata:
name: k8s-example
spec:
selector:
app: k8s-example
ports:
- port: 8800
targetPort: 8800
type: LoadBalancer
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: k8s-example
spec:
selector:
matchLabels:
app: k8s-example
template:
metadata:
labels:
app: k8s-example
spec:
containers:
- name: k8s-example
image: localhost:8180/k8s-example/k8s-example:latest
imagePullPolicy: IfNotPresent
resources:
requests:
memory: "128Mi"
cpu: "500m"
limits:
memory: "128Mi"
cpu: "500m"
ports:
- containerPort: 8800

View File

@@ -1,12 +1,19 @@
package main
import (
"fmt"
"k8s-example/hello"
"net/http"
)
var (
version = "1.0.0-local"
)
func main() {
http.HandleFunc("/hello", hello.Handler)
fmt.Println("server on :8800, version:", version)
err := http.ListenAndServe(":8800", nil)
if err != nil {
panic(err)