- kubeconfig类型:默认内容为当前用户的 kubeconfig 配置。 选择此类型创建默认有值
- 用户名密码(my-login):git账号和密码
- 用户名密码(my-aliyun-docker):阿里云容器镜像仓库账号密码
pipeline {
agent {
node {
label 'maven'
}
}
stages {
stage('拉取') {
agent none
steps {
git(url: 'https://gitee.com/wy521a/dream-yard-admin.git', credentialsId: 'my-login', changelog: true, poll: false, branch: 'master')
sh 'ls'
}
}
stage('编译') {
agent none
steps {
container('maven') {
sh 'ls'
sh 'mvn clean package -Dmaven.test.skip=true'
sh 'ls ruoyi-admin/target/'
}
}
}
stage('打包') {
agent none
steps {
container('maven') {
sh 'ls ruoyi-admin/target/'
sh 'docker build -t dream-yard-admin:latest .'
}
}
}
stage('推送') {
agent none
steps {
container('maven') {
withCredentials([usernamePassword(credentialsId : 'my-aliyun-docker' ,passwordVariable : 'DOCKER_USER' ,usernameVariable : 'DOCKER_PASSWORD' ,)]) {
sh 'echo 镜像库密码 | docker login 镜像库地址 -u 镜像库账号 -p 镜像库密码'
sh 'docker tag dream-yard-admin:latest 镜像库地址/dream-yard/dream-yard-admin:$BUILD_NUMBER'
sh 'docker push 镜像库地址/dream-yard/dream-yard-admin:$BUILD_NUMBER'
}
}
}
}
stage('部署') {
agent none
steps {
container('maven') {
withCredentials([
kubeconfigFile(
credentialsId: 'kubeconfig',
variable: 'KUBECONFIG')
]) {
sh 'envsubst < ruoyi-admin/deploy/deploy.yaml | kubectl apply -f -'
}
}
}
}
}
}
- 粘贴以上Jenkinsfile文件,点编辑流水线
- 拉取
- Url:git地址
- 凭证名称:选择我们创建的my-login名称的凭证,就是我们配置的git登录的账号密码
- 分支:就是拉取哪个分支
- 编译
- 这里需要指定容器运行shell命令,否则会报错
- 第二个shell脚本就是我们编译的maven命令
- 同样也需要指定容器
- 第二个shell脚本就是我们打镜像的命令
FROM openjdk:8-jdk
MAINTAINER 作者
ENV TZ=Asia/Shanghai
ENV JAVA_OPTS="-Xms128m -Xmx256m -Djava.security.egd=file:/dev/./urandom"
RUN ln -sf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN mkdir -p /ruoyi-admin
WORKDIR /ruoyi-admin
EXPOSE 8080
ADD ./ruoyi-admin/target/ruoyi-admin.jar ./
CMD sleep 5;java $JAVA_OPTS -jar ruoyi-admin.jar
- 推送
- 这里需要指定容器——>添加凭证
- 声明:我这里使用凭证的账号密码变量不好使,所以就写死了
- 第一个shell脚本是登录阿里云容器镜像仓库
- 第二个shell脚本是打镜像命令
- 第三个是将我们打的镜像推送到阿里云镜像仓库
- 部署
- 添加容器——>凭证,这个凭证是我们创建当前用户kubeconfig凭证
- shell脚本命令就是执行部署的命令,不要使用kubernetesDeploy部署,这个到后面会停止使用、
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: dream-yard-admin
name: dream-yard-admin
namespace: dream-yard # 正常创建的项目空间名称
spec:
progressDeadlineSeconds: 600
replicas: 1
strategy:
type: RollingUpdate # Recreate:在创建新Pods之前,所有现有的Pods会被杀死 RollingUpdate:滚动升级,逐步替换的策略,同时滚动升级时,支持更多的附加参数
rollingUpdate:
maxSurge: 1 #maxSurge:1 表示滚动升级时会先启动1个pod
maxUnavailable: 1 #maxUnavailable:1 表示滚动升级时允许的最大Unavailable的pod个数,也可以填写比例,maxUnavailable=50%
selector:
matchLabels:
app: dream-yard-admin
template:
metadata:
labels:
app: dream-yard-admin
spec:
containers:
- env:
- name: CACHE_IGNORE
value: js|html
- name: CACHE_PUBLIC_EXPIRATION
value: 3d
image: registry.cn-hangzhou.aliyuncs.com/dream-yard/dream-yard-admin:$BUILD_NUMBER
readinessProbe:
httpGet:
path: /
port: 8080
timeoutSeconds: 10
failureThreshold: 30
periodSeconds: 5
imagePullPolicy: Always
name: dream-yard-admin
ports:
- containerPort: 8080
protocol: TCP
resources:
limits:
cpu: 300m
memory: 600Mi
requests:
cpu: 100m
memory: 100Mi
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Always
terminationGracePeriodSeconds: 30
---
apiVersion: v1
kind: Service
metadata:
labels:
app: dream-yard-admin-service
name: dream-yard-admin-service
namespace: dream-yard # 正常创建的项目空间名称
spec:
ports:
- name: http
port: 8080
protocol: TCP
targetPort: 8080
selector:
app: dream-yard-admin
sessionAffinity: None
type: NodePort
点个爱心和关注吧