[TOC]
## Jenkins流水线任务实现
### 参数化构建
添加参数化构建,方便选择不的项目版本
| Git参数化构建 |
| :----------------------------------------------------------: |
| ![](https://img.kancloud.cn/75/24/75244d40cb1d960fc916042c71ba2635_1920x849.png) |
### 拉取Git代码
通过流水线语法生成Checkout代码的脚本
| 语法生成 |
| :----------------------------------------------------------: |
| ![](https://img.kancloud.cn/20/ec/20ec2e7c424b0e184bba913ff0b7b3c7_1920x855.png) |
![](https://img.kancloud.cn/8b/4e/8b4e3d8c7b058718a62f142039d7aa35_1920x882.png)
将*/master更改为标签`${tag}`
```sh
pipeline {
agent any
stages {
stage('拉取Git代码') {
steps {
checkout([$class: 'GitSCM', branches: [[name: '${tag}']], extensions: [], userRemoteConfigs: [[url: 'http://49.233.115.171:8929/root/test.git']]])
}
}
}
}
```
### 构建代码
通过脚本执行mvn的构建命令
```sh
pipeline {
agent any
stages {
stage('拉取Git代码') {
steps {
checkout([$class: 'GitSCM', branches: [[name: '${tag}']], extensions: [], userRemoteConfigs: [[url: 'http://49.233.115.171:8929/root/test.git']]])
}
}
stage('构建代码') {
steps {
sh '/var/jenkins_home/maven/bin/mvn clean package -DskipTests'
}
}
}
```
### 代码质量检测
通过脚本执行sonar-scanner命令即可
```sh
pipeline {
agent any
stages {
stage('拉取Git代码') {
steps {
checkout([$class: 'GitSCM', branches: [[name: '${tag}']], extensions: [], userRemoteConfigs: [[url: 'http://49.233.115.171:8929/root/test.git']]])
}
}
stage('构建代码') {
steps {
sh '/var/jenkins_home/maven/bin/mvn clean package -DskipTests'
}
}
stage('检测代码质量') {
steps {
sh '/var/jenkins_home/sonar-scanner/bin/sonar-scanner -Dsonar.sources=./ -Dsonar.projectname=${JOB_NAME} -Dsonar.projectKey=${JOB_NAME} -Dsonar.java.binaries=target/ -Dsonar.login=31388be45653876c1f51ec02f0d478e2d9d0e1fa'
}
}
}
}
```
### 制作自定义镜像并发布
- 生成自定义镜像脚本
```sh
pipeline {
agent any
environment{
harborHost = '192.168.11.11:80'
harborRepo = 'repository'
harborUser = 'DevOps'
harborPasswd = 'P@ssw0rd'
}
// 存放所有任务的合集
stages {
stage('拉取Git代码') {
steps {
checkout([$class: 'GitSCM', branches: [[name: '${tag}']], extensions: [], userRemoteConfigs: [[url: 'http://49.233.115.171:8929/root/test.git']]])
}
}
stage('构建代码') {
steps {
sh '/var/jenkins_home/maven/bin/mvn clean package -DskipTests'
}
}
stage('检测代码质量') {
steps {
sh '/var/jenkins_home/sonar-scanner/bin/sonar-scanner -Dsonar.sources=./ -Dsonar.projectname=${JOB_NAME} -Dsonar.projectKey=${JOB_NAME} -Dsonar.java.binaries=target/ -Dsonar.login=31388be45653876c1f51ec02f0d478e2d9d0e1fa'
}
}
stage('制作自定义镜像并发布Harbor') {
steps {
sh '''cp ./target/*.jar ./docker/
cd ./docker
docker build -t ${JOB_NAME}:${tag} ./'''
sh '''docker login -u ${harborUser} -p ${harborPasswd} ${harborHost}
docker tag ${JOB_NAME}:${tag} ${harborHost}/${harborRepo}/${JOB_NAME}:${tag}
docker push ${harborHost}/${harborRepo}/${JOB_NAME}:${tag}'''
}
}
}
}
```
- 生成Publish Over SSH脚本
```sh
pipeline {
agent any
environment{
harborHost = '192.168.11.11:80'
harborRepo = 'repository'
harborUser = 'DevOps'
harborPasswd = 'P@ssw0rd'
}
// 存放所有任务的合集
stages {
stage('拉取Git代码') {
steps {
checkout([$class: 'GitSCM', branches: [[name: '${tag}']], extensions: [], userRemoteConfigs: [[url: 'http://49.233.115.171:8929/root/test.git']]])
}
}
stage('构建代码') {
steps {
sh '/var/jenkins_home/maven/bin/mvn clean package -DskipTests'
}
}docker
stage('检测代码质量') {
steps {
sh '/var/jenkins_home/sonar-scanner/bin/sonar-scanner -Dsonar.sources=./ -Dsonar.projectname=${JOB_NAME} -Dsonar.projectKey=${JOB_NAME} -Dsonar.java.binaries=target/ -Dsonar.login=7d66af4b39cfe4f52ac0a915d4c9d5c513207098'
}
}
stage('制作自定义镜像并发布Harbor') {
steps {
sh '''cp ./target/*.jar ./docker/
cd ./docker
docker build -t ${JOB_NAME}:${tag} ./'''
sh '''docker login -u ${harborUser} -p ${harborPasswd} ${harborHost}
docker tag ${JOB_NAME}:${tag} ${harborHost}/${harborRepo}/${JOB_NAME}:${tag}
docker push ${harborHost}/${harborRepo}/${JOB_NAME}:${tag}'''
}
}
stage('目标服务器拉取镜像并运行') {
steps {
sshPublisher(publishers: [sshPublisherDesc(configName: 'testEnvironment', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: "/usr/bin/deploy.sh $harborHost $harborRepo $JOB_NAME $tag $port ", execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
}
}
}
}
```
[Ps:由于采用变量,记得使用双引号](
)
- DevOps介绍
- 持续集成流程
- git工作流实践
- 新人准备git环境
- git工作流最佳实践
- jenkins基础入门
- jenkins简介
- jenkins安装(docker)
- 源码拉取
- maven构建
- 应用发布
- 结合gitlab版本管理
- jenkins-docker部署
- harbor镜像发布
- jenkins集成SonarQube
- SonarQube代码检测
- 集成SonarQube
- jenkins-pipeline
- 构建pipeline任务
- pipeline基础结构
- pipeline核心语法
- jenkinsfile构建
- 完整案例
- 整合钉钉发送通知
- 脚本使用凭证信息
- jenkins结合k8s使用
- k8s架构及基础概念
- k8s操作命令
- jenkins部署k8s
- 附录(其他未完成)
- jenkins高可用服务搭建
- gitlab-docker环境搭建
- 开发人员安装git
- JDK与maven
- docker安装gitlab
- docker安装
- docker-compose安装
- harbor安装
- SonarQube安装
- 2.暂时没写的内容
- 构建结果邮件发送
- 已有项目上传gitlab