java项目

微服务

安装插件

Maven Integration

Dockerfile

FROM openjdk:8-jre-slim
MAINTAINER yanyong
ARG ACTIVE
ENV JAVA_OPT="-Xms1g -Xmx1g"
ENV JAVA_ARG="--spring.profiles.active=$ACTIVE"
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
  && echo 'Asia/Shanghai' > /etc/timezone
ADD target/*.jar /opt/app.jar
CMD ["sh","-c","exec java $JAVA_OPT -jar /opt/app.jar $JAVA_ARG"]

deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: megaordermiddleware-basic-gateway
spec:
  selector:
    matchLabels:
      app: megaordermiddleware-basic-gateway
  replicas: 2
  template:
    metadata:
      labels:
        app: megaordermiddleware-basic-gateway
    spec:
      containers:
      - name: megaordermiddleware-basic-gateway
        image: harbor.mkl.io/oms/megaordermiddleware-basic-gateway:1.0-$BUILD_NUMBER
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 7777
      imagePullSecrets:
      - name: secret-harbor-user

---

apiVersion: v1
kind: Service
metadata:
  name: megaordermiddleware-basic-gateway
spec:
  type: NodePort
  selector:
    app: megaordermiddleware-basic-gateway
  ports:
    - port: 7777
      targetPort: 7777

多环境区分,src/main/resources/bootstrap.yml

nacos配置中心增加命名空间:yanyong

spring:
  profiles: yanyong
  cloud:
    nacos:
      discovery:
        server-addr: nacos.mkl.io
        namespace: yanyong
      config:
        server-addr: nacos.mkl.io
        file-extension: yml
        namespace: yanyong

jenkins, maven打包

clean package -pl megaordermiddleware-basic/megaordermiddleware-basic-gateway -am -Dmaven.test.skip=true

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


_image="harbor.mkl.io/oms/megaordermiddleware-basic-gateway:1.0-$BUILD_NUMBER"
_image_latest="${_image%-*}"-latest
_workspace="megaordermiddleware-basic/megaordermiddleware-basic-gateway"  #默认当前目录
_dockerfile="${_workspace}/Dockerfile-test"
_harboruser="$harboruser"
_harborpass="$harborpass"
ACTIVE=yanyong

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} --build-arg ACTIVE=$ACTIVE ${_workspace:-.}  #ACTIVE值传入Dockerfile
    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设置

#Source files
megaordermiddleware-basic/megaordermiddleware-basic-gateway/deployment-test.yaml

#Remove prefix
megaordermiddleware-basic/megaordermiddleware-basic-gateway

#Remote directory
DEPLOY/$JOB_BASE_NAME

#Exec command
export BUILD_NUMBER=$BUILD_NUMBER
envsubst < DEPLOY/$JOB_BASE_NAME/deployment-test.yaml | kubectl apply -f -

前端vue

Dockerfile

FROM node:14.17.3 as nodebuilder
WORKDIR /Node
COPY . .
#RUN apt update && apt-get -y install yarn && yarn && yarn build:prod
#RUN npm install && npm run build:prod
#RUN npm install --registry=https://registry.npm.taobao.org && npm run build:prod
#RUN npm install --registry=https://mirrors.huaweicloud.com/repository/npm/ && npm run build:prod

RUN git config --global url."https://".insteadOf git:// \
  && cp .env.test .env.production \
  && apt update && apt-get -y install yarn \
  && yarn && yarn build:prod

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

nmp install安装依赖时报错:

ERR! /usr/bin/git ls-remote -h -t git://github.com/adobe-webplatform/eve.git

解决办法:

1
git config --global url."https://".insteadOf git://

deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: megaordermiddleware-management-ui-vue
spec:
  selector:
    matchLabels:
      app: megaordermiddleware-management-ui-vue
  replicas: 2
  template:
    metadata:
      labels:
        app: megaordermiddleware-management-ui-vue
    spec:
      containers:
      - name: megaordermiddleware-management-ui-vue
        image: harbor.mkl.io/oms/megaordermiddleware-management-ui-vue:1.0-$BUILD_NUMBER
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
      imagePullSecrets:
      - name: secret-harbor-user


---

apiVersion: v1
kind: Service
metadata:
  name: megaordermiddleware-management-ui-vue
spec:
  type: NodePort
  selector:
    app: megaordermiddleware-management-ui-vue
  ports:
    - port: 80
      targetPort: 80

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/oms/megaordermiddleware-management-ui-vue:1.0-$BUILD_NUMBER"
_image_latest="${_image%-*}"-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 -

https://docs.docker.com/engine/reference/builder/