go语言项目

后端API接口

beego框架

Dockerfile

FROM golang:1.16-alpine as builder
WORKDIR /work

ENV GO111MODULE=on \
    GOPROXY=https://goproxy.cn,direct

COPY . .

RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o app . \
  && mkdir run && cp -r conf app run && cp conf/app-test.conf run/conf/app.conf

#FROM scratch
FROM alpine
RUN apk add --no-cache ca-certificates tzdata \
  && ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
  && echo 'Asia/Shanghai' > /etc/timezone
WORKDIR /work

COPY --from=builder /work/run .

EXPOSE 1500
ENTRYPOINT ["./app"]

创建pvc

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
cat << EOF | kubectl apply -f -
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: enjoydream-pvc-static
spec:
  storageClassName: nfs-client
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 10Gi
EOF

创建secret

1
kubectl create secret docker-registry secret-harbor-user --docker-server=harbor.mkl.io --docker-username=user --docker-password=123456

deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: enjoy-dream-api
spec:
  selector:
    matchLabels:
      app: enjoy-dream-api
  replicas: 2
  template:
    metadata:
      labels:
        app: enjoy-dream-api
    spec:
      terminationGracePeriodSeconds: 30
      containers:
        - name: enjoy-dream-api
          image: harbor.mkl.io/enjoydream/enjoy-dream-api:3.0-$BUILD_NUMBER
          imagePullPolicy: IfNotPresent
          volumeMounts:
            - name: pvc-static
              subPath: static
              mountPath: "/work/static"
          ports:
            - containerPort: 1500
      imagePullSecrets:
        - name: secret-harbor-user
      volumes:
        - name: pvc-static
          persistentVolumeClaim:
            claimName: enjoydream-pvc-static

---

apiVersion: v1
kind: Service
metadata:
  name: enjoy-dream-api
spec:
  type: NodePort
  selector:
    app: enjoy-dream-api
  ports:
    - port: 1500
      targetPort: 1500

前端vue

Dockerfile

FROM node:14.17.3 as nodebuilder
WORKDIR /Node
COPY . .
RUN apt update && apt-get -y install yarn && cp .env.test .env && yarn && yarn build

FROM nginx:alpine
MAINTAINER yanyong
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
  && echo 'Asia/Shanghai' > /etc/timezone
COPY --from=nodebuilder /Node/dist/ /usr/share/nginx/html/

deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: enjoy-dream-admin
spec:
  selector:
    matchLabels:
      app: enjoy-dream-admin
  replicas: 2
  template:
    metadata:
      labels:
        app: enjoy-dream-admin
    spec:
      terminationGracePeriodSeconds: 30
      containers:
        - name: enjoy-dream-admin
          image: harbor.mkl.io/enjoydream/enjoy-dream-admin:3.0-$BUILD_NUMBER
          imagePullPolicy: IfNotPresent
          ports:
          - containerPort: 80
      imagePullSecrets:
        - name: secret-harbor-user

---

apiVersion: v1
kind: Service
metadata:
  name: enjoy-dream-admin
spec:
  type: NodePort
  selector:
    app: enjoy-dream-admin
  ports:
    - port: 80
      targetPort: 80

jenkins发布

后端,前端都参考以下脚本

jenkins, Build脚本

 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
41
42
#!/bin/sh
set -ex


_image="harbor.mkl.io/enjoydream/enjoy-dream-api:3.0-$BUILD_NUMBER"
_image_latest='harbor.mkl.io/enjoydream/enjoy-dream-api:3.0-latest'
_dockerfile=Dockerfile-test
_workspace=""  #默认当前目录
_harboruser="$harboruser"
_harborpass="$harborpass"

checkimage(){
    local image_id=$(docker images -q --filter reference="${_image}")
    if [[ -n "$image_id" ]]; then docker rmi -f $image_id; fi  #删除镜像
}

build(){
    checkimage

    docker build -f ${_dockerfile} -t ${_image} ${_workspace:-.}
    docker tag ${_image} ${_image_latest}
    
    #build产生的中间镜像tag为none
    docker rmi $(docker images -f "dangling=true" -q) || :  #删除所有tag为none的镜像
}

push(){
    local repo=${_image%%/*}
    docker login -u ${_harboruser} -p ${_harborpass} ${repo}
    docker push ${_image}
    docker push ${_image_latest}
    docker logout
    
    checkimage
}

main(){
    build
    push
}

main

jenkins, Post-build Actions脚本

1
2
export BUILD_NUMBER=$BUILD_NUMBER
envsubst < DEPLOY/$JOB_BASE_NAME/deployment.yaml | kubectl apply -f -