1 Star 0 Fork 14.8K

user_5348805/RuoYi-Cloud_3

forked from 若依/RuoYi-Cloud 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
Jenkinsfile 7.44 KB
一键复制 编辑 原始数据 按行查看 历史
user_5348805 提交于 2022-11-07 06:32 . add Jenkinsfile.
pipeline {
agent {
label 'maven'
}
parameters {
string(name: 'Environments', defaultValue: 'dev', description: '支持的发布环境:dev,test,prod')
string(name: 'ServicesBuild', defaultValue: 'no', description: '构建镜像,支持单选和多选,选项有:ruoyi-system,ruoyi-gen,ruoyi-job,ruoyi-file,ruoyi-monitor,ruoyi-gateway,ruoyi-auth,all,no')
string(name: 'MultiServicesDeploy', defaultValue: 'no', description: '发布服务,支持单选和多选,选项有:ruoyi-system,ruoyi-gen,ruoyi-job,ruoyi-file,ruoyi-monitor,ruoyi-gateway,ruoyi-auth,all,no')
string(name: 'DeployVersion', defaultValue: 'no', description: '发布的镜像版本,该选项用在 test 和 prod 环境')
}
environment {
HARBOR_CREDENTIAL_ID = 'harbor-id'
GITLAB_CREDENTIAL_ID = 'gitlab-id'
KUBECONFIG_CREDENTIAL_ID = 'kube-id'
REGISTRY = 'harbor.yunzhicloud.com:3443'
DOCKERHUB_NAMESPACE = 'ruoyi'
K8S_NAMESPACE = 'unify-user'
BUILD_VERSION = sh(script:"echo `date +%s`", returnStdout: true).trim()
// 可以看出来,YAML中需要引用 parametes中的参数,只能写成 ${PROJECT_NAME} 这种形式。
// k8s YAML中如果要引用 environment中的参数,可以写成 $APP_NAME 这种形式。
}
stages {
stage ('拉取代码') {
agent none
when {
not {
equals expected: 'no', actual: params.ServicesBuild
}
}
steps {
echo "Current build version :: ${params.DeployVersion}"
echo "Current build version :: $BUILD_VERSION"
checkout(scm)
}
}
stage('初始化变量') {
agent none
steps {
container('maven') {
script {
//自由组合发布
if("${params.ServicesBuild}".trim() != "no") {
if("${params.ServicesBuild}".trim() == "all") {
ServicesBuild = 'ruoyi-system,ruoyi-gen,ruoyi-job,ruoyi-file,ruoyi-monitor,ruoyi-gateway,ruoyi-auth'
ServicesBuild = "${ServicesBuild}".split(",")
}else {
ServicesBuild = "${params.ServicesBuild}".split(",")
}
for (service in ServicesBuild) {
println "now docker build ${service}"
}
}
if("${params.MultiServicesDeploy}".trim() != "no") {
if("${params.MultiServicesDeploy}".trim() == "all") {
MultiServicesDeploy = 'ruoyi-system,ruoyi-gen,ruoyi-job,ruoyi-file,ruoyi-monitor,ruoyi-gateway,ruoyi-auth'
MultiServicesDeploy = "${MultiServicesDeploy}".split(",")
}else {
MultiServicesDeploy = "${params.MultiServicesDeploy}".split(",")
}
for (service in MultiServicesDeploy) {
println "now docker deploy ${service}"
}
}
}
}
}
}
stage('打包') {
agent none
when {
not {
equals expected: 'no', actual: params.ServicesBuild
}
}
steps {
container('maven') {
script {
sh "mvn -Dmaven.test.skip=true clean package"
// sh 'mvn -Dmaven.test.skip=true clean package -gs `pwd`/config/settings.xml'
}
}
}
}
stage('构建镜像') {
agent none
when {
not {
equals expected: 'no', actual: params.ServicesBuild
}
}
steps {
container('maven') {
script {
def branchedStages = [:]
for (service in ServicesBuild) {
// def workspace = "ruoyi-"
def workspace = ""
def svc = "${service}"
if("${svc}".trim() == "ruoyi-gateway" || "${svc}".trim() == "ruoyi-auth" || "${svc}".trim() == "ruoyi-admin"){
// workspace = "${workspace}" + "${svc}".trim().split("-")[1]
workspace = "${svc}"
}
if("${svc}".trim() == "ruoyi-system" || "${svc}".trim() == "ruoyi-gen" || "${svc}".trim() == "ruoyi-job" || "${svc}".trim() == "ruoyi-file"){
// workspace = "ruoyi-modules/" + "${workspace}" + "${svc}".trim().split("-")[1]
workspace = "ruoyi-modules/" + "${svc}"
}
if("${svc}".trim() == "ruoyi-monitor"){
// workspace = "ruoyi-visual/" + "${workspace}" + "${svc}".trim().split("-")[1]
workspace = "ruoyi-visual/" + "${svc}"
}
branchedStages["${svc} 镜像构建与推送"] = {
stage ("${svc}") {
// sh "docker build --build-arg workspace=${workspace} -f Dockerfile -t $REGISTRY/$DOCKERHUB_NAMESPACE/$svc:$BUILD_VERSION ."
sh "cd ${workspace} && docker build -f Dockerfile -t $REGISTRY/$DOCKERHUB_NAMESPACE/$svc:$BUILD_VERSION ."
withCredentials([usernamePassword(passwordVariable : 'HARBOR_PASSWORD' ,usernameVariable : 'HARBOR_USERNAME' ,credentialsId : "$HARBOR_CREDENTIAL_ID" ,)]) {
sh 'echo "$HARBOR_PASSWORD" | docker login $REGISTRY -u "$HARBOR_USERNAME" --password-stdin'
sh "docker push $REGISTRY/$DOCKERHUB_NAMESPACE/${svc}:$BUILD_VERSION"
}
}
}
}
// Execute the stages in parallel
parallel branchedStages
}
}
}
}
stage('部署到服务') {
agent none
when {
not {
equals expected: 'no', actual: params.MultiServicesDeploy
}
}
steps {
container('maven') {
script {
def branchedStages = [:]
if ("${params.Environments}".trim() == "prod") {
input(id: 'deploy-to-prod', message: '@admin发布到正式环境吗?', submitter:'admin, lcj')
}
for (service in MultiServicesDeploy) {
def workspace = ""
def svc = "${service}"
if("${params.DeployVersion}".trim() != "no"){
env.DEPLOY_VERSION = "${params.DeployVersion}".trim()
}else{
env.DEPLOY_VERSION = "$BUILD_VERSION"
}
if("${svc}".trim() == "ruoyi-gateway" || "${svc}".trim() == "ruoyi-auth" || "${svc}".trim() == "ruoyi-admin"){
workspace = "${svc}".trim()
}
if("${svc}".trim() == "ruoyi-system" || "${svc}".trim() == "ruoyi-gen" || "${svc}".trim() == "ruoyi-job" || "${svc}".trim() == "ruoyi-file"){
workspace = "ruoyi-modules/" + "${svc}"
}
if("${svc}".trim() == "ruoyi-monitor"){
workspace = "ruoyi-visual/" + "${svc}"
}
branchedStages["${svc} 即将部署"] = {
stage ("即将部署的服务是 ${svc}") {
withCredentials([
kubeconfigFile(
credentialsId: env.KUBECONFIG_CREDENTIAL_ID,
variable: 'KUBECONFIG')
]) {
println "部署的版本: $DEPLOY_VERSION"
sh "envsubst < ${workspace}/deploy/service.yaml | kubectl apply -f -"
sh "envsubst < ${workspace}/deploy/deployment.yaml | kubectl apply -f -"
}
}
}
}
// Execute the stages in parallel
parallel branchedStages
}
}
}
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/fulinzhongguo/RuoYi-Cloud_3.git
git@gitee.com:fulinzhongguo/RuoYi-Cloud_3.git
fulinzhongguo
RuoYi-Cloud_3
RuoYi-Cloud_3
master

搜索帮助