Skip to content

一、active案例

1.rollback

active案例

active案例

Groovy Script :

return [
"tmrm-auth-business-service",
"tmrm-auth-service",
"tmrm-auth-web",
"tmrm-order-service"
]
Groovy Script :

return [
"tmrm-auth-business-service",
"tmrm-auth-service",
"tmrm-auth-web",
"tmrm-order-service"
]

active案例

active案例

Groovy Script

rollback=['bash', '-c', "ssh 172.16.0.96 'sh /data/scritps/less.sh;cat cat /data/scritps/tmrm-order-service.txt' "].execute().text.readLines()

if (actions.equals("tmrm-order-service")) {
  return rollback
} else {
  return ["选择action后显示"]
}
Groovy Script

rollback=['bash', '-c', "ssh 172.16.0.96 'sh /data/scritps/less.sh;cat cat /data/scritps/tmrm-order-service.txt' "].execute().text.readLines()

if (actions.equals("tmrm-order-service")) {
  return rollback
} else {
  return ["选择action后显示"]
}

active案例

active案例

Groovy Script

business=["all","172.16.0.96"]
if (actions.equals("tmrm-order-service")) {
  return business
} else {
  return ["选择action后显示"]
}
Groovy Script

business=["all","172.16.0.96"]
if (actions.equals("tmrm-order-service")) {
  return business
} else {
  return ["选择action后显示"]
}
  • 效果图

active案例

#/bin/bash
echoRed() { echo $'\e[0;31m'"$1"$'\e[0m'; }
echoYellow() { echo $'\e[0;33m'"$1"$'\e[0m'; }

rollbackWar=/data/test_env_project_war/back_war
rollbackPath=/data/webapps
scripts=/data/webapps/scripts

