How can you deploy a specific artifact from Jenkins into Nexus?

后端 未结 4 2022
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-02 02:51

I have a multi-module maven project running in Jenkins. I would like to deploy the final artifact (an RPM from an assembly build) to the Nexus server. I see no reason to d

相关标签:
4条回答
  • 2020-12-02 03:02

    Here's the ugly hack I came up with. I'll gladly give someone else the "correct" answer for this if anyone has a better idea:

    I realized that I need to deploy both the parent pom.xml and the assembly. I did this in two separate post build steps.

    First, I chose "Invoke top-level Maven targets" with a Maven Version of "Maven" (I think this uses Jenkins version of maven. I don't want to put a different version on the system). I used Goals of:

    -s svn-admin/settings.xml -N deploy
    

    That deploys just the parent pom to nexus with my specified settings.xml.

    The REALLY big hack happens when I want to deploy the rpm. I tried a "deploy-file" target, but without a variable I could expand to the version number, I couldn't specify the exact file and wildcards don't expand. Instead I did an "Execute shell" option and used curl I found here:

    env
    UPLOAD_FILE=assembly/target/ips-${POM_VERSION}-x.x86_64.rpm
    DESTINATION=http://mvnrepo01/nexus/content/repositories/releases/com/bla/ips/assembly/${POM_VERSION}/assembly-${POM_VERSION}.rpm
    sha1sum ${UPLOAD_FILE} | awk -F" " '{print $1}' | curl -v -u admin:password --upload-file - ${DESTINATION}.sha1
    md5sum ${UPLOAD_FILE} | awk -F" " '{print $1}' | curl -v -u admin:password --upload-file - ${DESTINATION}.md5
    curl -v -u admin:password --upload-file ${UPLOAD_FILE} ${DESTINATION}
    
    UPLOAD_FILE=assembly/pom.xml
    DESTINATION=http://mvnrepo01/nexus/content/repositories/releases/com/bla/ips/assembly/${POM_VERSION}/assembly-${POM_VERSION}.pom
    sha1sum ${UPLOAD_FILE} | awk -F" " '{print $1}' | curl -v -u admin:password --upload-file - ${DESTINATION}.sha1
    md5sum ${UPLOAD_FILE} | awk -F" " '{print $1}' | curl -v -u admin:password --upload-file - ${DESTINATION}.md5
    curl -v -u admin:password --upload-file ${UPLOAD_FILE} ${DESTINATION}
    

    Like i said, this is an ugly hack. I'm pretty sure there are metadata files that aren't getting updated, but the rpm, it's pom, and their checksums are getting uploaded.

    0 讨论(0)
  • 2020-12-02 03:08

    You can use Nexus Jenkins Plugin to deploy a specific artifact from Jenkins into Nexus: https://support.sonatype.com/hc/en-us/articles/227256688-How-do-I-configure-the-Nexus-Jenkins-Plugin

    Example of Jenkins pipeline:

    stage('Publish') {
        def pom = readMavenPom file: 'pom.xml'
        nexusPublisher nexusInstanceId: 'your-nexus-instance-id', \
            nexusRepositoryId: 'your-nexus-repository-id', \
            packages: [[$class: 'MavenPackage', \
            mavenAssetList: [[classifier: '', extension: '', filePath: "target/${pom.artifactId}-${pom.version}.${pom.packaging}"]], \
            mavenCoordinate: [artifactId: "${pom.artifactId}", \
            groupId: "${pom.groupId}", \
            packaging: "${pom.packaging}", \
            version: "${pom.version}"]]]
    }
    

    In this case Nexus Jenkins Plugin will deploy only your target/${pom.artifactId}-${pom.version}.${pom.packaging} and pom.xml files to Nexus repository.

    0 讨论(0)
  • 2020-12-02 03:15

    If you need still generic way.

    Use Execute shell option and use the mvn deploy command manually, You can pass your version and others things such as groupID etc as parameters in job, If you maintain separate job to build and upload this will work out.

    Ex:

    export M2_HOME=/PATH/TO/softwares/apache-maven-3.0.4 PATH=$M2_HOME/bin:$JAVA_HOME/bin:$PATH export PATH

    mvn -v

    mvn deploy:deploy-file -Durl=http://someorg:8081/nexus/content/repositories/t1.snapshot/ -DrepositoryId=t1.snapshot -DartifactId=artifactID -DgroupId=groupID -Dpackaging=zip -Dfile=${WORKSPACE}/filename.zip -Dversion=1.0-TEST-SNAPSHOT -s "/path/to/.m2/settings.xml"

    0 讨论(0)
  • 2020-12-02 03:16

    You can make use of 'Deploy artifacts to Maven Repository' under 'Post-Build Actions'. Take look at this answer

    0 讨论(0)
提交回复
热议问题