美文网首页
Jenkins pipeline环境下gradle publis

Jenkins pipeline环境下gradle publis

作者: 伊丽莎白菜 | 来源:发表于2022-05-05 21:22 被阅读0次

〇、问题背景

当发生代码合并,触发jenkins流水线,构建成功后将制品发布到maven私服,非常简单的需求。

一、编写过程

1.1 build.gradle

查阅了gradle官方文档: https://docs.gradle.org/current/userguide/publishing_maven.html

我照着抄,相关配置如下:

plugins {
    id 'java'
    id 'maven-publish'
}

group = 'com.*.*'
// 构建时动态传入版本号
version = project.hasProperty('versionWithCommit') ? project.getProperty('versionWithCommit') : "0.0.5-SNAPSHOT"
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
    maven {
        url "https://maven.demo.cn/repository/maven-releases/"
        credentials {
            username = project.findProperty("mavenUsername")
            password = project.findProperty("mavenPassword")
        }
    }
}

/**
    省略无关配置
**/

test {
    useJUnitPlatform()
}

publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java
        }
    }
    repositories {
        maven {
            url = "https://maven.demo.cn/repository/maven-releases/"
            // 用户名密码从jenkins凭据获取
            credentials {
                username = project.findProperty("mavenUsername")
                password = project.findProperty("mavenPassword")
            }
        }
    }
}

1.2 jenkins pipeline

基本也有现成的可以抄,有效脚本如下:

  /**
        省略更新代码等无关配置
  **/    
  // 执行测试用例并打包
  stage('Build With Test') {
      steps {
        script {
            env.version = getVersion()
            withCredentials([usernamePassword
            (
                          credentialsId: 'maven-credencials',
                          usernameVariable: 'username',
                          passwordVariable: 'password'
            )]){
            sh '''
            docker run --rm --add-host=127.0.0.1   \
            --net=host -P \
            -v $PWD/src:/src \
            -v /home/ubuntu/jenkins-data/gradle/.gradle:/home/gradle/.gradle  \
            -v /home/ubuntu/jenkins-data/maven/.m2:/root/.m2 \
            -w /src/adaptor/sdk/adaptor gradle:jdk14 \
            gradle \
            -PversionWithCommit=$version \
            -i --stacktrace --watch-fs \
            -Dorg.gradle.vfs.verbose=true clean test build --no-build-cache -PbuildNumber=$BUILD_NUMBER  -PmavenUsername=$username -PmavenPassword=$password
            '''}
            junit testResults: 'src/**/build/test-results/test/TEST-*.xml',allowEmptyResults: true
            junit testResults: 'src/**/**/build/test-results/test/TEST-*.xml',allowEmptyResults: true
        }
      }
    }
    
    stage('Deploy Artifact') {
      steps {
        script {
            env.version = getVersion()
            withCredentials([usernamePassword
            (
                          credentialsId: 'maven-credencials',
                          usernameVariable: 'username',
                          passwordVariable: 'password'
            )]){
            sh '''
            docker run --rm --add-host=maven.demo.cn:161.189.43.115   \
            --net=host -P \
            -v $PWD/src:/src \
            -v /home/ubuntu/jenkins-data/gradle/.gradle:/home/gradle/.gradle  \
            -v /home/ubuntu/jenkins-data/maven/.m2:/root/.m2 \
            -w /src/adaptor/sdk/adaptor gradle:jdk14 \
            gradle \
            -Dorg.gradle.internal.publish.checksums.insecure=true \
            -PversionWithCommit=$version \
            -i publish --stacktrace --info \
            -PbuildNumber=$BUILD_NUMBER  \
            -PmavenUsername=$username \
            -PmavenPassword=$password
            echo 'adaptor jars in $PWD/src/adaptor/sdk/adaptor/build/libs:'
            ls -al $PWD/src/adaptor/sdk/adaptor/build/libs 
            '''}
        }
      }
    }

