I use Jenkins to build my project, once that is done I upload the directory with all artifact files or just a single file to JFrog.
Pipeline-code example:
def server = Artifactory.server "jfrog1"
sh "touch uploadconfig.json"
writeFile(file: "uploadconfig.json", text: "{ \"files\": [ { \"pattern\": \"./uploadconfig.json\", \"target\": \"test-generic-local/uploadconfig1.json\", \"flat\" : \"true\" }]}")
uploadSpec = readFile 'uploadconfig.json'
uploadInfo = server.upload spec: uploadSpec
When the file is uploaded it has the build.name and build.number property.
But my issue is it will not show up under Artifactory -> Builds.
This is an issue since I want to have the option to download the latest file from this build using:
"files": [
{
"pattern": "test-generic-local/uploadconfig*",
"target": "./",
"build" : "nameOfMyJenkinsJob/LATEST"
}
]
When I try to run it now without the artifacts being listed under Build I get the following error message:
The build name nameOfMyJenkinsJob could not be found.
I see that you are not publishing the build to Artifactory. Kindly refer to this JFrog wiki on how to publish the build info. Also, I would recommend you referring this Github Jenkins file where in the build publish stage is where the build is pushed.
Related
I am trying to write a pipeline script to publish *.war/*.jar file to JFrogArtifactory. I don't find any syntax for the same.
Anyone can help me out on the same.
please help me with a sample script.
JFrog has a dedicated GitHub repository with many examples for such cases.
There are Jenkins Pipelines examples there.
First, you must install Artifactory Plugin and config it in Jenkins server.
Refer: https://www.jfrog.com/confluence/display/JFROG/Configuring+Jenkins+Artifactory+Plug-in
And then try add below script to Jenkinsfile:
script {
def server = Artifactory.server '<artifactory id>'
def uploadSpec = '''{
"files": [{
"pattern": "<name of war or jar file>",
"target": "<artifactory repo>/path-to/war-or-jar/file/in-Artifactory"
}]
}'''
server.upload(uploadSpec)
}
Don't forget replace <artifactory id> <name of war or jar file> and <artifactory repo>/path-to/war-or-jar/file/in-Artifactory
More information: https://www.jfrog.com/confluence/display/JFROG/Declarative+Pipeline+Syntax
The scripted pipeline syntax for deploying war files to JFrog artifactory is :
env.ARTIFACTORY = 'True'
if(env.ARTIFACTORY == 'True')
{
stage('Deploying to Artifactory')
{
FAILED_STAGE = env.STAGE_NAME
bat 'mvn deploy'
}
}
Note :
1.) 'bat' command is for Windows batch file. If you're using Linux, replace 'bat' with 'sh'
2.) env.ARTIFACTORY is used to give you control over whether or not you want to execute this particular stage in your pipeline job. if you don't want this stage to execute, simply set env.ARTIFACTORY = 'False'
3.) Also note, you've to configure JFrog in : Manage Jenkins -> Configure system -> JFrog Platform Instances
4.) Include JFrog in your pom.xml file under distributionManagement tag.
I need my Jenkins build to fail in case zero artifacts are uploaded into the artifactory.
I have included failNoOp into my upload spec but it seems it's not working in my Jenkinsfile. Here is my code:
def server = Artifactory.newServer url: env.ArtifactoryServerTEST, credentialsId:'ArtifactoryUATServerKey'
server.bypassProxy = true
def uploadSpec = """{
"files": [
{
"pattern": "final_artifacts/*.gz",
"target": "test/"
}
]
}"""
server.upload spec: uploadSpec, failNoOp: true
This needs to fail my build if none of the artifacts are uploaded into the artifactory, but it is giving me this error:
java.lang.IllegalArgumentException: Only the following arguments are
allowed, [spec, buildInfo]
I'm using Artifactory OSS Version 6.2.0. anyone can help me on this?
As #yahavi mentioned, I could fix this problem by upgrade the artifactory plugin. I updated to the latest, so as at now current latest version is 3.3.2.
I am able to integrate jenkins with artifactory to store my artifacts whenever a build is carried out, however whenever new artifacts are being pushed, the older versions will be removed automatically.
So I will like to check is there anyway for me to label each artifacts according to the version number or build number for easy version control.
Be sure to include the Jenkins ${env.BUILD_NUMBER} in the artifact path and name.
Assuming your Artifactory repository is set up as a Maven repository your pipeline should include something like this:
def uploadSpec = """{
"files": [
{
"pattern": "${env.PACKAGING_FOLDER}/${env.REPOSITORY_APPLICATION}-${env.BUILD_NUMBER}.zip",
"target": "${env.REPOSITORY_CODE}/${env.REPOSITORY_GROUP}/${env.REPOSITORY_APPLICATION}/${env.BUILD_NUMBER}/${env.REPOSITORY_APPLICATION}-${env.BUILD_NUMBER}.zip",
"regexp": "true"
}
]
}"""
def buildInfo = Artifactory.newBuildInfo()
buildInfo.env.capture = true
buildInfo = server.upload(uploadSpec)
server.publishBuildInfo(buildInfo)
I have a Jenkins job which generate a zip file I want to updload to Artifactory. I have an issue setting the version of the artifact to be uploaded.
By convention, I use the timestamp has version. I want to upload file to my/group/timestamp/file.zip. The url of the file would be http://ArtifactoryAdress/foo/my/group/timestamp/file.zip
Here is my pipeline code
def serverArtifactory = Artifactory.server 'NameArtificatory'
def uploadSpec = """{
"files": [
{
"pattern": "file.zip",
"target": "my/group/${timestamp}/"
}
]
}"""
serverArtifactory.upload(uploadSpec)
I get the following error from Jenkins Job
java.lang.RuntimeException: java.io.IOException: Failed to deploy
file. Status code: 400 Response message: Artifactory returned the
following errors:
Parent my/group/timestampValue must be a folder Status code: 400
I looked around buildInfo but was not able to find how to set a version.
By the way, I am also agree with a solution without the timestamp but only group name.
Finally, this error is clear and simple.
As mentioned, a file with path my/group/timestampValue already exists. You have to delete it on Artifactory.
Don't forget it's still groovy use ${}. I used below code and it's work
def uploadSpec = """{
"files": [
{
"pattern": "**/target/*.war",
"target": "local-release/${APP_REPO}/${version.trim()}/${timestamp}.zip"
}
]}"""
server.upload(uploadSpec)
#Edit. And I just thought about it. Print please your ${timestamp}. Maybe it's contains characters with spaces, or something like this which Artifactory not allowed in directory name. Try trim your timestamp.trim()
I'm trying to set up a jenkins pipeline for publishing a zip file to jfrog artifactory.
I am using com.jfrog.artifactory plugin to do so. This works great from command line gradle and I can run the artifactoryPublish task to publish the artifacts and tie them back to the module, which then has a tie back to the artifacts.
The artifacts show up with the properties:
build.name = `projectname`
build.number = `some large number`
And I can click from them to the build/module and back to the artifact.
However, when I run this from a jenkinsfile pipeline, the artifacts get published and get tied back to the module, but then the module does not successfully tie the module back to the artifacts.
The artifacts do not receives the build.name and build.number properties and i cannot click from the module back to the artifacts, as the module cannot find or resolve the paths back to the artifacts(a zip file and a generated pom).
I am passing the params from jenkins like:
ORG_GRADLE_PROJECT_buildInfo.build.number=${env.BUILD_NUMBER} which seems to work on other projects... but for whatever reason I cannot shake it.
I can include more jenkinsfile if that would help debug, but i'm really just checking out a repository and trying to publish it.
I have been reading heavily the documentation here:
https://www.jfrog.com/confluence/display/RTF/Gradle+Artifactory+Plugin
and haven't been able to make it work through -Pproject stuff.
Does anyone have any idea what else I can try? i don't really want to use the jenkins pipeline artifactory plugin directly because it's so nice to be able to deploy from the command line too.
build.gradle:
publishing {
publications {
ManualUpdaterPackage(MavenPublication){
artifact assembleManualUpdaterPackage
}
}
}
artifactory {
contextUrl = "${artifactoryUrl}" //The base Artifactory URL if not overridden by the publisher/resolver
publish {
defaults {
publications('ManualUpdaterPackage')
}
repository {
repoKey = project.version.endsWith('-SNAPSHOT') ? snapshotRepo : releaseRepo
username = "${artifactory_user}"
password = "${artifactory_password}"
maven = true
}
}
}
task assembleManualUpdaterPackage (type: Zip){
dependsOn anotherTask
from (packageDir + "/")
include '**'
// archiveName "manualUpdaterPackage-${version}.zip"
destinationDir(file(manualUpdaterZipDir))
}
jenkinsfile snip:
withCredentials(
[
[
$class : 'UsernamePasswordMultiBinding',
credentialsId : 'validcreds',
passwordVariable: 'ORG_GRADLE_PROJECT_artifactory_password',
usernameVariable: 'ORG_GRADLE_PROJECT_artifactory_user'
]
]
) {
withEnv(
[
"ORG_GRADLE_PROJECT_buildInfo.build.number=${env.BUILD_NUMBER}",
"ORG_GRADLE_PROJECT_buildInfo.build.name=${artifactName}",
"ORG_GRADLE_PROJECT_buildInfo.build.url=${env.JOB_URL}"
]
) {
sh 'chmod +x gradlew'
sh "./gradlew --no-daemon clean artifactoryPublish"
}
}
https://www.jfrog.com/confluence/display/RTF/Working+With+Pipeline+Jobs+in+Jenkins#WorkingWithPipelineJobsinJenkins-GradleBuildswithArtifactory
Eventually my coworker recommended looking into the Artifactory Pipeline Gradle plugin instead. It is very nice to work with and we've had much quicker success with it.