Skip to content

Commit 7c3f376

Browse files
committed
chore: docker & kubernetes ci/cd
1 parent 78e6258 commit 7c3f376

File tree

8 files changed

+172
-3
lines changed

8 files changed

+172
-3
lines changed

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,29 @@
1+
[![Code Scanning - Action](https://github.com/ka1i/golang-cli-app/actions/workflows/codeScan.yml/badge.svg)](https://github.com/ka1i/golang-cli-app/actions/workflows/codeScan.yml)
2+
13
# golang cli app
24

35
## Build
4-
```
6+
```bash
57
make
68
```
79

810
## Usage
911
```bash
1012
./bin/app-cli -h
13+
```
14+
15+
## Cloud build & deploy
16+
```bash
17+
PrivateRegistry=<private registry>
18+
19+
# Docker Build
20+
docker build --progress=plain -f build/local/Dockerfile -t ${PrivateRegistry}/gca/cli:latest .
21+
22+
# Docker Deploy
23+
docker stack deploy -c deployment/docker/docker-compose.yml gca
24+
25+
# Kubernetes Deploy
26+
kubectl create configmap gca-configs --from-file configs.yaml -n app # 请提前编辑好配置文件
27+
./deployment/kubernetes/autogen.sh 1 ${PrivateRegistry} # 参数1:指定副本集数量。 参数2:指定私有镜像仓库地址
28+
kubectl apply -f deployment/kubernetes/cli-app.yaml -n app # 开始部署
1129
```

build/docker/Dockerfile

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
FROM golang:1.20 AS builder
2+
3+
ENV GOPROXY https://proxy.golang.com.cn,direct
4+
5+
WORKDIR /golang-cli-app/
6+
7+
COPY . .
8+
9+
RUN go mod tidy
10+
RUN make build
11+
12+
# final runner image
13+
FROM alpine:latest as runner
14+
15+
LABEL maintaner="Mardan M"
16+
17+
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories \
18+
&& apk add tzdata
19+
20+
WORKDIR /opt
21+
22+
COPY --from=builder /golang-cli-app/bin/app-cli /opt/app-cli
23+
24+
EXPOSE 2333
25+
26+
ENTRYPOINT ["/opt/app-cli"]

build/local/Dockerfile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# final runner image
2+
FROM alpine:latest as runner
3+
4+
LABEL maintaner="Mardan M"
5+
6+
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories \
7+
&& apk add tzdata
8+
9+
WORKDIR /opt
10+
11+
COPY ./bin/app-cli /opt/app-cli
12+
13+
EXPOSE 2333
14+
15+
ENTRYPOINT ["/opt/app-cli"]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
version: '3.9'
2+
3+
services:
4+
cli:
5+
image: gca/cli:latest
6+
env_file:
7+
- gca.env
8+
volumes:
9+
- /etc/gca/app.yaml:/opt/app.yaml:ro
10+
ports:
11+
- "10086:10086"

deployment/docker/gca.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
GCA_APP_PORT=10086
2+
GCA_APP_MODE=debug

deployment/kubernetes/autogen.sh

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#!/bin/bash --posix
2+
3+
replicas=$1
4+
repository=$2
5+
6+
replicas=${replicas:=1}
7+
repository=${repository:=empty.repository}
8+
9+
cat > ./deployment/kubernetes/cli-app.yaml << EOF
10+
kind: ConfigMap
11+
apiVersion: v1
12+
metadata:
13+
name: gca-environment
14+
data:
15+
GCA_APP_MODE: "debug"
16+
GCA_APP_PORT: "10086"
17+
18+
---
19+
apiVersion: v1
20+
kind: PersistentVolumeClaim
21+
metadata:
22+
name: gca-data
23+
spec:
24+
accessModes:
25+
- ReadWriteOnce
26+
volumeMode: Filesystem
27+
resources:
28+
requests:
29+
storage: 32Gi
30+
31+
---
32+
kind: Deployment
33+
apiVersion: apps/v1
34+
metadata:
35+
name: gca-cli
36+
labels:
37+
app: gca-cli
38+
39+
spec:
40+
replicas: ${replicas}
41+
selector:
42+
matchLabels:
43+
app: gca-cli
44+
template:
45+
metadata:
46+
labels:
47+
app: gca-cli
48+
spec:
49+
containers:
50+
- name: gca-cli
51+
image: ${repository}/gca/cli:latest
52+
ports:
53+
- name: web
54+
containerPort: 10086
55+
imagePullPolicy: Always
56+
envFrom:
57+
- configMapRef:
58+
name: gca-environment
59+
volumeMounts:
60+
- name: configs
61+
readOnly: true
62+
subPath: "app.yaml"
63+
mountPath: "/opt/configs/app.yaml"
64+
- name: data
65+
mountPath: "/opt/share"
66+
readinessProbe:
67+
tcpSocket:
68+
port: 8080
69+
timeoutSeconds: 10
70+
periodSeconds: 10
71+
successThreshold: 1
72+
failureThreshold: 3
73+
volumes:
74+
- name: data
75+
persistentVolumeClaim:
76+
claimName: gca-data
77+
- name: configs
78+
configMap:
79+
name: gca-configs
80+
items:
81+
- key: "app.yaml"
82+
path: "app.yaml"
83+
84+
---
85+
apiVersion: v1
86+
kind: Service
87+
metadata:
88+
name: gca-cli
89+
spec:
90+
ports:
91+
- name: http
92+
port: 10086
93+
nodePort: 30801
94+
selector:
95+
app: gca-cli
96+
type: NodePort
97+
EOF

internal/pkg/system/prepare/environment.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func app() {
2222

2323
func appMode() {
2424
var mode string
25-
mode = os.Getenv("AOK_APP_MODE")
25+
mode = os.Getenv("GCA_APP_MODE")
2626
if len(mode) == 0 {
2727
mode = gin.TestMode
2828
}

pkg/utils/utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func Resolver() string {
2323
addr = append(addr, "80")
2424
}
2525

26-
port := os.Getenv("AOK_APP_PORT")
26+
port := os.Getenv("GCA_APP_PORT")
2727
if len(port) != 0 {
2828
addr[1] = port
2929
}

0 commit comments

Comments
 (0)