欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

jenkins

程序员文章站 2022-05-04 11:01:29
...

jenkins

https://www.w3cschool.cn/jenkins/

https://jenkins.io/zh/doc/

https://jenkins-zh.cn/tutorial/

https://yq.aliyun.com/articles/35169

持续集成

持续集成(CONTINUOUS INTEGRATION,简称CI)

持续集成指的是,频繁地(一天多次)将代码集成到主干

它的好处主要有两个

(1)快速发现错误。每完成一点更新,就集成到主干,可以快速发现错误,定位错误也比

较容易

(2)防止分支大幅偏离主干。如果不是经常集成,主干又在不断更新,会导致以后集成的

难度变大,甚至难以集成

持续交付

持续交付(CONTINUOUS DELIVERY)指的是,频繁地将软件的新版本,交付给质量团队或者用户,以供评审。如果评审通过,代码就进入生产阶段。

持续交付可以看作持续集成的下一步。它强调的是,不管怎么更新,软件是随时随地可以交付的。

持续部署

持续部署(CONTINUOUS DEPLOYMENT)是持续交付的下一步,指的是代码通过评审以后,自动部署到生产环境。

持续部署的目标是,代码在任何时刻都是可部署的,可以进入生产阶段

jenkins

JENKINS是一个用JAVA编写的开源的持续集成工具。在与ORACLE发生争执后,项目从HUDSON项目独立出来。

JENKINS提供了软件开发的持续集成服务。它运行在SERVLET容器中(例如APACHE TOMCAT)。它支持软件配置管理(SCM)工具(包括ACCUREV SCM、CVS、SUBVERSION、GIT、PERFORCE、CLEARCASE和RTC),可以执行基于APACHE ANT和APACHE MAVEN的项目,以及任意的SHELL脚本和WINDOWS批处理命令。JENKINS的主要开发者是川口耕介。

JENKINS是在MIT许可证下发布的*软件

jenkins中一切皆文件

jenkins安装配置启动

下载jenkins

http://pkg.jenkins.io/redhat-stable/

https://mirrors.tuna.tsinghua.edu.cn/jenkins/redhat-stable/

安装jdk

yum -y install java-1.8.0-openjdk java-1.8.0-openjdk-devel

安装jenkins

rpm -ivh jenkins-2.73.1-1.1.noarch.rpm
ln -s /usr/local/jdk/bin/java /usr/bin/java

修改jenkins运行用户

/etc/sysconfig/jenkins

JENKINS_USER="root"

启动jenkins

/etc/init.d/jenkins start

访问

http://10.0.0.201:8080

解锁jenkins

cat /var/lib/jenkins/secrets/initialAdminPassword

插件

https://mirrors.tuna.tsinghua.edu.cn/jenkins/plugins/

https://updates.jenkins.io/update-center.json

https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json

插件目录

/var/lib/jenkins/plugins/

插件更新

[aaa@qq.com ~]# grep -rl 'update-center' /var/lib/jenkins/
/var/lib/jenkins/hudson.model.UpdateCenter.xml

cat /var/lib/jenkins/hudson.model.UpdateCenter.xml

<?xml version='1.0' encoding='UTF-8'?>
<sites>
  <site>
    <id>default</id>
    <url>https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json</url>
  </site>
</sites>

https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json

http://10.0.0.201:8080

登录

用户名:admin

密码:

cat /var/lib/jenkins/secrets/initialAdminPassword

e74cb1cd08a24d5ebd526d086b6a0e74

安装的内容

rpm -ql jenkins
/etc/init.d/jenkins
/etc/logrotate.d/jenkins
/etc/sysconfig/jenkins        #配置文件
/usr/lib/jenkins             
/usr/lib/jenkins/jenkins.war
/usr/sbin/rcjenkins
/var/cache/jenkins
/var/lib/jenkins
/var/log/jenkins

用户文件

/var/lib/jenkins/users/admin/config.xml

工作目录

/var/lib/jenkins/workspace/freestyle-job1

