【jenkins】编写配置spring-boot项目, CI/CD流水线项目

1. 新建项目

 

 2. 新建流水线项目

 

 输入名称,选择流水线项目

3. 基础配置

 

 4. 根据自己需求配置对应参数

 

 4. 配置触发器

 

 5. 配置触发器高级选项

 

 6. 配置高级项目选项-流水线脚本

 

 7. pipline脚本示例

pipeline {
    agent any 
    stages {
        stage('PreEnv') {
            steps {
                script {
                    rootDir = rootDir
                }
                echo 'rootDir: ${rootDir}'
                sh 'ifconfig'
                sh 'sudo apt install -y openjdk-11-jdk'
                sh 'sudo apt install -y maven'
                sh 'mvn -v'
                sh 'java -version'
                //sh 'which java'
                sh 'sudo update-alternatives --install /usr/local/java/bin/java java /usr/lib/jvm/java-11-openjdk-amd64/bin/java 3000'
                //sh 'sudo update-alternatives --list java'
                sh 'sudo update-alternatives --auto java'
                //sh 'sudo update-alternatives --display java'
                sh 'java -version'
            }
        }
        stage('Build') { 
            steps {
                echo 'start Build......'
                checkout([$class: 'GitSCM', branches: [[name: '*/test']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'SubmoduleOption', disableSubmodules: true, parentCredentials: false, recursiveSubmodules: false, reference: '', trackingSubmodules: false], [$class: 'RelativeTargetDirectory', relativeTargetDir: 'xxxx']], submoduleCfg: [], userRemoteConfigs: [[url: 'git@xxxx:xxxx/xxxxx.git']]]) 
                sh 'cd $rootDir && ls'
                sh 'cd $rootDir && mvn clean package -DskipTests'
                
            }
        }
        stage('Test') {
            steps {
                sh 'echo start Test........'
                sh 'cd $rootDir'
                writeFile file: 'stop.sh', text: "${STOP_SCRIPT}"
                sh 'ls'
                sh 'sudo bash ./stop.sh'
                sh 'nohup java -jar /home/jenkins/workspace/xxxxx/xxxxx/target/xxxx-0.0.1-SNAPSHOT.jar >> test.log &'
                sh 'ps -ef | grep xxxxx-0.0.1-SNAPSHOT.jar'
                sh 'kill $(ps -ef | grep xxxx-0.0.1-SNAPSHOT.jar | grep -v grep | awk \'{ print $2 }\')'
                sh 'sudo bash ./stop.sh'
                sh 'ps -ef | grep xxxxx-0.0.1-SNAPSHOT.jar'
            }
        }
        stage('Deploy') { 
            steps {
                sh 'echo start Deploy........'
                //sh 'scp /home/jenkins/workspace/xxxxx-test/xxxxx/target/xxxx-0.0.1-SNAPSHOT.jar jenkins@192.168.3.110:/home/xxxxx/xxxx/'
                // TODO execute '/home/xxxx/xxxx/script/upgrade.sh' remotely for redeploy.
                // TODO check redeploy state. 
                
            }
        }
    }
}

 

posted @ 2021-12-22 14:37  代码诠释的世界  阅读(415)  评论(0编辑  收藏  举报