Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Jenkinsfile for upload maven artifact to Artifactory

I've my .Jenkinsfile like this:

properties([[$class: 'GitLabConnectionProperty', gitLabConnection: 'gitlab@srv']])
node {
  env.JAVA_HOME = tool 'JDK 7'
  def mvnHome = tool 'Maven 3.2.2'
  def nodeJS = tool 'IA_NodeJS'
  env.PATH = "${mvnHome}/bin:${nodeJS}/bin:${env.JAVA_HOME}/bin:${env.PATH}"

  stage ('checkout') {
    checkout scm
  }

  stage ('build') {
    gitlabCommitStatus("build") {
      // your build steps
      sh 'mvn clean install -Denv=dev -P !faster'
    }
  }

  stage ('upload') {
    gitlabCommitStatus("upload") {
      def server = Artifactory.server "artifactory@ibsrv02"
      def buildInfo = Artifactory.newBuildInfo()
      buildInfo.env.capture = true
      buildInfo.env.collect()

      def uploadSpec = """{
        "files": [
          {
            "pattern": "**/target/*.jar",
            "target": "libs-snapshot-local"
          }, {
            "pattern": "**/target/*.pom",
            "target": "libs-snapshot-local"
          }, {
            "pattern": "**/target/*.war",
            "target": "libs-snapshot-local"
          }
        ]
      }"""
      // Upload to Artifactory.
      server.upload spec: uploadSpec, buildInfo: buildInfo

      buildInfo.retention maxBuilds: 10, maxDays: 7, deleteBuildArtifacts: true
      // Publish build info.
      server.publishBuildInfo buildInfo
    }
  }
}

with this method jenkins uploads artifacts without make the "maven's style" layout (packages subfolder and poms).

I want to upload resulting artifact to Artifactory like a normal job uploads it with "Maven3-Artifactory Integration" checked.

like image 323
TeoMatthew Avatar asked Sep 09 '16 12:09

TeoMatthew


People also ask

How do I pull Artifactory files from Jenkins?

For example, to pull Artifactory every ten minutes, set */10 * * * * 4. Set a Path to watch. For example, when setting generic-libs-local/builds/starship, Jenkins polls the /builds/starship folder under the generic-libs-local repository in Artifactory for new or modified files.

What Jenkins build jobs does the Jenkins Artifactory plugin support?

The Jenkins Artifactory Plugin supports the following Jenkins Build Jobs: JFrog Pipelines integration with Jenkins is supported since version 1.6 of JFrog Piplines and version 3.7.0 of the Jenkins Artifactory Plugin. This integration allows triggering a Jenkins job from JFrog Pipelines.

How do I deploy Maven Artifactory artifacts?

There's a Maven Artifactory plugin that will connect to the Artifactory repository as part of a successful build. Simply issue a Maven deploy command as part of the build, and if the Maven plugin is configured correctly, the artifact it produces will be uploaded.

What is Jfrog's Jenkins Artifactory plugin for DevOps?

For organizations in pursuit of DevOps enlightenment, it's the CI/CD build that should be pushing packaged artifacts to an Artifactory repository. Fortunately, JFrog provides a Jenkins Artifactory plugin that facilitates automation of the process.


1 Answers

From Artifactory Jenkins plugin version 2.7.2 you can run Maven and Gradle using Artifactory pipeline DSL.

Using the new DSL your build script would look like this:

  def server = Artifactory.server "artifactory@ibsrv02"
  def buildInfo = Artifactory.newBuildInfo()
  buildInfo.env.capture = true
  def rtMaven = Artifactory.newMavenBuild()
  rtMaven.tool = MAVEN_TOOL // Tool name from Jenkins configuration
  rtMaven.opts = "-Denv=dev"
  rtMaven.deployer releaseRepo:'libs-release-local', snapshotRepo:'libs-snapshot-local', server: server
  rtMaven.resolver releaseRepo:'libs-release', snapshotRepo:'libs-snapshot', server: server

  rtMaven.run pom: 'pom.xml', goals: 'clean install', buildInfo: buildInfo

  buildInfo.retention maxBuilds: 10, maxDays: 7, deleteBuildArtifacts: true
  // Publish build info.
  server.publishBuildInfo buildInfo

You can find more Artifactory pipeline DSL examples in the jenkins-pipeline-examples.

like image 73
Tamir Hadad Avatar answered Sep 23 '22 10:09

Tamir Hadad