From 79fc8cff1016bfa51f6e310598d5522be1c84b4c Mon Sep 17 00:00:00 2001 From: Zpekii Date: Mon, 5 Jan 2026 13:07:56 +0800 Subject: [PATCH] feat: add Docker build and deployment workflow --- .gitea/workflows/deploy.yaml | 16 +++++++++++++++ .gitignore | 4 +++- Dockerfile | 7 +++++++ deployment.yaml | 38 ++++++++++++++++++++++++++++++++++++ main.go | 7 +++++++ 5 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 Dockerfile create mode 100644 deployment.yaml diff --git a/.gitea/workflows/deploy.yaml b/.gitea/workflows/deploy.yaml index f9b8465..f88c0d7 100644 --- a/.gitea/workflows/deploy.yaml +++ b/.gitea/workflows/deploy.yaml @@ -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 }}." \ No newline at end of file diff --git a/.gitignore b/.gitignore index 8ff13dd..5dc5727 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ *.exe -*.prof \ No newline at end of file +*.prof +certs/* +helloapp \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..2a54f7a --- /dev/null +++ b/Dockerfile @@ -0,0 +1,7 @@ +FROM ubuntu:ci + +RUN mkdir -p /var/deploy + +COPY helloapp /var/deploy/helloapp + +CMD ["/var/deploy/helloapp"] \ No newline at end of file diff --git a/deployment.yaml b/deployment.yaml new file mode 100644 index 0000000..4cc937d --- /dev/null +++ b/deployment.yaml @@ -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 \ No newline at end of file diff --git a/main.go b/main.go index 6593ee0..5096c7d 100644 --- a/main.go +++ b/main.go @@ -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)