Не удалось развернуть артефакты на Nexus 3 через конвейер Jenkins - PullRequest
0 голосов
/ 17 апреля 2020

После некоторого исследования я придумал это 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://chandeln@bitbucket.org/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

1 Ответ

0 голосов
/ 17 апреля 2020

Следующее решение сработало для меня

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";
                    }
                }
            }
        }
    }
}
...