更改 Jenkins 共享库的 maven repo
到目前为止,我只使用了共享库 pom.xml 中公共中央 maven 存储库中可用的 java 库,但现在我想使用部署在我们私有 nexus 中的 java 库,但我一直收到错误:
unable to resolve class com.somecompany.SomeClass
经过一番挖掘,我找到了这个文档: https://www.jenkins.io/doc/book/pipeline/shared-libraries/
我的问题是,我根本不想使用 maven 中央仓库,但出于安全考虑,我想使用我的 nexus 作为中央 maven 仓库的镜像仓库。
我已经在使用 mvn 命令从管道构建工件时使用了 maven settings.xml,我想知道是否有任何方法可以在共享库中执行相同操作?
###################### UPDATED-1 ###################
共享库中的我的 Groovy 类:
import com.somecompany.collections.secret.vault.VaultClient
Map getSecrets() {
VaultClient client = new VaultClient()
Map test = client.getAllSecrets()
return test
}
我的 pom.xml
<dependencies>
<dependency>
<groupId>com.somecompany</groupId>
<artifactId>vault-secret-management</artifactId>
<version>${vault.secret.management}</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-ecr</artifactId>
</dependency>
最后是我的管道:
@GrabResolver(name='restlet', root='https://nexus.us.somecompany.com/repository/asa-coll-release-maven-hosted/')
@Grab(group='com.somecomapny', module='vault-secret-management', version='1.0.20')
@Library('jenkins-libraries@feature/DM-27548-try-to-use-vault-client-in-shared-library')
String projectName = '6562_GL_CYBERFINANCIAL_IIT_IAC'
pipeline {
agent {
label 'ecs-node'
}
environment {
VAULT_ADDR = 'https://vault.uat.use1.crypto.aws.efx/'
VAULT_NAMESPACE = 'intl-collections/dev-use1'
VAULT_SECRETS_PATH = 'kv-admin-console/admin-console'
VAULT_MAX_RETRIES = 3
VAULT_RETRY_INTERVAL = 10
VAULT_ROLE = 'efx-cloud-ec2-developer-provisioner-role'
}
stages {
stage('Hello') {
steps {
script {
def secret = vaultHelper.getSecrets()
println("result: "+secret)
}
}
}
}
}
这是错误:
14:46:30 [Pipeline] // node
14:46:30 [Pipeline] End of Pipeline
14:46:30 org.jenkinsci.plugins.workflow.cps.CpsCompilationErrorsException: startup failed:
14:46:30 /mnt/jenkins-ebs/jenkins/jobs/cxs349-test-vault/builds/13/libs/jenkins-libraries/vars/vaultHelper.groovy: 1: unable to resolve class com.equifax.collections.secret.vault.VaultClient
14:46:30 @ line 1, column 1.
14:46:30 import com.equifax.collections.secret.vault.VaultClient
14:46:30 ^
14:46:30
14:46:30 1 error
14:46:30
14:46:30 at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
14:46:30 at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:958)
14:46:30 at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:605)
14:46:30 at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:554)
14:46:30 at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
14:46:30 at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
14:46:30 at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:254)
14:46:30 at groovy.lang.GroovyClassLoader.recompile(GroovyClassLoader.java:761)
14:46:30 at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:718)
14:46:30 at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:787)
14:46:30 at java.lang.ClassLoader.loadClass(ClassLoader.java:405)
14:46:30 at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell$TimingLoader.loadClass(CpsGroovyShell.java:170)
14:46:30 at java.lang.ClassLoader.loadClass(ClassLoader.java:405)
14:46:30 at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:677)
14:46:30 at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:787)
14:46:30 at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:775)
14:46:30 at org.jenkinsci.plugins.workflow.cps.global.UserDefinedGlobalVariable.getValue(UserDefinedGlobalVariable.java:57)
14:46:30 at org.jenkinsci.plugins.workflow.cps.CpsScript.getProperty(CpsScript.java:135)
14:46:30 at org.codehaus.groovy.runtime.InvokerHelper.getProperty(InvokerHelper.java:174)
14:46:30 at groovy.lang.Closure.getPropertyTryThese(Closure.java:312)
14:46:30 at groovy.lang.Closure.getPropertyDelegateFirst(Closure.java:302)
14:46:30 at groovy.lang.Closure.getProperty(Closure.java:287)
14:46:30 at org.codehaus.groovy.runtime.InvokerHelper.getProperty(InvokerHelper.java:174)
14:46:30 at groovy.lang.Closure.getPropertyTryThese(Closure.java:312)
14:46:30 at groovy.lang.Closure.getPropertyOwnerFirst(Closure.java:306)
14:46:30 at groovy.lang.Closure.getProperty(Closure.java:295)
14:46:30 at org.codehaus.groovy.runtime.InvokerHelper.getProperty(InvokerHelper.java:174)
14:46:30 at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.getProperty(ScriptBytecodeAdapter.java:456)
14:46:30 at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.getProperty(DefaultInvoker.java:39)
14:46:30 at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20)
14:46:30 at WorkflowScript.run(WorkflowScript:29)
14:46:30 at ___cps.transform___(Native Method)
14:46:30 at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.get(PropertyishBlock.java:74)
14:46:30 at com.cloudbees.groovy.cps.LValueBlock$GetAdapter.receive(LValueBlock.java:30)
14:46:30 at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.fixName(PropertyishBlock.java:66)
14:46:30 at sun.reflect.GeneratedMethodAccessor202.invoke(Unknown Source)
14:46:30 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
14:46:30 at java.lang.reflect.Method.invoke(Method.java:498)
14:46:30 at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
14:46:30 at com.cloudbees.groovy.cps.impl.ConstantBlock.eval(ConstantBlock.java:21)
14:46:30 at com.cloudbees.groovy.cps.Next.step(Next.java:83)
14:46:30 at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:174)
14:46:30 at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:163)
14:46:30 at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:129)
14:46:30 at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:268)
14:46:30 at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163)
14:46:30 at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:185)
14:46:30 at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:402)
14:46:30 at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$400(CpsThreadGroup.java:96)
14:46:30 at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:314)
14:46:30 at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:278)
14:46:30 at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:67)
14:46:30 at java.util.concurrent.FutureTask.run(FutureTask.java:266)
14:46:30 at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:136)
14:46:30 at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
14:46:30 at jenkins.security.ImpersonatingExecutorService$1.run(ImpersonatingExecutorService.java:59)
14:46:30 at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
14:46:30 at java.util.concurrent.FutureTask.run(FutureTask.java:266)
14:46:30 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
14:46:30 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
14:46:30 at java.lang.Thread.run(Thread.java:748)
14:46:30 Finished: FAILURE
##################### UPDATE-2 (已解决)###########################
将 @GrabResolver 和 @Grab 移至 Shared-Library 内的实际 groovy 代码中而不是 Jenkins 管道中即可解决问题:)
import com.equifax.collections.secret.vault.VaultClient
@groovy.lang.GrabResolver(name='restlet', root='https://nexus.us.equifax.com/repository/asa-coll-release-maven-hosted/')
@groovy.lang.Grab(group='com.equifax', module='vault-secret-management', version='1.0.20')
Map getSecrets() {
VaultClient client = VaultClient.getInstance();
Map test = client.getAllSecrets()
return test
}
首先,您需要一个 settings.xml 文件,该文件指定您的存储库(链接到您的 nexus 实例)。我假设您在本地开发环境中有这样一个文件
之后,您需要将 settings.xml 上传到 jenkins,具体说明请参见 此处
您还应该考虑在您的 nexus 上使用一个新用户,其名称类似于“jenkins”,而不是使用您自己的登录名,因为 Jenkins 正在访问您的 Nexus 存储库。
已解决
我专注于使用 Maven 来加载我的共享库的依赖项,到目前为止,所有依赖项都在 maven 中央存储库中。
然后我查看了 Grab 文档,但我在管道内部使用了
@GrabResolver
和
@Grab
,这是错误的(请参阅我在 UPDATE-1 中发布的代码)
将
@GrabResolver
和
@Grab
移动到共享库内的实际 groovy 代码即可解决问题,请参阅我原始问题中的 UPDATE-2