但是还是以新环境为原则来编写。仅仅是部分内容有省略。
选择新建任务,选择流水线,填写任务名称为:springboot-demo-pipline
这一块是最为重要的内容,这一块将按循序渐进的方式进行。这里采用软件VSCode编写Jenkinsfile的代码。VSCode软件中可以安装Jenkinsfile支持插件。由于这是入门案例,所以代码是直接黏贴到Jenkins页面的。
选择“Pipline script”
添加如下代码:
pipeline{
agent any
tools {
maven 'maven3.6.3'
jdk 'jdk1.8'
}
environment{
TASK_NAME="springboot-demo-pipline"
ROOT_PROJECT_PATH="user-system"
USER_SERVICE_PROJECT="user-service-0.0.1-SNAPSHOT.jar"
USER_SERVICE_PROJECT_NAME="user-service"
}
options{
buildDiscarder(logRotator(numToKeepStr: '10'))
timestamps()
}
triggers{
pollSCM('H/1 * * * *')
}
/*后续其他代码*/
}
该脚本完整代码如下:
pipeline{
/*第一部分代码*/
stages{
stage('Preparation'){
steps{
echo 'pulling code'
/*生成的git代码*/
}
}
}
}
这一步可以进行测试构建。
完整代码如下:
stage('Build'){
steps{
script{
echo 'Stopping SpringBoot Application'
pid= sh (script:"ps -ef | grep $USER_SERVICE_PROJECT | grep -v grep | awk '{print \$2}'",returnStdout: true).trim()
if (pid!=""){
echo "Kill process: $pid"
sh "kill -9 $pid"
}
echo 'starting build'
sh 'mvn -f $ROOT_PROJECT_PATH clean install -Dmaven.test.skip=true'
echo 'deleteing old project'
sh 'rm -rf /usr/local/src/java/$USER_SERVICE_PROJECT'
echo 'copy new project'
sh 'cp /var/lib/jenkins/workspace/$TASK_NAME/$USER_SERVICE_PROJECT_NAME/target/$USER_SERVICE_PROJECT /usr/local/src/java/'
echo "starting project"
sh 'JENKINS_NODE_COOKIE=dontKillMe nohup java -jar /usr/local/src/java/$USER_SERVICE_PROJECT >log.txt 2>&1 &'
echo "startup success!"
}
}
}
此时,再次测试构建项目。
在上一篇配置 "系统配置"的基础上,在pipeline script中加入如下代码:
pipeline{
/*其他代码*/
post {
success {
emailext (
subject: "SUCCESSFUL: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'",
body: """ SUCCESSFUL: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]':
Check console output at "${env.JOB_NAME} [${env.BUILD_NUMBER}]"
to: "[email protected],[email protected]",
from: "[email protected]"
)
}
failure {
emailext (
subject: "FAILED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'",
body: """ FAILED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]':
Check console output at "${env.JOB_NAME} [${env.BUILD_NUMBER}]"
to: "[email protected],[email protected]",
from: "[email protected]"
)
}
}
}
在上一篇的“系统配置“基础上,在pipeline script中添加如下代码:
post {
success {
/*省略email代码*/
slackSend channel: "#test-jenkins", message: "Content Build and Validate Succeded for ${env.CONTENT_GIT_REPO} for Repository ${env.CONTENT_GIT_URL} by ${env.JOB_NAME} (<${env.BUILD_URL}|Open>)", color: 'success', tokenCredentialId: 'slack-token'
}
failure {
/*省略email代码*/
slackSend channel: "#test-jenkins", message: "Content Build and Validate FAILED for ${env.CONTENT_GIT_REPO} for Repository ${env.CONTENT_GIT_URL} by ${env.JOB_NAME} (<${env.BUILD_URL}|Open>)", color: 'warning', tokenCredentialId: 'slack-token'
}
}
再次构建项目,slack和邮箱均收到信息。
pipeline{
agent any
tools {
maven 'maven3.6.3'
jdk 'jdk1.8'
}
environment{
TASK_NAME="springboot-demo-pipline"
ROOT_PROJECT_PATH="user-system"
USER_SERVICE_PROJECT="user-service-0.0.1-SNAPSHOT.jar"
USER_SERVICE_PROJECT_NAME="user-service"
}
options{
buildDiscarder(logRotator(numToKeepStr: '10'))
timestamps()
}
triggers{
pollSCM('H/1 * * * *')
}
stages{
stage('Preparation'){
steps{
echo 'pulling code'
git credentialsId:' ', url: ' ' /*此处补齐*/
}
}
stage('Build'){
steps{
script{
echo 'Stopping SpringBoot Application'
pid= sh (script:"ps -ef | grep $USER_SERVICE_PROJECT | grep -v grep | awk '{print \$2}'",returnStdout: true).trim()
if (pid!=""){
echo "Kill process: $pid"
sh "kill -9 $pid"
}
echo 'starting build'
sh 'mvn -f $ROOT_PROJECT_PATH clean install -Dmaven.test.skip=true'
echo 'deleteing old project'
sh 'rm -rf /usr/local/src/java/$USER_SERVICE_PROJECT'
echo 'copy new project'
sh 'cp /var/lib/jenkins/workspace/$TASK_NAME/$USER_SERVICE_PROJECT_NAME/target/$USER_SERVICE_PROJECT /usr/local/src/java/'
echo "starting project"
sh 'JENKINS_NODE_COOKIE=dontKillMe nohup java -jar /usr/local/src/java/$USER_SERVICE_PROJECT >log.txt 2>&1 &'
echo "startup success!"
}
}
}
}
post {
success {
emailext (
subject: "SUCCESSFUL: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'",
body: """ SUCCESSFUL: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]':
Check console output at "${env.JOB_NAME} [${env.BUILD_NUMBER}]"
to: "[email protected],[email protected]",
from: "[email protected]"
)
slackSend channel: "#test-jenkins", message: "Content Build and Validate Succeded for ${env.CONTENT_GIT_REPO} for Repository ${env.CONTENT_GIT_URL} by ${env.JOB_NAME} (<${env.BUILD_URL}|Open>)", color: 'success', tokenCredentialId: 'slack-token'
}
failure {
emailext (
subject: "FAILED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'",
body: """ FAILED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]':
Check console output at "${env.JOB_NAME} [${env.BUILD_NUMBER}]"
to: "[email protected],[email protected]",
from: "[email protected]"
)
slackSend channel: "#test-jenkins", message: "Content Build and Validate FAILED for ${env.CONTENT_GIT_REPO} for Repository ${env.CONTENT_GIT_URL} by ${env.JOB_NAME} (<${env.BUILD_URL}|Open>)", color: 'warning', tokenCredentialId: 'slack-token'
}
}
}
源码及其文档下载地址:https://download.csdn.net/download/qq_25337221/12436082
本内容由安康学院“雨季”原创。