Jenkins pipeline for JFrog Artifactory fails to publish BuildInfo - jenkins

I have Jenkinsfile written in groovy as follows:
env.MVN_Goals = MVN_Goals
node {
// Get Artifactory server instance, defined in the Artifactory Plugin administration page.
def server = Artifactory.newServer url: 'http://localhost:8085/artifactory', username: 'admin', password: 'password'
// Create an Artifactory Maven instance.
def rtMaven = Artifactory.newMavenBuild()
stage ('Clone sources'){
git url: 'D:/Sample GIT_Maven Repo'
}
stage 'Artifactory configuration'
rtMaven.deployer releaseRepo:'libs-release-local', snapshotRepo:'libs-snapshot-local', server: server
rtMaven.resolver releaseRepo:'libs-release', snapshotRepo:'libs-snapshot', server: server
def buildInfo = Artifactory.newBuildInfo()
stage('Maven_Build'){
if (isUnix()) {
sh "D:/apache-maven-3.3.9/bin/mvn -B -Dmaven ${MVN_Goals}"
}
else{
bat "D:/apache-maven-3.3.9/bin/mvn -B -Dmaven ${MVN_Goals}"
}
step([$class: 'ArtifactArchiver', artifacts: '**/target/*.jar', fingerprint: true])
}
stage ('Publish build info'){
server.publishBuildInfo buildInfo
}
}
I tried configuring the Artifactory in Jenkins by adding the Artifactory plugin for Jenkins. When I tried to test the connection, I am getting an error as There is either an incompatible or no instance of Artifactory at the provided URL. The same error is occurring when i tried to build my job in Jenkins. Is there a way to resolve it?
Artifactory plugin version - 2.9.1
Artifactory Version - 4.15.0

