4.1.1、gitlab安装优化等
略
4.1.2、jenkins配置
4.1.3、gitlab仓库创建
4.1.3.1、创建共享库
目录
jenkinslib/src/org/devops/build.groovy
=================================================================================================
package org.devops
//构建类型
//传入参数构建类型,构建命令
def Build(buildType,buildShell){
//页面传入的buildType匹配mvn、ant、gradle、npm
//m2、ant、Gradle、NPM为Global Tool Configuration配置变量名称
def buildTools=["mvn":"m2","ant":"ant","gradle":"Gradle","npm":"NPM"]
println("当前选择的构建类型为 ${buildType}")
buildHome = tool buildTools[buildType]
sh "${buildHome}/bin/${buildType} ${buildShell}"
}
4.1.3.2、创建代码库
代码文件如下:
simple-java-maven-app-master.zip
4.1.3.3、jenkins配置
srcUrl
http://39.105.58.61:9999/root/demo-maven-service.git
branchName
master
buildType
mvn
buildShell
clean package
4.1.3.4、jenkinsfile配置
4.1.3.4.1、gitlab共享库jenkinsfile
4.1.3.4.2、jenkins流水线语法脚本生产
http://39.105.58.61:9999/root/jenkinslib.git
凭证认证
*/master
Jenkinsfile
4.1.3.4.3、变量替换
checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'gitlabpwd', url: 'http://39.105.58.61:9999/root/demo-maven-service.git']]])
替换
checkout([$class: 'GitSCM', branches: [[name: "${branchName}"]], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'gitlabpwd', url: "${srcUrl}"]]])
4.1.3.4.4、jenkinsfile脚本
#!groovy
//引用Library库
@Library('jenkinslib@master') _
//引用构建共享库
def build = new org.devops.build()
//引入发布共享库
def deploy = new org.devops.deploy()
//引入参数
String srcUrl = "${env.srcUrl}" //引用参数
String branchName = "${env.branchName}" //引用参数
String buildType = "${env.buildType}" //引用参数
String buildShell = "${env.buildShell}" //引用参数
pipeline {
agent {
node {
label "master" //构建主机
}
}
stages {
//下载代码
stage("CheckOut") {
steps {
script {
checkout([$class: 'GitSCM', branches: [[name: "${branchName}"]], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'gitlabpwd', url: "${srcUrl}"]]])
}
}
}
stage("Build") {
steps {
script {
//引入构建共享库内函数
build.Build(buildType,buildShell)
//引用发布共享库内函数
}
}
}
}
}
4.1.3.5、maven更改阿里云镜像源
http://dblab.xmu.edu.cn/blog/2758/
cd /usr/local/maven/conf
vim settings.xml
<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">
<mirrors>
<mirror>
<id>aliyunmaven</id>
<mirrorOf>*</mirrorOf>
<name>阿里云公共仓库</name>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>
<mirror>
<id>aliyunmaven</id>
<mirrorOf>*</mirrorOf>
<name>阿里云谷歌仓库</name>
<url>https://maven.aliyun.com/repository/google</url>
</mirror>
<mirror>
<id>aliyunmaven</id>
<mirrorOf>*</mirrorOf>
<name>阿里云阿帕奇仓库</name>
<url>https://maven.aliyun.com/repository/apache-snapshots</url>
</mirror>
<mirror>
<id>aliyunmaven</id>
<mirrorOf>*</mirrorOf>
<name>阿里云spring仓库</name>
<url>https://maven.aliyun.com/repository/spring</url>
</mirror>
<mirror>
<id>aliyunmaven</id>
<mirrorOf>*</mirrorOf>
<name>阿里云spring插件仓库</name>
<url>https://maven.aliyun.com/repository/spring-plugin</url>
</mirror>
</mirrors>
</settings>