pipeline {
agent {
node {
label 'maven'
}
}
stages {
stage('拉取项目') {
agent none
when {
environment name: 'BUILD_ENV', value: 'UAT_CICD'
}
steps {
container('maven') {
git(credentialsId: 'gitlab-id', branch: 'master', url: 'http://172.31.0.13:8888/linux86/RuoYi-Cloud.git', changelog: true, poll: false)
sh 'mvn clean install -Dmaven.test.skip=true'
}
}
}
stage('项目编译') {
agent none
when {
environment name: 'BUILD_ENV', value: 'UAT_CICD'
}
steps {
container('maven') {
sh 'mvn -Dmaven.test.skip=true clean package'
}
}
}
stage('镜像构建') {
agent none
when {
environment name: 'BUILD_ENV', value: 'UAT_CICD'
}
steps {
container('maven') {
script {
def environment = params.MODULE_NAME
if (environment == "ruoyi-auth" || environment == "ruoyi-gateway" || environment == "ruoyi-ui") {
sh 'cd $MODULE_NAME && docker build -t $REGISTRY/$HARBOR_NAMESPACE/$MODULE_NAME:$MODULE_VERSION -f Dockerfile .'
} else if (environment == "ruoyi-monitor") {
sh 'cd ruoyi-visual/$MODULE_NAME && docker build -t $REGISTRY/$HARBOR_NAMESPACE/$MODULE_NAME:$MODULE_VERSION -f Dockerfile .'
} else {
sh 'cd ruoyi-modules/$MODULE_NAME && docker build -t $REGISTRY/$HARBOR_NAMESPACE/$MODULE_NAME:$MODULE_VERSION -f Dockerfile .'
}
}
}
}
}
stage('镜像推送') {
agent none
when {
environment value: 'UAT_CICD', name: 'BUILD_ENV'
}
steps {
container('maven') {
withCredentials([usernamePassword(credentialsId : 'harbor-id' ,passwordVariable : 'DOCKER_PASSWORD' ,usernameVariable : 'DOCKER_USERNAME' ,)]) {
sh 'echo "$DOCKER_PASSWORD" | docker login $REGISTRY -u "$DOCKER_USERNAME" --password-stdin'
sh 'docker push $REGISTRY/$HARBOR_NAMESPACE/$MODULE_NAME:$MODULE_VERSION'
}
}
}
}
stage('部署到UAT环境') {
agent none
steps {
container('maven') {
withCredentials([kubeconfigFile(credentialsId: "$KUBECONFIG_CREDENTIAL_ID", variable: 'KUBECONFIG')]) {
script {
sh "echo \'${DEPLOY_YAML}\' > deploy.yaml"
sh "cat deploy.yaml"
sh "envsubst < deploy.yaml | kubectl apply -f -"
}
}
}
}
}
}
environment {
DOCKER_CREDENTIAL_ID = 'dockerhub-id'
GITHUB_CREDENTIAL_ID = 'github-id'
KUBECONFIG_CREDENTIAL_ID = 'k8s-kubeconfig'
REGISTRY = 'www.harbor.oldboyedu.com'
HARBOR_NAMESPACE = 'ruoyi-cloud'
GITHUB_ACCOUNT = 'kubesphere'
APP_NAME = 'devops-java-sample'
DEPLOY_YAML = '''
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: ${MODULE_NAME}
name: ${MODULE_NAME}
namespace: ${HARBOR_NAMESPACE}
spec:
replicas: 1
selector:
matchLabels:
app: ${MODULE_NAME}
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 25%
maxSurge: 25%
revisionHistoryLimit: 10
progressDeadlineSeconds: 600
template:
metadata:
labels:
app: ${MODULE_NAME}
spec:
imagePullSecrets:
- name: harbor
containers:
- name: ${MODULE_NAME}
imagePullPolicy: Always
image: $REGISTRY/$HARBOR_NAMESPACE/$MODULE_NAME:$MODULE_VERSION
ports:
- containerPort: 8080
protocol: TCP
resources:
limits:
cpu: 1000m
memory: 500Mi
requests:
cpu: 10m
memory: 10Mi
restartPolicy: Always
terminationGracePeriodSeconds: 30
---
apiVersion: v1
kind: Service
metadata:
name: ${MODULE_NAME}
namespace: ${HARBOR_NAMESPACE}
labels:
app: ${MODULE_NAME}
spec:
ports:
- port: 8080
targetPort: 8080
selector:
app: ${MODULE_NAME}
type: ClusterIP
sessionAffinity: None
'''
}
parameters {
choice(name: 'MODULE_NAME', choices: '''ruoyi-auth
ruoyi-gateway
ruoyi-system
ruoyi-job
ruoyi-monitor
ruoyi-file''', description: '模块构建')
choice(name: 'BUILD_ENV', choices: '''UAT_CICD
UAT_CD''', description: '构建环境')
string(name: 'MODULE_VERSION', defaultValue: 'v0.0Beta', description: '模块版本')
}
}
tanqidi-jenkinsfile
未经允许不得转载:工具盒子 » tanqidi-jenkinsfile