Thomas W.
Thomas W.

Reputation: 51

Generating Artifactory build-info from Maven invocation

Overall goal:

I am currently looking for a reasonable approach to handle automatic CI builds for Maven SNAPSHOT artifacts of (short-lived) feature branches. There should be traceability for all produced artifacts (JIRA => Git => Jenkins build => Artifact + build-info in Artifactory).

Current setup:

Jenkins with jobs to build the master branches of all involved projects that get triggered by web hooks and no CI builds for feature branches.

We have a lot of broken builds after merging into master. I agree with you that it is the responsibility of the PR creator to make sure the branch compiles and the (integration) tests pass and the responsibility of the assignee to pull the branch and test everything locally before merging to master but unfortunately reality is different. Futhermore, as you all know, SNAPSHOT versions are not reproducible and if two devs work on the same version number they will overwrite each other's artifacts.

We are using Artifactory Professional 4.7.5 as repository manager.

Target setup:

I setup a Jenkins 2 instance and setup an organization in GitHub Enterprise that can be scanned easily by the GitHub organization folder plugin. All the job configuration has been moved to Jenkinsfiles. Whenever a feature branch is pushed it will trigger a build and the result will be visible in the GitHub Enterprise Web GUI. Still, that doesn't solve the problem with SNAPSHOTs of feature branches not being isolated from each other.

I did some research and found the maven-branch-extension which binds to the deploy phase of the Maven build. I setup a new feature branch repository with a custom layout and could successfully deploy artifacts to that repository by invoking sh 'mvn clean deploy -Pfeature -Dgit.branch=${env.BRANCH_NAME} (the git.branch argument is required because we are in detached HEAD state when using the Jenkins pipeline scm step and thus the plugin cannot infer the branch name).

Problem:

We don't get the build-info that is generated when using the Jenkins Artifactory plugin. The Jenkins Artifactory plugin binds to the install phase so the maven-branch-extension doesn't kick in.

I tried adding a separate step after the build stage:

stage('Publish build-info') {
    def artifactoryServer = Artifactory.server('myrepo.mycompany.se')
    def buildInfo = Artifactory.newBuildInfo()
    buildInfo.env.capture = true
    buildInfo.env.collect()
    def target = (env.BRANCH_NAME != 'master') ? "myproject-snapshot-feature-local/${env.BRANCH_NAME}/" : "myproject-snapshot-local/"

    def uploadSpec = """{
        "files": [{
            "pattern": "target/(.*).(jar|war|ear)",
            "target": "${target}",
            "recursive": "false",
            "regexp": "true"
        }]

    }"""

    artifactoryServer.upload(uploadSpec)
    artifactoryServer.publishBuildInfo(buildInfo)
}

This will publish some build-info to Artifactory but it will also upload artifacts again.

I wonder if it's possible to generate the metadata at all if the build has not been performed by the Jenkins Artifactory plugin.

Also, if you think my approach with a separate feature branch repository is not a good solution in the first place, please let me know.

Upvotes: 2

Views: 2174

Answers (2)

Agent86
Agent86

Reputation: 424

This is old, but I am doing something similar to this now and ran across the quesitons.

Have you tried to use Maven CI firendly versions? I am just trying this for a project and they look promisiing.

in my case I default revision to my version (e.g. 1.0.0) and changelist to "-SNAPSHOT". The pom version is set to ${rivision}${changelist}, so my local builds produce regular SNAPSHOT builds, but in jenkins I can build and set changelist to ".${timestamp}".

Something similar to this should work for you.

Upvotes: 0

Tamir Hadad
Tamir Hadad

Reputation: 501

You can do maven build using Artifactory Jenkins plugin instead of running maven and then uploading the files using the generic upload DSL.

For more usage examples you can look at JFrog jenkins-pipeline-examples.

Upvotes: 2

Related Questions