1.安装
官方地址,https://gradle.org/install/
官网:https://gradle.org/releases/ Gradle基于Groovy,具有更灵活更强大的构建系统,能帮助我们构建更复杂的项目
2.下载
The current Gradle release is 7.4.2
- 依赖环境
Gradle runs on all major operating systems and requires only a Java JDK version 8 or higher to be installed
$ java -version
java version "1.8.0_121"
$ java -version
java version "1.8.0_121"
- 下载gradle
$ mkdir /opt/gradle
$ unzip -d /opt/gradle gradle-7.4.2-bin.zip
$ ls /opt/gradle/gradle-7.4.2
LICENSE NOTICE bin getting-started.html init.d lib media
$ mkdir /opt/gradle
$ unzip -d /opt/gradle gradle-7.4.2-bin.zip
$ ls /opt/gradle/gradle-7.4.2
LICENSE NOTICE bin getting-started.html init.d lib media
- 配置环境变量
$vim /etc/profile
export PATH=$PATH:/opt/gradle/gradle-7.4.2/bin
source /etc/profile
$vim /etc/profile
export PATH=$PATH:/opt/gradle/gradle-7.4.2/bin
source /etc/profile
$ gradle -v
------------------------------------------------------------
Gradle 7.4.2
------------------------------------------------------------
$ gradle -v
------------------------------------------------------------
Gradle 7.4.2
------------------------------------------------------------
常用命令
gradle build 构建项目
gradle build -x test 构建项目跳过测试
gradle clean 清空构建目录
3.jenkins配置
- 系统设置->全局工具配置
- pipeline
stage ("gradlebuild"){
gradleHome = tool 'GRADLE'
sh "${gradleHome}/bin/gradle -v"
}
stage ("gradlebuild"){
gradleHome = tool 'GRADLE'
sh "${gradleHome}/bin/gradle -v"
}
- 案例
groovy
//gitlab传递的数据{}
println("${webhookdata}")
def buildTools = ["gradle": "/data/gradle-7.2"]
//数据格式化
webhookdata = readJSON text: "${webhookdata}"
//提取仓库信息
env.srcUrl= webhookdata["project"]["git_http_url"] //项目地址
env.branchName= webhookdata["ref"]-"refs/heads/" // 分支
env.commitId= webhookdata["checkout_sha"] //提交id
env.commitUser= webhookdata["user_username"] // 提交人
env.userEmail = webhookdata["user_email"] //邮箱
currentBuild.description = "Trigger by Gitlab \n branch: ${env.branchName} \n user: ${env.commitUser} "
currentBuild.displayName = "${env.commitId}"
pipeline{
//指定运行此流水线的节点,指定标签的名字
agent { node { label "build"}}
stages{
//阶段1 获取代码
stage("CheckOut"){
//定义阶段1的变量
environment {
NAME = "hwf01"
VERSION = "1.1.11"
ENVTYPE = "DEV1"
}
steps{
script{
//branchName = "${params.branchName}"
//srcUrl = "${params.srcUrl}"
checkout([$class: 'GitSCM', branches: [[name: "${env.branchName}"]],
extensions: [],
userRemoteConfigs: [[credentialsId: 'gitlab-root',
url: "${env.srcUrl}"]]])
}
}
}
stage("Build") {
steps {
script {
println("运行构建")
//要是想获取input的值,打印他的环境变量
sh "${buildTools["gradle"]}/bin/gradle clean && ${buildTools["gradle"]}/bin/gradle build -x test"
}
}
}
stage("Test"){
steps{
script {
println("运行测试")
sh "${buildTools["gradle"]}/bin/gradle test"
junit 'build/test-results/test/*.xml'
}
}
}
}
post {
always{
script{
println("流水线结束后,经常做的事情")
//EmailUser("${env.userEmail}", currentBuild.currentResult)
}
}
success{
script{
println("流水线成功后,要做的事情")
}
}
failure{
script{
println("流水线失败后,要做的事情")
}
}
aborted{
script{
println("流水线取消后,要做的事情")
}
}
}
}
//gitlab传递的数据{}
println("${webhookdata}")
def buildTools = ["gradle": "/data/gradle-7.2"]
//数据格式化
webhookdata = readJSON text: "${webhookdata}"
//提取仓库信息
env.srcUrl= webhookdata["project"]["git_http_url"] //项目地址
env.branchName= webhookdata["ref"]-"refs/heads/" // 分支
env.commitId= webhookdata["checkout_sha"] //提交id
env.commitUser= webhookdata["user_username"] // 提交人
env.userEmail = webhookdata["user_email"] //邮箱
currentBuild.description = "Trigger by Gitlab \n branch: ${env.branchName} \n user: ${env.commitUser} "
currentBuild.displayName = "${env.commitId}"
pipeline{
//指定运行此流水线的节点,指定标签的名字
agent { node { label "build"}}
stages{
//阶段1 获取代码
stage("CheckOut"){
//定义阶段1的变量
environment {
NAME = "hwf01"
VERSION = "1.1.11"
ENVTYPE = "DEV1"
}
steps{
script{
//branchName = "${params.branchName}"
//srcUrl = "${params.srcUrl}"
checkout([$class: 'GitSCM', branches: [[name: "${env.branchName}"]],
extensions: [],
userRemoteConfigs: [[credentialsId: 'gitlab-root',
url: "${env.srcUrl}"]]])
}
}
}
stage("Build") {
steps {
script {
println("运行构建")
//要是想获取input的值,打印他的环境变量
sh "${buildTools["gradle"]}/bin/gradle clean && ${buildTools["gradle"]}/bin/gradle build -x test"
}
}
}
stage("Test"){
steps{
script {
println("运行测试")
sh "${buildTools["gradle"]}/bin/gradle test"
junit 'build/test-results/test/*.xml'
}
}
}
}
post {
always{
script{
println("流水线结束后,经常做的事情")
//EmailUser("${env.userEmail}", currentBuild.currentResult)
}
}
success{
script{
println("流水线成功后,要做的事情")
}
}
failure{
script{
println("流水线失败后,要做的事情")
}
}
aborted{
script{
println("流水线取消后,要做的事情")
}
}
}
}