谷粒商城部署推送镜像(五十一)
383-390 流水线部署
问题:在代码扫描的时候,会有报错,无法识别kubesphere内嵌的snoarqube,但是改成我主机安装的发现又没什么问题,但是最后会发送回调请求判断这个阶段有没有成功,所以也有问题,所以先跳过代码扫描。
正在成功的时候如下图:
修改后的Jenkins文件:
pipeline {
agent {
node {
label 'maven'
}
}
environment {
DOCKER_CREDENTIAL_ID = 'dockerhub-id'
GITHUB_CREDENTIAL_ID = 'gitee-id'
KUBECONFIG_CREDENTIAL_ID = 'demo-kubeconfig'
REGISTRY = 'docker.io'
DOCKERHUB_NAMESPACE = 'dalianpai'
GITHUB_ACCOUNT = 'dalianpai'
SONAR_CREDENTIAL_ID = 'sonar-qube'
BRANCH_NAME = 'prod'
}
stages {
stage('拉取代码') {
steps {
git(url: 'https://gitee.com/dalianpai/gulimall.git', credentialsId: 'gitee-id', branch: 'prod', changelog: true, poll: false)
sh 'echo 正在构建 $PROJECT_NAME 版本号:$PROJECT_VERSION'
container ('maven') {
sh "mvn clean install -Dmaven.test.skip=true -gs `pwd`/mvn-settings.xml"
}
}
}
stage ('构建镜像-推送镜像') {
steps {
container ('maven') {
sh 'mvn -Dmaven.test.skip=true -gs `pwd`/mvn-settings.xml clean package'
sh 'cd $PROJECT_NAME && docker build -f Dockerfile -t $REGISTRY/$DOCKERHUB_NAMESPACE/$PROJECT_NAME:SNAPSHOT-$BRANCH_NAME-$BUILD_NUMBER .'
withCredentials([usernamePassword(passwordVariable : 'DOCKER_PASSWORD' ,usernameVariable : 'DOCKER_USERNAME' ,credentialsId : "$DOCKER_CREDENTIAL_ID" ,)]) {
sh 'echo "$DOCKER_PASSWORD" | docker login $REGISTRY -u "$DOCKER_USERNAME" --password-stdin'
sh 'docker tag $REGISTRY/$DOCKERHUB_NAMESPACE/$PROJECT_NAME:SNAPSHOT-$BRANCH_NAME-$BUILD_NUMBER $REGISTRY/$DOCKERHUB_NAMESPACE/$PROJECT_NAME:latest '
sh 'docker push $REGISTRY/$DOCKERHUB_NAMESPACE/$PROJECT_NAME:latest '
}
}
}
}
stage('部署到k8s') {
steps {
input(id: "deploy-to-dev-$PROJECT_NAME", message: "是否将 $PROJECT_NAME 部署到集群中?")
kubernetesDeploy(configs: "$PROJECT_NAME/deploy/**", enableConfigSubstitution: true, kubeconfigId: "$KUBECONFIG_CREDENTIAL_ID")
}
}
stage('发布版本'){
when{
expression{
return params.PROJECT_VERSION =~ /v.*/
}
}
steps {
container ('maven') {
input(id: 'release-image-with-tag', message: '发布当前版本镜像吗?')
withCredentials([usernamePassword(credentialsId: "$GITHUB_CREDENTIAL_ID", passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME')]) {
sh 'git config --global user.email "dalianpai@163.com" '
sh 'git config --global user.name "dalianpai" '
sh 'git tag -a $PROJECT_VERSION -m "$PROJECT_VERSION" '
sh 'git push http://$GIT_USERNAME:$GIT_PASSWORD@gitee.com/$GITHUB_ACCOUNT/gulimall.git --tags --ipv4'
}
sh 'docker tag $REGISTRY/$DOCKERHUB_NAMESPACE/$PROJECT_NAME:SNAPSHOT-$BRANCH_NAME-$BUILD_NUMBER $REGISTRY/$DOCKERHUB_NAMESPACE/$PROJECT_NAME:$PROJECT_VERSION '
sh 'docker push $REGISTRY/$DOCKERHUB_NAMESPACE/$PROJECT_NAME:$PROJECT_VERSION '
}
}
}
}
parameters {
string(name: 'PROJECT_VERSION', defaultValue: 'v0.0Beta', description: '')
string(name: 'PROJECT_NAME', defaultValue: '', description: '')
}
}
修改后的k8s资源清单
kind: Deployment
apiVersion: apps/v1
metadata:
name: gulimall-gateway
namespace: gulimall
labels:
app: gulimall-gateway
spec:
replicas: 1
selector:
matchLabels:
app: gulimall-gateway
template:
metadata:
labels:
app: gulimall-gateway
spec:
containers:
- name: gulimall-gateway
image: $REGISTRY/$DOCKERHUB_NAMESPACE/$PROJECT_NAME:latest
ports:
- containerPort: 8080
protocol: TCP
resources:
limits:
cpu: 1000m
memory: 1000Mi
requests:
cpu: 10m
memory: 10Mi
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
imagePullPolicy: IfNotPresent
restartPolicy: Always
terminationGracePeriodSeconds: 30
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 25%
maxSurge: 25%
revisionHistoryLimit: 10
progressDeadlineSeconds: 600
---
kind: Service
apiVersion: v1
metadata:
name: gulimall-gateway
namespace: gulimall
labels:
app: gulimall-gateway
spec:
ports:
- name: http
protocol: TCP
port: 8080
targetPort: 8080
nodePort: 31004
selector:
app: gulimall-gateway
type: NodePort
sessionAffinity: None
修改后的Dockerfile,不然会报OOM
FROM java:8
EXPOSE 8080
VOLUME /tmp
ADD target/*.jar /app.jar
RUN bash -c 'touch /app.jar'
ENTRYPOINT ["java","-jar","-Xms256m","-Xmx700m","/app.jar","--spring.profiles.active=prod"]