def buildInfo = Artifactory.newBuildInfo() is within that particular stage.
Modify that as
env.MVN_Goals = MVN_Goals
node {
// Get Artifactory server instance,
// defined in the Artifactory Plugin administration page.
def server = Artifactory.newServer url: 'http://localhost:8085/artifactory', username: 'admin', password: 'password'
// Create an Artifactory Maven instance.
def rtMaven = Artifactory.newMavenBuild()
def buildInfo = Artifactory.newBuildInfo()
stage ('Clone sources'){
git url: 'D:/Sample GIT_Maven Repo'
}

Related

Unsupported OS - I am trying to run Docker push Buildinfo in Windows machine but its get failed for Unsupported OS

node("DevHub && WinServer2019"){
cleanWs()
stage ("in windows docker") {
withDockerRegistry(credentialsId: "ABC_Technical_User", url: "https://abc-swc-build-toolchains-docker-dev") {
dockerImage = docker.image("abc-swc-build-toolchains-docker-dev/ci-snapshot/mt4/windows_vc100:image-13-77d3d3b")
dockerImage.pull()
String rtDocker = ''
def server = Artifactory.newServer url: 'https://abc/artifactory/', credentialsId: 'ABC_Technical_User'
def buildInfo = Artifactory.newBuildInfo()
// Step 2: Create an Artifactory Docker instance:
def bdDocker = Artifactory.docker server: server
buildInfo localBuildInfo = bdDocker.pull 'abc-swc-build-toolchains-docker-dev/ci-snapshot/mt4/windows_vc100:base-image-13-77d3d3b', 'abc-swc-build-toolchains-docker-dev'
// Step 4: Publish the build-info to Artifactory:
buildInfo.append localBuildInfo
echo "buildInfo : ${buildInfo}"
server.publishBuildInfo buildInfo
}
}
}
I am getting below error
INFO: Pulling image: abc-swc-build-toolchains-docker-dev/ci-snapshot/mt4/windows_vc100:base-image-13-77d3d3b
**java.lang.RuntimeException: Unsupported OS**
at com.github.dockerjava.netty.NettyDockerCmdExecFactory$UnixDomainSocketInitializer.init(NettyDockerCmdExecFactory.java:147)
at com.github.dockerjava.netty.NettyDockerCmdExecFactory.init(NettyDockerCmdExecFactory.java:116)
at com.github.dockerjava.core.DockerClientImpl.withDockerCmdExecFactory(DockerClientImpl.java:193)
at com.github.dockerjava.core.DockerClientBuilder.build(DockerClientBuilder.java:45)
at org.jfrog.build.extractor.docker.DockerJavaWrapper.getDockerClient(DockerJavaWrapper.java:77)
at org.jfrog.build.extractor.docker.DockerJavaWrapper.pullImage(DockerJavaWrapper.java:150)
at org.jfrog.build.extractor.docker.extractor.DockerPull.execute(DockerPull.java:76)
at org.jfrog.build.extractor.packageManager.PackageManagerExtractor.executeAndSaveBuildInfo(PackageManagerExtractor.java:33)
at org.jfrog.build.extractor.docker.extractor.DockerPull.main(DockerPull.java:61)
java.lang.RuntimeException: docker build failed
Can someone help me on this?
Would it be possible for you to test the scenario with the latest Jenkins Artifactory plugin and Java 11? I remember a similar issue reported in the past due to a lower Java version/old plugin as well. Upgrading the plugin to the latest version along with the respective Java version mentioned, should help in resolving the issue.

Jenkins Artifactory Plugin

Can Jenkins Artifactory plugin run docker-in-docker? I would like to use this plugin in tandem with Jenkins Kubernetes plugin in a pipeline build provided below.
The example pipeline creates a Pod which firstly uses a git container to clone the given project-examples repository and then uses a maven container to build the project.
I suspect I am hitting this issue and just wanted to double check if it is still the case.
When executed, the Artifactory plugin searches for resources, e.g. MAVEN_HOME, in the Pod instead of the maven container. Not being able to find the resources the plugin fails.
def label = "worker-${UUID.randomUUID().toString()}"
podTemplate(label: label, containers: [
containerTemplate(name: 'maven', image: 'maven:3.3.9-jdk-8-alpine', ttyEnabled: true, command: 'cat'),
containerTemplate(name: 'git', image: 'alpine/git', command: 'cat', ttyEnabled: true)
]) {
node(label) {
container('maven') {
def server
def buildInfo
def rtMaven
stage ('Clone') {
git url: 'https://github.com/jfrogdev/project-examples.git'
}
stage ('Test a Maven project') {
server = Artifactory.server 'private-artifactory'
rtMaven = Artifactory.newMavenBuild()
rtMaven.tool = 'maven'
rtMaven.run pom: 'maven-example/pom.xml', goals: 'clean build', buildInfo: buildInfo
buildInfo = Artifactory.newBuildInfo()
}
}
I've had the same problem and been trying various variations on configuring the connection to the maven installation.
The final configuration that seems to be working in picking up the maven installation that I have in my docker image is as follows:
stage("build") {
def server=Artifactory.server('My art')
def rtMaven=Artifactory.newMavenBuild()
rtMaven.resolver server: server, releaseRepo: 'libs-release', snapshotRepo: 'libs-snapshot'
rtMaven.deployer server: server, releaseRepo: 'libs-release-local', snapshotRepo: 'libs-snapshot-local'
env.MAVEN_HOME="/usr/share/maven"
def buildInfo = rtMaven.run pom: 'sub-dir/pom.xml', goals: 'clean install'
server.publishBuildInfo buildInfo
}
Using env.MAVEN_HOME instead of relying on a tool configuration seems to work. Rembember to place it before rtMave.run.

Artifactory build options for maven build and issues

I am new to Artifactory and I find that there is a lot of documentation on the different ways to get artifacts to Artifactory but nothing that is complete.
For example:
The pipeline plugin used by jenkins:
sample code:
node {
def server = Artifactory.newServer url: SERVER_URL, credentialsId: CREDENTIALS
def rtMaven = Artifactory.newMavenBuild()
stage 'Build'
git url: 'https://github.com/jfrogdev/project-examples.git'
stage 'Artifactory configuration'
rtMaven.tool = MAVEN_TOOL // Tool name from Jenkins configuration
rtMaven.deployer releaseRepo:'libs-release-local', snapshotRepo:'libs-snapshot-local', server: server
rtMaven.resolver releaseRepo:'libs-release', snapshotRepo:'libs-snapshot', server: server
def buildInfo = Artifactory.newBuildInfo()
stage 'Exec Maven'
rtMaven.run pom: 'maven-example/pom.xml', goals: 'clean install', buildInfo: buildInfo
stage 'Publish build info'
server.publishBuildInfo buildInfo
}
I am not sure how to set up some variables like CREDENTIALS. specially if I want to use api key not user id and password.
Also, if I want to use the Artifactory rest API to build and promote my project(Maven Build). should I be using:
curl -X PUT "http://localhost:8080/artifactory/api/build" -H "Content-Type: application/json" --upload-file build.json
Where build.json is the sample JSON at https://www.jfrog.com/confluence/display/RTF/Artifactory+REST+API under Build Upload.
If i use the API do i still need the above jenkins plugin code or can just use the API?
Where do I pass my credentials (Userid and APi key) in the curl command?
If any experienced user could guide me with these questions that will be of great help.

Don't 'artifactoryPublish' when running Artifactory Jenkins Plugin

Is it possible to do a gradle.run (see below), without running the artifactoryPublish task? I thought I could accomplish this by specifying the tasks parameter, but the plugin appears to add the task back in. For example, the following:
def server = Artifactory.server('artifactory-primary')
def gradle = Artifactory.newGradleBuild()
gradle.resolver server: server, repo: 'gradle-all-virtual'
gradle.deployer server: server, repo: 'gradle-libs-snapshot-local'
gradle.deployer.mavenCompatible = true
gradle.useWrapper = true
gradle.usesPlugin = true
def buildInfo = gradle.run(
rootDir: ".",
buildFile: 'build.gradle',
tasks: 'build',
switches: '--no-daemon -x check')
server.publishBuildInfo buildInfo
Results in:
...
gradlew -x check build artifactoryPublish -b build.gradle
...
When what I really want is:
...
gradlew -x check build -b build.gradle
...
Ultimately I want to build in one stage and deploy in another.
The same snippet but with references to artifactory removed from my Gradle file and with Tamir's addition added in:
def server = Artifactory.server('artifactory-primary')
def gradle = Artifactory.newGradleBuild()
gradle.resolver server: server, repo: 'gradle-all-virtual'
gradle.deployer server: server, repo: 'gradle-libs-snapshot-local'
gradle.deployer.mavenCompatible = true
gradle.deployer.deployArtifacts = false
gradle.useWrapper = true
gradle.usesPlugin = false
def buildInfo = gradle.run(
rootDir: ".",
buildFile: 'build.gradle',
tasks: 'build',
switches: '--no-daemon -x check')
server.publishBuildInfo buildInfo
Produces the same result.
The artifactoryPublish task is added by default, you can see that in the Jenkins Artifactory plugin code.
If you prefere not to deploy artifacts to artifactory you can do so by configuring deployer.deployArtifacts = false.
In your case:
gradle.deployer.deployArtifacts = false
If you want to build you project in two phases you can once build it with deployArtifacts=false and in the second time to build it with deployArtifacts=true

Jenkins + Gradle + Artifactory: Couldn't read generated build info

I'm trying to push my artifacts to Artifactory with Jenkins Pipeline, which call Gradle tool.
I am following the examples published on GitHub:
Example1
Example2
My Jenkins Pipeline script:
stage('Perform Gradle Release') {
//ssh-agent required to perform GIT push (when tagging the branch on release)
sshagent([git_credential]) {
sh "./gradlew clean release unSnapshotVersion -Prelease.useAutomaticVersion=true -Prelease.releaseVersion=${release_version} -Prelease.newVersion=${development_version}"
}
// Create an Artifactory server instance
def server = Artifactory.server('my-artifactory')
// Create and set an Artifactory Gradle Build instance:
def rtGradle = Artifactory.newGradleBuild()
rtGradle.resolver server: server, repo: 'libs-release'
rtGradle.deployer server: server, repo: 'libs-release-local'
//Use Gradle Wrapper
rtGradle.useWrapper = true
//Creates buildinfo
def buildInfo = Artifactory.newBuildInfo()
buildInfo.env.capture = true
buildInfo.env.filter.addInclude("*")
// Run Gradle:
rtGradle.run rootDir: "./", buildFile: 'build.gradle', tasks: 'clean artifactoryPublish', buildInfo: buildInfo
// Publish the build-info to Artifactory:
server.publishBuildInfo buildInfo
}
My Gradle file is very light, I'm just using the plugin Gradle Release Plugin to perform gradle release.
When executing the pipeline, it fails with this message:
:artifactoryPublish
BUILD SUCCESSFUL
Total time: 17.451 secs
ERROR: Couldn't read generated build info at : /tmp/generated.build.info4898776990575217114.json
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
hudson.model.Run$RunnerAbortedException
at org.jfrog.hudson.pipeline.Utils.getGeneratedBuildInfo(Utils.java:188)
at org.jfrog.hudson.pipeline.steps.ArtifactoryGradleBuild$Execution.run(ArtifactoryGradleBuild.java:127)
at org.jfrog.hudson.pipeline.steps.ArtifactoryGradleBuild$Execution.run(ArtifactoryGradleBuild.java:96)
at org.jenkinsci.plugins.workflow.steps.AbstractSynchronousStepExecution.start(AbstractSynchronousStepExecution.java:40)
...
Finished: FAILURE
When I check on the server, there is no such file /tmp/generated.build.info4898776990575217114.json (the user has of course permission to write to /tmp).
Thanks for your help.
[EDIT] It is weird, but I found some files named "buildInfo2408849984051060030.properties", containing the informations. The name is not the same, neither the format, and these files are stores on my Jenkins machine, not my slave executing the pipeline.
Thanks #tamir-hadad, it has indeed been fixed on 2.8.2.

Resources