在jenkins管道脚本中获取gradle变量 [英] Get gradle variables in jenkins pipeline script

查看:1146
本文介绍了在jenkins管道脚本中获取gradle变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用groovy构建脚本将我的构建管道迁移到管道插件。



我的管道通常是:


  1. 测试(gradle)

  2. IntegrationTest(gradle)

  3. 发布(artifactory)

我想使用gradle变量,比如version / group等我在jenkins构建脚本发布到artifactory中的正确文件夹。在过去,某些插件插件会照顾我。如何实现这一点?



对于单个gradle项目,我使用如下所示的内容:

  node('master')
{
def version = 1.0
def gitUrl ='some.git'
def projectRoot =
def group =dashboard / frontend /
def artifactName =dashboard_ui
def artifactRepo =ext-release-local

stagegit

git分支:'develop',poll:true,url:$ {gitUrl}

dir(projectRoot)
{

sh'chmod + x gradlew'
stage'test'
sh'./gradlew clean test'

stagebuild
sh'./gradlew build createPom'

stageartifact
def server = Artifactory.server('artifactory_dev01')
def uploadSpec ={
files:[
{
pattern:build / **。jar,
target:$ {artifactRepo} / $ group /${artifactName}/${version}/${artifactName}-${version}.jar

{
pattern:pom.xml,
target:$ {artifactRepo} / $ group / $ {artifactName} / $ {version} / $ {artifactName} .pom
}
]
}
def buildInfo1 = server.upload spec:uploadSpec
server.publishBuildInfo buildInfo1
}
}


解决方案

我认为你实际上有两种不同的方法来解决这个问题:

从sh脚本获取版本/组



找到一种从gradle构建工具获取Gradle版本的方法(例如 gradle getVersion(),但我不熟悉Gradle),然后使用shell脚本来获取此版本。如果Gradle命令获取版本是 gradle getVersion(),您可以在您的管道中执行:

  def projectVersion = sh脚本:gradle getVersion(),returnStdout:true 
def projectGroup = sh脚本:gradle getGroup(),returnStdout:true

然后只需注入 $ projectVersion $ projectGroup 变量在您当前的管道中。



2。配置你的Gradle构建脚本发布到Artifactory



这是相反的方法,我个人喜欢:不是给Artifactory所有的Gradle项目信息,而是给Gradle你的Artifactory设置并使用Gradle目标轻松发布到Artifactory。

JFrog在这个解决方案的使用Gradle 部分。基本上,您将按照以下步骤操作:


  1. 使用 Gradle构建脚本生成器从Artifactory生成符合的Gradle构建脚本并将其包含到您的项目构建脚本中

  2. 使用Gradle目标 gradle artifactoryPublish 来发布您当前的工件到Artifactory


I'm trying to migrate my build pipelines to the "Pipeline plugin" using the groovy build scripts.

My pipelines are usually:

  1. Test (gradle)
  2. IntegrationTest (gradle)
  3. Build (gradle)
  4. Publish (artifactory)

I would like to use the gradle variables like version/group etc. in my jenkins build script to publish to the correct folders in artifactory. Something the artifactory plugin would take care of for me in the past. How can this be achieved?

For a single gradle project I use something like this:

node('master')
{
    def version = 1.0
    def gitUrl = 'some.git'
    def projectRoot = ""
    def group = "dashboard/frontend/"
    def artifactName = "dashboard_ui"
    def artifactRepo = "ext-release-local"

    stage "git"

    git branch: 'develop', poll: true, url: "${gitUrl}"

    dir(projectRoot)
    {

        sh 'chmod +x gradlew'
        stage "test"
        sh './gradlew clean test'

        stage "build"
        sh './gradlew build createPom'

        stage "artifact"
        def server = Artifactory.server('artifactory_dev01')
        def uploadSpec = """{
          "files": [
            {
              "pattern": "build/**.jar",
              "target": "${artifactRepo}/$group/${artifactName}/${version}/${artifactName}-${version}.jar"
            },
            {
              "pattern": "pom.xml",
              "target": "${artifactRepo}/$group/${artifactName}/${version}/${artifactName}.pom"
            }
         ]
        }"""
        def buildInfo1 = server.upload spec: uploadSpec
        server.publishBuildInfo buildInfo1
    }
}

解决方案

I think you actually have two different approaches to tackle this problem :

1. Get version/group from sh script

Find a way to get Gradle version from gradle build tool (e.g. gradle getVersion(), but I'm not familiar with Gradle) and then use shell script to get this version. If Gradle command to get the version is gradle getVersion(), you would do in your pipeline :

def projectVersion = sh script: "gradle getVersion()", returnStdout: true
def projectGroup= sh script: "gradle getGroup()", returnStdout: true

and then just inject your $projectVersion and $projectGroup variables in your current pipeline.

2. Configure your Gradle build script to publish to Artifactory

This is the reverse approach, which I personnaly prefer : instead of giving Artifactory all your Gradle project information, juste give Gradle your Artifactory settings and use Gradle goal to easily publish to Artifactory.

JFrog has a good documentation for this solution in their Working with Gradle section. Basically, you will follow the following steps :

  1. Generate a compliant Gradle build script from Artifactory using Gradle Build Script Generator and include it to your project build script
  2. Use Gradle goal gradle artifactoryPublish to simply publish your current artifact to Artifactory

这篇关于在jenkins管道脚本中获取gradle变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