进行了一些研究之后,我想到了这个pipeline
,但这给出了一个错误。
我已经在Nexus
安全页面中配置了Jenkins
凭据。我正在尝试遵循适用于上述“构建”阶段的模式,但是没有运气。
我找到了此链接https://plugins.jenkins.io/nexus-artifact-uploader/,但这似乎是为了Nexus 2
。
给出编译错误。
pipeline {
agent any
stages {
stage('Build') {
steps {
withCredentials([usernamePassword(credentialsId: 'Bitbucket', usernameVariable: 'username', passwordVariable: 'password')]) {
git "https://${username}:${password}@bitbucket.org/${username}/greet.git"
}
sh "mvn clean deploy -DskipTests"
}
}
stage('Publish') {
steps {
step {
withCredentials([usernamePassword(credentialsId: 'Nexus', usernameVariable: 'username', passwordVariable: 'password')]) {
def pom = readMavenPom file: 'pom.xml'
nexusArtifactUploader(
nexusVersion: 'nexus3',
protocol: 'http',
nexusUrl: '${username}:${password}@my-nexus-server/nexus',
groupId: '${pom.groupId}',
version: '${pom.version}',
repository: 'maven-snapshots',
credentialsId: 'Nexus',
artifacts: [
[artifactId: '${pom.artifactId}',
classifier: '',
file: '${pom.artifactId}-${pom.version}.jar',
type: 'jar']
]
)
}
}
}
}
}
}
错误:
Started by user Admin
Obtained Jenkinsfile from git https://[email protected]/chandeln/greet.git
Running in Durability level: MAX_SURVIVABILITY
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 16: Expected a step @ line 16, column 25.
def pom = readMavenPom file: 'pom.xml'
^
WorkflowScript: 14: Missing required parameter: "delegate" @ line 14, column 17.
step {
^
2 errors
at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1085)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:603)
at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:581)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:558)
at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
at groovy.lang.GroovyShell.parseClass(GroovyShell.java:688)
at groovy.lang.GroovyShell.parse(GroovyShell.java:700)
at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.doParse(CpsGroovyShell.java:142)
at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:127)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:561)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:522)
at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:337)
at hudson.model.ResourceController.execute(ResourceController.java:97)
at hudson.model.Executor.run(Executor.java:428)
Finished: FAILURE
以下解决方案对我有用
pipeline {
agent any
environment {
// This can be nexus3 or nexus2
NEXUS_VERSION = "nexus3"
// This can be http or https
NEXUS_PROTOCOL = "http"
// Where your Nexus is running. 'nexus-3' is defined in the docker-compose file
NEXUS_URL = "my-nexus-server/nexus"
// Repository where we will upload the artifact
NEXUS_REPOSITORY = "maven-snapshots"
// Jenkins credential id to authenticate to Nexus OSS
NEXUS_CREDENTIAL_ID = "Nexus"
}
stages {
stage('Clone') {
steps {
withCredentials([usernamePassword(credentialsId: 'Bitbucket', usernameVariable: 'username', passwordVariable: 'password')]) {
git "https://${username}:${password}@bitbucket.org/${username}/greet.git"
}
}
}
stage("Build") {
steps {
script {
sh "mvn clean install -DskipTests=true"
}
}
}
stage('Publish') {
steps {
script {
// Read POM xml file using 'readMavenPom' step , this step 'readMavenPom' is included in: https://plugins.jenkins.io/pipeline-utility-steps
pom = readMavenPom file: "pom.xml";
// Find built artifact under target folder
filesByGlob = findFiles(glob: "target/*.${pom.packaging}");
// Print some info from the artifact found
echo "${filesByGlob[0].name} ${filesByGlob[0].path} ${filesByGlob[0].directory} ${filesByGlob[0].length} ${filesByGlob[0].lastModified}"
// Extract the path from the File found
artifactPath = filesByGlob[0].path;
// Assign to a boolean response verifying If the artifact name exists
artifactExists = fileExists artifactPath;
if(artifactExists) {
echo "*** File: ${artifactPath}, group: ${pom.groupId}, packaging: ${pom.packaging}, version ${pom.version}";
nexusArtifactUploader(
nexusVersion: NEXUS_VERSION,
protocol: NEXUS_PROTOCOL,
nexusUrl: NEXUS_URL,
groupId: pom.groupId,
version: pom.version,
repository: NEXUS_REPOSITORY,
credentialsId: NEXUS_CREDENTIAL_ID,
artifacts: [
// Artifact generated such as .jar, .ear and .war files.
[artifactId: pom.artifactId,
classifier: '',
file: artifactPath,
type: pom.packaging],
// Lets upload the pom.xml file for additional information for Transitive dependencies
[artifactId: pom.artifactId,
classifier: '',
file: "pom.xml",
type: "pom"]
]
);
} else {
error "*** File: ${artifactPath}, could not be found";
}
}
}
}
}
}