remoteFile=`ssh $host "ls ${rollbackWar}/$rollback"`
FileName=${remoteFile##*/}
rollbackname=${FileName%.*}


if [ "$FileName" == "$rollback" ];then
	echoRed "该${FileName}包存在可以回滚"
    echoRed "开始执行替换。。。。"
    ssh $host "\cp -f ${rollbackWar}/$rollback $rollbackPath/$rollbackname/${rollbackname}.jar"
    
    echoRed "开启启动$rollbackname。。。。"
    ssh $host "sh ${scripts}/order/rollbackService.sh ${sprintActive}"
    
else
	echoRed "该${FileName}包不存在,请检查该版本之后在回滚"
	exit 0
fi
#/bin/bash
echoRed() { echo $'\e[0;31m'"$1"$'\e[0m'; }
echoYellow() { echo $'\e[0;33m'"$1"$'\e[0m'; }

rollbackWar=/data/test_env_project_war/back_war
rollbackPath=/data/webapps
scripts=/data/webapps/scripts

remoteFile=`ssh $host "ls ${rollbackWar}/$rollback"`
FileName=${remoteFile##*/}
rollbackname=${FileName%.*}


if [ "$FileName" == "$rollback" ];then
	echoRed "该${FileName}包存在可以回滚"
    echoRed "开始执行替换。。。。"
    ssh $host "\cp -f ${rollbackWar}/$rollback $rollbackPath/$rollbackname/${rollbackname}.jar"
    
    echoRed "开启启动$rollbackname。。。。"
    ssh $host "sh ${scripts}/order/rollbackService.sh ${sprintActive}"
    
else
	echoRed "该${FileName}包不存在,请检查该版本之后在回滚"
	exit 0
fi
  • if else
dev=["dev-01"]
qa=["qa-01","qa-02","qa-03"]
pl= ["pl-01","pl-02"]

if (env.equals("dev")) {
  return dev
} else if (env.equals("qa")) {
  return qa
} else if (env.equals("pl")) {
  return pl
} else {
  return ["Unknown hosts"]
}
dev=["dev-01"]
qa=["qa-01","qa-02","qa-03"]
pl= ["pl-01","pl-02"]

if (env.equals("dev")) {
  return dev
} else if (env.equals("qa")) {
  return qa
} else if (env.equals("pl")) {
  return pl
} else {
  return ["Unknown hosts"]
}
  • 列出文件列表
path="/data/jenkins/workspace/backup/test-monitor-grade-44s"
rollback=['bash', '-c', "ls -t1 ${path}"].execute().text.readLines()

if (Action.equals("RollBack")) {
  return rollback
} else {
  return ["选择RollBack后显示"]
}
path="/data/jenkins/workspace/backup/test-monitor-grade-44s"
rollback=['bash', '-c', "ls -t1 ${path}"].execute().text.readLines()

if (Action.equals("RollBack")) {
  return rollback
} else {
  return ["选择RollBack后显示"]
}

pipeline

properties([
    parameters([
        [$class: 'ChoiceParameter', 
            choiceType: 'PT_SINGLE_SELECT', 
            description: 'Select the Env Name from the Dropdown List', 
            filterLength: 1, 
            filterable: true, 
            name: 'Env', 
            randomName: 'choice-parameter-5631314439613978', 
            script: [
                $class: 'GroovyScript', 
                fallbackScript: [
                    classpath: [], 
                    sandbox: false, 
                    script: 
                        'return[\'Could not get Env\']'
                ], 
                script: [
                    classpath: [], 
                    sandbox: false, 
                    script: 
                        'return["Dev","QA","Stage","Prod"]'
                ]
            ]
        ], 
        [$class: 'CascadeChoiceParameter', 
            choiceType: 'PT_SINGLE_SELECT', 
            description: 'Select the Server from the Dropdown List', 
            filterLength: 1, 
            filterable: true, 
            name: 'Server', 
            randomName: 'choice-parameter-5631314456178619', 
            referencedParameters: 'Env', 
            script: [
                $class: 'GroovyScript', 
                fallbackScript: [
                    classpath: [], 
                    sandbox: false, 
                    script: 
                        'return[\'Could not get Environment from Env Param\']'
                ], 
                script: [
                    classpath: [], 
                    sandbox: false, 
                    script: 
                        ''' if (Env.equals("Dev")){
                                return["devaaa001","devaaa002","devbbb001","devbbb002","devccc001","devccc002"]
                            }
                            else if(Env.equals("QA")){
                                return["qaaaa001","qabbb002","qaccc003"]
                            }
                            else if(Env.equals("Stage")){
                                return["staaa001","stbbb002","stccc003"]
                            }
                            else if(Env.equals("Prod")){
                                return["praaa001","prbbb002","prccc003"]
                            }
                        '''
                ]
            ]
        ]
    ])
])

pipeline {
  environment {
         vari = ""
  }
  agent any
  stages {
      stage ("Example") {
        steps {
         script{
          echo 'Hello'
          echo "${params.Env}"
          echo "${params.Server}"
          //if (params.Server.equals("Could not get Environment from Env Param")) {
          //    echo "Must be the first build after Pipeline deployment.  Aborting the build"
          //    currentBuild.result = 'ABORTED'
          //    return
          //}
          echo "Crossed param validation"
			}
        }
        
      }
  }
}
properties([
    parameters([
        [$class: 'ChoiceParameter', 
            choiceType: 'PT_SINGLE_SELECT', 
            description: 'Select the Env Name from the Dropdown List', 
            filterLength: 1, 
            filterable: true, 
            name: 'Env', 
            randomName: 'choice-parameter-5631314439613978', 
            script: [
                $class: 'GroovyScript', 
                fallbackScript: [
                    classpath: [], 
                    sandbox: false, 
                    script: 
                        'return[\'Could not get Env\']'
                ], 
                script: [
                    classpath: [], 
                    sandbox: false, 
                    script: 
                        'return["Dev","QA","Stage","Prod"]'
                ]
            ]
        ], 
        [$class: 'CascadeChoiceParameter', 
            choiceType: 'PT_SINGLE_SELECT', 
            description: 'Select the Server from the Dropdown List', 
            filterLength: 1, 
            filterable: true, 
            name: 'Server', 
            randomName: 'choice-parameter-5631314456178619', 
            referencedParameters: 'Env', 
            script: [
                $class: 'GroovyScript', 
                fallbackScript: [
                    classpath: [], 
                    sandbox: false, 
                    script: 
                        'return[\'Could not get Environment from Env Param\']'
                ], 
                script: [
                    classpath: [], 
                    sandbox: false, 
                    script: 
                        ''' if (Env.equals("Dev")){
                                return["devaaa001","devaaa002","devbbb001","devbbb002","devccc001","devccc002"]
                            }
                            else if(Env.equals("QA")){
                                return["qaaaa001","qabbb002","qaccc003"]
                            }
                            else if(Env.equals("Stage")){
                                return["staaa001","stbbb002","stccc003"]
                            }
                            else if(Env.equals("Prod")){
                                return["praaa001","prbbb002","prccc003"]
                            }
                        '''
                ]
            ]
        ]
    ])
])

pipeline {
  environment {
         vari = ""
  }
  agent any
  stages {
      stage ("Example") {
        steps {
         script{
          echo 'Hello'
          echo "${params.Env}"
          echo "${params.Server}"
          //if (params.Server.equals("Could not get Environment from Env Param")) {
          //    echo "Must be the first build after Pipeline deployment.  Aborting the build"
          //    currentBuild.result = 'ABORTED'
          //    return
          //}
          echo "Crossed param validation"
			}
        }
        
      }
  }
}

url:

http://wiki.eryajf.net/pages/2075.html#_3-调整shell。

http://blog.leanote.com/post/benmo/jenkins基于Ansible自动发布spring-boot-cloud项目-非K8S

https://wiki.eryajf.net/pages/3510.html#_3-优化方案

https://blog.51cto.com/u_3388803/2519849

https://blog.csdn.net/TOBEALISTENNER/article/details/109071609