https://gitee.com/kangjie1209/monitor

jenkins使用

调整并发执行任务

jenkins

jenkins

配置邮件

jenkins

smtp.163.com

smtp.126.com

smtp.qq.com

/etc/mail.rc

jenkins

gitlab添加jenkins公钥

jenkins

项目

创建项目

jenkins

general

jenkins

参数化构建过程

jenkins

jenkins

源码管理

jenkins

jenkins

jenkins添加私钥

jenkins

jenkins

构建

jenkins

/server/scripts/deploy.sh

#!/bin/sh
DATE=$(date +%Y-%m-%d-%H-%M-%S)
CODE_DIR="/var/lib/jenkins/workspace/freestyle-job/"
WEB_DIR="/var/www/"

get_code_tar(){
        cd $CODE_DIR && tar zcf /opt/web-$DATE.tar.gz ./*
}

scp_code_web(){
        scp /opt/web-$DATE.tar.gz 10.0.0.8:$WEB_DIR
}

code_tarxf(){
        ssh 10.0.0.8 "cd $WEB_DIR &&mkdir web-$DATE && tar xf web-$DATE.tar.gz -C web-$DATE"

}
ln_html(){
         ssh 10.0.0.8 "cd $WEB_DIR && rm -rf html && ln -s web-$DATE html"
}

main(){
        get_code_tar;
        scp_code_web;
        code_tarxf;
        ln_html;
}
main

构建触发器

用于触发构建

jenkins

jenkins

gitlab项目中添加 jenkins URL和Token

jenkins

jenkins

gitlab test

jenkins

** 测试结果**

jenkins

jenkins

git push 之后自动触发jenkins 构建

构建后

jenkins

jenkins

jenkins

jenkins 连接gitlab API

jenkins

gitlab private token

jenkins


maven

maven环境变量

vim /etc/profile.d/maven.sh

export MAVEN_HOME=/usr/local/maven
export PATH="$MAVEN_HOME/bin:$PATH"

maven仓库配置

/usr/local/maven/conf/settings.xml

<mirrors>
<mirror> 
<id>nexus-aliyun</id> 
<mirrorOf>*</mirrorOf> 
<name>Nexus aliyun</name> 
<url>http://maven.aliyun.com/nexus/content/groups/public</url> 
</mirror>
</mirrors>

maven打包

mvn package

本地仓库

~/.m2/repository/

清空包

mvn clean

jdk修改

/usr/java/jdk1.8.0_121/jre/lib/security/java.security

117行

securerandom.source=file:/dev/urandom

war包部署

scp jeesns-web.war aaa@qq.com:/usr/local/tomcat/webapps/ROOT.war

java程序连接数据库文件

WEB-INF/classes/jeesns.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/jeesns?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true
jdbc.user=root
jdbc.password=root
managePath=manage
groupPath=group
weiboPath=weibo
frontTemplate=front
memberTemplate=member
manageTemplate=manage
mobileTemplate=mobile

nexus(伺服)

安装启动nexus

rpm -ivh jdk-8u121-linux-x64.rpm 
tar xf nexus-3.13.0-01-unix.tar.gz 
mv nexus-3.13.0-01 /usr/local/
ln -s /usr/local/nexus-3.13.0-01/ /usr/local/nexus
/usr/local/nexus/bin/nexus start
ss -lntup |grep java
tcp    LISTEN     0      1      127.0.0.1:42219                 *:*                   users:(("java",pid=4939,fd=82))
tcp    LISTEN     0      50        *:8081                  *:*                   users:(("java",pid=4939,fd=786))

10.0.0.202:8081

用户名:admin

密码:admin123

配置Maven全局配置文件

/usr/local/maven/conf/settings.xml

<?xml version="1.0" encoding="UTF-8"?>

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
  <pluginGroups>
  </pluginGroups>

  <proxies>
  </proxies>

  <servers>
    <server>   
      <id>my-nexus-releases</id>   
      <username>admin</username>   
      <password>admin123</password>   
    </server>   
    <server>   
      <id>my-nexus-snapshot</id>   
      <username>admin</username>   
      <password>admin123</password>   
    </server>
  </servers>

  <mirrors>
    <mirror>
      <id>nexus</id>
      <mirrorOf>*</mirrorOf>
      <url>http://10.0.0.203:8081/repository/maven-public/</url>
    </mirror>
  </mirrors>

  <profiles>
    <profile>
      <id>nexus</id>
      <repositories>
        <repository>
          <id>central</id>
          <url>http://10.0.0.203:8081/repository/maven-public/</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </repository>
      </repositories>
    <pluginRepositories>
        <pluginRepository>
          <id>central</id>
          <url>http://10.0.0.203:8081/repository/maven-public/</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </pluginRepository>
      </pluginRepositories>
    </profile>  
  </profiles>
<activeProfiles>
  <activeProfile>nexus</activeProfile>
</activeProfiles>
</settings>

配置完成后保存,并重启 nexus 服务

重启 nexus 服务 完成后,在命令行窗口进入一个使用 Maven 构建的项目

删除 本地仓库 ~/.m2/repository/

清理

mvn clean

重新打包

mvn package

登录 nexus 的 admin 账号可以看到 Public Repositories 下出现了一些 Jar 包


jenkins构建maven项目

jenkins

全局工具配置maven环境

jenkins

jenkins

jenkins

jenkins

ssh 10.0.0.8 "cd /usr/local/tomcat/webapps/ && mv ROOT.war ROOT.war.bak"
scp jeesns-web/target/jeesns-web.war aaa@qq.com:/usr/local/tomcat/webapps/ROOT.war
pipeline {
    agent {
        docker {
            image 'maven:3-alpine'
            args '-v /root/.m2:/root/.m2'
        }
    }
    stages {
        stage('Build') {
            steps {
                sh 'mvn -B -DskipTests clean package'
            }
        }
        stage('Test') {
            steps {
                sh 'mvn test'
            }
            post {
                always {
                    junit 'target/surefire-reports/*.xml'
                }
            }
        }
        stage('Deliver') { 
            steps {
                sh './jenkins/scripts/deliver.sh' 
            }
        }
    }
}

通知

钉钉通知

1.配置钉钉–>创建群组–>添加一个自定义机器人–>获取token

https://oapi.dingtalk.com/robot/send?access_token=acad44677aba443bb486185c1636bf8538c8beaac4f8ad1a34ab2e949eb31d04

2.jenkins+钉钉 ---->安装钉钉的插件

3.项目支持通知机制

jenkins

sonarqube

创建库

mysql -uroot -p123456 -e "create database sonar charset utf8;"

sonarqube

yum install java -y
unzip sonarqube-7.0.zip -d /usr/local/
ln -s /usr/local/sonarqube-7.0/ /usr/local/sonarqube

配置sonarqube连接数据库

[aaa@qq.com ~]# grep '^[a-Z]' /usr/local/sonarqube/conf/sonar.properties 
sonar.jdbc.username=root
sonar.jdbc.password=123456
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false

启动sonarqube

useradd sonar
chown -R sonar.sonar /usr/local/sonarqube-7.0/
su - sonar -c "/usr/local/sonarqube/bin/linux-x86-64/sonar.sh start"

登录sonarqube

用户: admin

密码: admin

sonarqube token

jenkins: 037597413e60d40073a45ee5d9ffeeff79405968
sonar-scanner \
  -Dsonar.projectKey=html \
  -Dsonar.sources=. \
  -Dsonar.host.url=http://10.0.0.8:9000 \
  -Dsonar.login=037597413e60d40073a45ee5d9ffeeff79405968
mvn sonar:sonar \
  -Dsonar.host.url=http://10.0.0.8:9000 \
  -Dsonar.login=037597413e60d40073a45ee5d9ffeeff79405968

导入插件

mv /usr/local/sonarqube/extensions/{plugins,plugins_bak}
tar xf sonar_plugins.tar.gz -C /usr/local/sonarqube/extensions/

重启sonarqube

su - sonar -c "/usr/local/sonarqube/bin/linux-x86-64/sonar.sh restart"

让sonarqube开启验证

**配置–>配置–>权限 Force user authentication **

手动分析代码

1.安装sonar-scanner

安装在Jenkins服务器

unzip sonar-scanner-cli-4.0.0.1744-linux.zip -d /usr/local/
ln -s /usr/local/sonar-scanner-4.0.0.1744-linux/ /usr/local/sonar-scanner

2.配置sonar-scanner

cat /usr/local/sonar-scanner/conf/sonar-scanner.properties

    #----- Default SonarQube server  #需要配置hosts解析
    sonar.host.url=http://10.0.0.8:9000
    sonar.login=6b10565c085c63e589b8f82c3c1d459756f95318
    #----- Default source code encoding
    sonar.sourceEncoding=UTF-8
    

3.扫描代码

进入项目目录扫描

/usr/local/sonar-scanner/bin/sonar-scanner \
      -Dsonar.projectKey=html \
      -Dsonar.sources=.
mvn sonar:sonar \
  -Dsonar.host.url=http://10.0.0.8:9000 \
  -Dsonar.login=037597413e60d40073a45ee5d9ffeeff79405968

jenkins+sonarqube集成

** 1.jenkins安装sonarqube的插件**

** 2.jenkins需要知道sonar客户端命令在哪? **

** 系统管理->全局工具配置->SonarQube Scanner**

jenkins

** 3.jenkins需要知道sonar服务器在哪里? **

** 系统管理–>系统设置–>SonarQube servers**

jenkins

jenkins

html

jenkins

sonar.projectName=${JOB_NAME}
sonar.projectKey=html
sonar.sources=.

java

jenkins

clean
verify
sonar:sonar
-Dsonar.host.url=http://10.0.0.8:9000
-Dsonar.login=037597413e60d40073a45ee5d9ffeeff79405968

jenkins

sonar.projectName=${JOB_NAME}
sonar.projectKey=java
sonar.sources=.
sonar.java.binaries=target/sonar

Jenkins pipeline

jenkins

pipeline分类

声明式 脚本式

https://jenkins.io/zh/doc/book/pipeline/syntax/

声明式

Jenkinsfile

pipeline {
    agent any 
    stages {
        stage('Build') { 
            steps {
                // 
            }
        }
        stage('Test') { 
            steps {
                // 
            }
        }
        stage('Deploy') { 
            steps {
                // 
            }
        }
    }
}
pipeline {
	agent any
	
	stages {   //整个部署的任务
	
		stage ('获取代码'){
			steps {
				echo "获取代码ok"
			}
		}
		stage ('代码质检'){
			steps {
				echo "sonarqube is ok"
			}
		}
		stage ('代码构建') {
			steps {
				echo "构建成功"
			}
		}
		stage ('确认部署?') {
			steps {
				input message: '是否部署', ok: 'ok'
			}
		}
		stage('代码部署'){
			steps {
				echo "部署完毕"
			}
		}
	}
}
pipeline {
    agent any
    stages {
        stage('Example') {
            steps {
                echo 'Hello World'
                script {
                    def browsers = ['chrome', 'firefox']
                    for (int i = 0; i < browsers.size(); ++i) {
                        echo "Testing the ${browsers[i]} browser"
                    }
                }
            }
        }
    }
}

脚本式

Jenkinsfile

node {  
    stage('Build') { 
        // 
    }
    stage('Test') { 
        // 
    }
    stage('Deploy') { 
        // 
    }
}
node {
    stage('Example') {
        if (env.BRANCH_NAME == 'master') {
            echo 'I only execute on the master branch'
        } else {
            echo 'I execute elsewhere'
        }
    }
}
node {
    stage('Example') {
        try {
            sh 'exit 1'
        }
        catch (exc) {
            echo 'Something failed, I should sound the klaxons!'
            throw
        }
    }
}

声明式

pipeline {
    agent any
    stages {
        stage('get code') {
            steps {
                echo 'Building..'
				            sh 'ls'
            }
        }
		stage('Build') {
            steps {
                echo 'Building..'
            }
        }
        stage('Test') {
            steps {
                echo 'Testing..'
            }
        }
        stage('Deploy') {
            steps {
                echo 'Deploying....'
            }
        }
    }
}

Jenkinsfile

放在代码仓库中

pipeline{
  agent any
  stages{
    stage("get code"){
       steps{
            echo "get code"
       }
    }
    stage("unit test"){
       steps{
            echo "unit test"
       } 
    }
    stage("package"){
        steps{
            sh 'tar zcf /opt/web-${BUILD_ID}.tar.gz ./* --exclude=.git --exclude=Jenkinsfile'
        }
    }
    stage("deploy"){
        steps{
            sh 'ssh 10.0.0.8 "cd /usr/share/nginx/ && mkdir web-${BUILD_ID}"'
            sh 'scp /opt/web-${BUILD_ID}.tar.gz 10.0.0.8:/usr/share/nginx/web-${BUILD_ID}'
            sh 'ssh 10.0.0.8 "cd /usr/share/nginx/web-${BUILD_ID} && tar xf web-${BUILD_ID}.tar.gz && rm -rf web-${BUILD_ID}.tar.gz"'
            sh 'ssh 10.0.0.8 "cd /usr/share/nginx/ && rm -f html && ln -s web-${BUILD_ID} html"'
        }
    }
  }
}

jenkins

vim /server/scripts/pre_build.sh

#!/bin/bash
/usr/bin/cp /tmp/application-center-dev.yml   blog-core/src/main/resources/config/application-center-dev.yml
/usr/bin/cp /tmp/application-center-test.yml  blog-core/src/main/resources/config/application-center-test.yml

cat /server/scripts/post_build.sh

#!/bin/bash
export JAVA_HOME=/usr/local/jdk
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib
DATE=$(date +%Y-%m-%d-%H-%M-%S)
tar zcf /opt/oneblog_${DATE}.tar.gz *
ssh aaa@qq.com "mkdir -p /opt/oneblog_${DATE}"
scp -rp /opt/oneblog_${DATE}.tar.gz /server/scripts/oneblog_start.sh aaa@qq.com:/opt/oneblog_${DATE}/
ssh aaa@qq.com "cd /opt/oneblog_${DATE}/ && tar xf oneblog_${DATE}.tar.gz && rm /opt/oneblog -f && ln -s /opt/oneblog_${DATE} /opt/oneblog"
ssh aaa@qq.com "/usr/local/mysql/bin/mysql dblog -e 'source /opt/oneblog/docs/db/init_data.sql;'"
wait
ssh aaa@qq.com "cd /opt/oneblog_${DATE}/ && /bin/bash oneblog_start.sh"

cat /server/scripts/oneblog_start.sh

#!/bin/bash
export JAVA_HOME=/usr/local/jdk
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib
kill -9 $(jps |awk '/blog-web/{print $1}')
sleep 2
cd /opt/oneblog/blog-web
java -jar target/blog-web.jar &> /opt/oneblog/blog-web/oneblog.log &

cat html_deploy_tag_rollback.sh

#!/bin/bash
host="10.0.0.8 10.0.0.7"
DATE=$(date +%F_%H-%M)
Name=${DATE}-${git_version}
get_code() {
	cd ${WORKSPACE}
	tar czf /opt/web-${Name}.tar.gz ./*
}
scp_web(){
for i in $host
do
	scp -r /opt/web-${Name}.tar.gz aaa@qq.com$i:/opt/
	ssh aaa@qq.com$i "rm -rf /code/web && \
                     mkdir -p /code/web-${Name} && \
                     tar xf /opt/web-${Name}.tar.gz -C /code/web-${Name} && \
                     ln -s /code/web-${Name} /code/web"
done
}
back(){
for i in $host
do
	back_version=$(ssh aaa@qq.com${i} find /code/ -maxdepth 1 -type d -name "*-${git_version}")
	ssh aaa@qq.com$i "rm -f /code/web && \
                     ln -s ${back_version} /code/web"
done
}

deploy(){
	get_code
	scp_web
}
#回退
#部署,如果此次提交的commit与之前部署成功的commit对比成功,则退出,否则进入正常的部署阶段
if [ $deploy_env == "deploy" ];then
	if [ ${GIT_COMMIT} == ${GIT_PREVIOUS_SUCCESSFUL_COMMIT} ];then
  echo "该版本 ${git_version} 已部署"
  exit
	else
  deploy
	
	fi
elif [ $deploy_env == "rollback" ];then
	back
fi 

cat /server/scripts/deploy.sh

#!/bin/bash
CODE_DIR="/web-code"
get_code_dir="/var/lib/jenkins/workspace/freestyle-job"
IP="10.0.0.8"
tar_code(){
  tar zcf /tmp/web-${git_version}.tar.gz *
}
cp_code(){
  ssh $IP "mkdir /code/web-$git_version"
  scp /tmp/web-${git_version}.tar.gz aaa@qq.com$IP:/code/web-${git_version}/
}
xf_code(){
  ssh $IP "cd /code/web-$git_version && tar xf web-${git_version}.tar.gz && rm -f web-${git_version}.tar.gz"
}
ln_code(){
  ssh $IP "cd /code && rm -f html && ln -s web-$git_version html"
}
deploy(){
  tar_code
  cp_code
  xf_code
  ln_code
}
back(){
	back_version=$(ssh aaa@qq.com${IP} find /code/ -maxdepth 1 -type d -name "*-${git_version}")
	ssh aaa@qq.com${IP} "rm -f /code/html && \
                     ln -s ${back_version} /code/html"
}
if [ "$deploy_env" == "deploy" ];then
	if [ ${GIT_COMMIT} == ${GIT_PREVIOUS_SUCCESSFUL_COMMIT} ];then
  echo "该版本 ${git_version} 已部署"
  exit
	else
  deploy
	
	fi
elif [ $deploy_env == "rollback" ];then
	back
fi

vim java_deploy_tag.sh

#!/bin/bash
#host="10.0.0.8 10.0.0.7"
host="10.0.0.7"
DATE=$(date +%F_%H-%M)
Name=${DATE}-${git_version}
get_code() {
	cd ${WORKSPACE}
}
scp_web(){
for i in $host
do
	scp -r target/*.war aaa@qq.com$i:/opt/ROOT-${Name}.war
	ssh aaa@qq.com$i "mkdir -p /soft/tomcat/webapps/ROOT-${Name} && \
                     unzip /opt/ROOT-${Name}.war -d /soft/tomcat/webapps/ROOT-${Name} && \
                     rm -f /soft/tomcat/webapps/ROOT && \
                     ln -s /soft/tomcat/webapps/ROOT-${Name} /soft/tomcat/webapps/ROOT && \
                     /soft/tomcat/bin/shutdown.sh && \
                     sleep 3 && \
                     /soft/tomcat/bin/startup.sh"
done
}
deploy(){
	get_code
	scp_web
}
	deploy
#!/usr/bin/bash
host="192.168.0.186 192.168.0.188"
DATE=$(date +%F_%H-%M)
Name=${DATE}-${git_version}
get_code() {
	cd ${WORKSPACE}
}
scp_web(){
for i in $host
do
	scp -r target/*.war aaa@qq.com$i:/opt/ROOT-${Name}.war
	ssh aaa@qq.com$i "mkdir -p /code/tomcat/ROOT-${Name} && \
                     unzip /opt/ROOT-${Name}.war -d /code/tomcat/ROOT-${Name} && \
                     rm -f /soft/tomcat/webapps/ROOT && \
                     ln -s /code/tomcat/ROOT-${Name} /soft/tomcat/webapps/ROOT && \
                     pkill -9 java && \
                     sleep 3 && \
                     /soft/tomcat/bin/startup.sh"
done
}
deploy(){
	get_code
	scp_web
}
	deploy 

Jenkinsfile

pipeline{
  agent any
  stages{
    stage("get code"){
       steps{
            echo "get code"
       }
    }
    stage("Pre Steps"){
       steps{
            sh '/bin/bash /server/scripts/pre_build.sh'
       } 
    }
    stage("Build"){
        steps{
            sh '/usr/local/maven/bin/mvn -X clean package -Dmaven.test.skip=true -Ptest'
        }
    }
    stage("Post Steps"){
        steps{
            sh '/bin/bash /server/scripts/post_build.sh'
        }
    }
  }
}

jenkins

jenkins

jenkins

pipeline {
	//这个任务在哪个主机上运行
	agent any  
	
	//参数化构建,主要设定git_version变量的值
	parameters {
		string(name: 'git_version', defaultValue: 'v1.1', description: '选择你要部署的tag??')
    }
	
	stages {   //整个部署的任务
	
		stage ('获取代码'){
			steps {
        checkout([$class: 'GitSCM', branches: [[name: '${git_version}']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '93d22264-1975-4277-b895-c1b82c8f1af4', url: 'aaa@qq.com:root/monitor.git']]])
			}
		}
		stage ('代码质检'){
			steps {
				withSonarQubeEnv('sonarqube') { //sonarqube 服务器名称
					sh '/usr/local/sonar-scanner/bin/sonar-scanner -Dsonar.projectKey=${JOB_NAME} -Dsonar.sources=.'
				}
			}
		}
		stage ('代码构建') {
			steps {
				echo "构建成功"
				//sh 'mvn package -Dmaven.test.skip=true'		//java项目
			}
		}
		stage ('确认部署?') {
			steps {
				input message: '是否部署', ok: 'ok'
			}
		}
		stage('代码部署'){
			steps {
				echo '部署成功'
			}
		}
	}
}
pipeline {
	//这个任务在哪个主机上运行
	agent any  
	
	//参数化构建,主要设定git_version变量的值
	parameters {
		string(name: 'git_version', defaultValue: 'v1.1', description: '选择你要部署的tag??')
    }
	
	stages {   //整个部署的任务
	
		stage ('获取代码'){
			steps {
        checkout([$class: 'GitSCM', branches: [[name: '${git_version}']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '93d22264-1975-4277-b895-c1b82c8f1af4', url: 'aaa@qq.com:root/monitor.git']]])
			}
		}
		stage ('代码质检'){
			steps {
				withSonarQubeEnv('sonarqube') {
					sh '/usr/local/sonar-scanner/bin/sonar-scanner -Dsonar.projectKey=${JOB_NAME} -Dsonar.sources=.'
				}
			}
		}
		stage ('代码构建') {
			steps {
				echo "构建成功"
				//sh 'mvn package -Dmaven.test.skip=true'		//java项目
			}
		}
		stage ('确认部署?') {
			steps {
				input message: '是否部署', ok: 'ok'
			}
		}
		stage('代码部署'){
			steps {
				echo '部署成功'
			}
		}
	}
  
  post {  //stages所有任务执行后触发post
      failure {   //构建失败通知
          dingTalk accessToken: 'acad44677aba443bb486185c1636bf8538c8beaac4f8ad1a34ab2e949eb31d04', imageUrl: '', jenkinsUrl: 'http://10.0.0.201:8080/', message: '代码部署失败', notifyPeople: 'phone'
      } 
	
      success {   //构建成功通知
          dingTalk accessToken: 'acad44677aba443bb486185c1636bf8538c8beaac4f8ad1a34ab2e949eb31d04', imageUrl: '', jenkinsUrl: 'http://10.0.0.201:8080/', message: '代码部署成功', notifyPeople: 'phone'
      }
  }
}
pipeline {
	//这个任务在哪个主机上运行
	agent { label 'node1' }
	
	//参数化构建,主要设定git_version变量的值
	parameters {
		string(name: 'git_version', defaultValue: 'v1.1', description: '选择你要部署的tag??')
    }
	
	stages {   //整个部署的任务
	
		stage ('获取代码'){
			steps {
        checkout([$class: 'GitSCM', branches: [[name: '${git_version}']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '93d22264-1975-4277-b895-c1b82c8f1af4', url: 'aaa@qq.com:root/monitor.git']]])
			}
		}
		stage ('代码质检'){
			steps {
				withSonarQubeEnv('sonarqube') {
					sh '/usr/local/sonar-scanner/bin/sonar-scanner -Dsonar.projectKey=${JOB_NAME} -Dsonar.sources=.'
				}
			}
		}
		stage ('代码构建') {
			steps {
				echo "构建成功"
				//sh 'mvn package -Dmaven.test.skip=true'		//java项目
			}
		}
		stage ('确认部署?') {
			steps {
				input message: '是否部署', ok: 'ok'
			}
		}
		stage('代码部署'){
			steps {
				echo '部署成功'
			}
		}
	}
  
  post {  //stages所有任务执行后触发post
      failure {   //构建失败通知
          dingTalk accessToken: 'acad44677aba443bb486185c1636bf8538c8beaac4f8ad1a34ab2e949eb31d04', imageUrl: '', jenkinsUrl: 'http://10.0.0.201:8080/', message: '代码部署失败', notifyPeople: 'phone'
      } 
	
      success {   //构建成功通知
          dingTalk accessToken: 'acad44677aba443bb486185c1636bf8538c8beaac4f8ad1a34ab2e949eb31d04', imageUrl: '', jenkinsUrl: 'http://10.0.0.201:8080/', message: '代码部署成功', notifyPeople: 'phone'
      }
  }
}
pipeline {
	//这个任务在哪个主机上运行
	//agent any  
	
	//将这个项目运行在slave上
	agent { label 'node1' }
	
	//参数化构建,主要设定git_version变量的值
	parameters {
		string(name: 'git_version', defaultValue: 'v1.1', description: '选择你要部署的tag??')
    }
	
	stages {   //整个部署的任务
	
		stage ('获取代码'){
			steps {
				checkout([$class: 'GitSCM', branches: [[name: '${git_version}']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '7125a93c-b778-496c-a9c2-c0b4b43bfe5e', url: 'aaa@qq.com:dev/monitor.git']]])
			}
		}
		stage ('代码质检'){
			steps {
				withSonarQubeEnv('sonarqube') {
					sh '/usr/local/sonar-scanner/bin/sonar-scanner -Dsonar.projectKey=${JOB_NAME} -Dsonar.sources=.'
				}
			}
		}
		stage ('代码构建') {
			steps {
				echo "构建成功"
				//sh 'mvn package -Dmaven.test.skip=true'		//java项目
			}
		}
		stage ('确认部署?') {
			steps {
				input message: '是否部署', ok: 'ok'
			}
		}
		stage('代码部署'){
			steps {
				sh 'sh -x /scripts/html_deploy_tag.sh'
			}
		}
	}
	
	post {  //stages所有任务执行后触发post
        failure {   //构建失败通知
            dingTalk accessToken:'daf7d4843cc7d6a9612bf7a76fdc067b38e3927bba1788d2581713a4604d3169',imageUrl: '', jenkinsUrl: 'http://jenkins.oldbgx.com:8080/', message: '代码部署失败', notifyPeople: 'phone'
        } 
		
        success {   //构建成功通知
            dingTalk accessToken:'daf7d4843cc7d6a9612bf7a76fdc067b38e3927bba1788d2581713a4604d3169',imageUrl: '', jenkinsUrl: 'http://jenkins.oldbgx.com:8080/', message: '代码部署成功', notifyPeople: 'phone'
        }
    }
}

Jenkins分布式

jenkins

jenkins

jenkins

/usr/libexec/git-core/git


Jenkins权限管理

jenkins

jenkins

相关标签: jenkins