def getVersion(){
        def buildVersion = readFile(file: 'build.version')
    def version = String.format("%s.%s",buildVersion, env.BUILD_NUMBER)
    println('version:' + env.version)
    return version
}

二、现象及问题

2.1 现象

简单随意,半小时搞定,git commit & git push,运行一个试试。

结果都是绿的,看起来没啥问题,jenkins 控制台输出如下:

> Task :generateMetadataFileForMavenJavaPublication
Caching disabled for task ':generateMetadataFileForMavenJavaPublication' because:
  Build cache is disabled
Task ':generateMetadataFileForMavenJavaPublication' is not up-to-date because:
  Task.upToDateWhen is false.
:generateMetadataFileForMavenJavaPublication (Thread[Execution worker for ':' Thread 12,5,main]) completed. Took 0.049 secs.
:generatePomFileForMavenJavaPublication (Thread[Execution worker for ':' Thread 12,5,main]) started.

> Task :generatePomFileForMavenJavaPublication
Caching disabled for task ':generatePomFileForMavenJavaPublication' because:
  Build cache is disabled
Task ':generatePomFileForMavenJavaPublication' is not up-to-date because:
  Task.upToDateWhen is false.
:generatePomFileForMavenJavaPublication (Thread[Execution worker for ':' Thread 12,5,main]) completed. Took 0.018 secs.
:publishMavenJavaPublicationToMavenRepository (Thread[Execution worker for ':' Thread 12,5,main]) started.

> Task :publishMavenJavaPublicationToMavenRepository
Caching disabled for task ':publishMavenJavaPublicationToMavenRepository' because:
  Build cache is disabled
Task ':publishMavenJavaPublicationToMavenRepository' is not up-to-date because:
  Task has not declared any outputs despite executing actions.
Publishing to repository 'maven' (https://maven.demo.cn/repository/maven-releases/)
Uploading adaptor-0.0.70.jar to /repository/maven-releases/com/demo/adaptor/0.0.70/adaptor-0.0.70.jar
Uploading adaptor-0.0.70.pom to /repository/maven-releases/com/demo/adaptor/0.0.70/adaptor-0.0.70.pom
Uploading adaptor-0.0.70.module to /repository/maven-releases/com/demo/adaptor/0.0.70/adaptor-0.0.70.module
Uploading maven-metadata.xml to /repository/maven-releases/com/demo/adaptor/maven-metadata.xml
:publishMavenJavaPublicationToMavenRepository (Thread[Execution worker for ':' Thread 12,5,main]) completed. Took 1.611 secs.
:publish (Thread[Execution worker for ':' Thread 12,5,main]) started.

> Task :publish
Skipping task ':publish' as it has no actions.
:publish (Thread[Execution worker for ':' Thread 12,5,main]) completed. Took 0.0 secs.

BUILD SUCCESSFUL in 4s
5 actionable tasks: 3 executed, 2 up-to-date
[Pipeline] }
[Pipeline] // withCredentials
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

但是当我登录nexus maven私服看了之后,我傻眼了,根本没看到我传的包啊😅

2.2 尝试修复

我觉得问题大概出在这里:

> Task :publish
Skipping task ':publish' as it has no actions.

关键的publish任务被skip了,虽然前面的publishMavenJavaPublicationToMavenRepository看起来很正常,但总觉得活没干完。

我在百度、必应、google遨游了两天,尝试过各种奇奇怪怪地修改,还问过我大哥能不能改成maven项目...

最后实在搞不定了,五一假期抱了个大腿,大腿帮我看了一个小时,说:

我觉得你应该已经传成功了,但你nexus账号没权限看...

What? nexus还有这么精细的权限控制?正经功能不优化,净做些花里胡哨的,这么坑人吗😅

三、结论

开工后找权限更高的同事帮忙看了,果然是我nexus没有读权限,nexus的读权限能精确到maven坐标的文件夹🤡

相关文章

网友评论

      本文标题:Jenkins pipeline环境下gradle publis

      本文链接:https://www.haomeiwen.com/subject/ndvoyrtx.html