Old Build not deleting from Artifactory with jenkinfile - jenkins

I am trying to run jenkins with code to remove old build from artifactory ,i only need to keep last 10 builds in artifactory however the builds are not getting deleted frm artifacty` below is the snip of pipeline using
stage("Artifactory Upload") {
def directory = "generic-local/ABC/XYZ/${params['version']}/${currDate}/${buildNo}/"
def server = Artifactory.server 'art-int'
def buildInfo = Artifactory.newBuildInfo()
buildInfo.retention maxBuilds: 10, deleteBuildArtifacts: true
def upload_spec_bin = """{
"files": [
{
"pattern": "*",
"target": "${directory}",
"exclusions": ["*.txt"]
}
]
}"""
server.upload spec: upload_spec_bin
server.publishBuildInfo buildInfo
}

Related

Jenkins/groovy CPS issue : artifactoryUpload

I'm getting same issue listed as fixed here : https://issues.jenkins.io/browse/JENKINS-58643
We are using Jenkins 2.190.3.2
stage('upload artefactory') {
steps {
sh "touch /tmp/blabla"
sh "gzip /tmp/blabla"
script {
server = Artifactory.server('myid')
server.credentialsId = 'my-cred'
def uploadSpec = """{
"files": [
{
"pattern": "/tmp/blabla.gz",
"target": "pkg/com/myentreprise/mystuff/scm/dumps/solr/"
}
]
}"""
server.upload spec: uploadSpec, failNoOp: true
}
}
}
[Pipeline] artifactoryUpload
expected to call org.jfrog.hudson.pipeline.common.types.ArtifactoryServer.upload but wound up catching artifactoryUpload; see: https://jenkins.io/redirect/pipeline-cps-method-mismatches/
this was fixed in Pipeline Groovy 2.75
We have 2.74, thus why we still ahve this bug.
Only solution seems to be to upgrade.

Jenkins wait for artifact download to finish

So I am downloading multiple artifacts with jFrog
rtDownload (
serverId: 'Artifactory-1',
spec: '''{
"files": [
{
"pattern": "bazinga-repo/froggy-files/",
"target": "bazinga/"
}
]
}''',
// Optional - Associate the downloaded files with the following custom build name and build number,
// as build dependencies.
// If not set, the files will be associated with the default build name and build number (i.e the
// the Jenkins job name and number).
buildName: 'holyFrog',
buildNumber: '42'
)
Which works but this works async and I have to use the results as soon as it finished. How do I await for each rtDownload in pipeline syntax?
Below working for me with downloading 2 Artifacts :
def target = params.BuildInfo.trim()
def downloadSpec = """{
"files": [{
"pattern": "${artifactory}.zip",
"target": "./${target}.zip"
}]
}"""
def buildInfo = server.download spec: downloadSpec
def files = findFiles(glob: "**/${target}.zip") // Define `files` here
if (files) { // And verify `it` here, so it'll wait
...
}
Personally, I ended up implementing Khoa's idea this way
try {
// attempt to retreive the results from artifactory
rtDownload (
serverId: 'my_arti_server',
spec: """{
"files": [
{
"pattern": "somepath/run_*.zip",
"target": "run/"
}
]
}""",
failNoOp: false, // no failure if no file was found
)
// rtDownload is async, we have to wait for dl completion
def count = 5
while(count > 0) {
sh script:"""#!/bin/bash +e
chmod 777 run/run_*.zip
"""
def files = findFiles glob: "run/run_*.zip"
if (files.length > 0 ){
break
}
sleep(5)
count--
}
} catch (Exception e) {
echo 'Exception occurred: ' + e.toString()
}
def files = findFiles glob: "run/run_*.zip"
if (files.length == 0 ){
error("files couldn't be found")
}
This is not perfect but it waits for some files to be present. If you have only one file it should work but if you have several files, it may continue as soon as one file is downloaded. I haven't checked but with this I assume that:
a file can be found once the download is completed (no file size changing regularly)
all files are downloaded "at the same time"

Jenkins-Groovy - Jfrog Artifactory - getting exception while downloading artifacts Calculated MD5 checksum is different from original

I am working with Jenkins - Groovy Script, uploading and downloading artifacts to and from jfrog artifactory.
Below is the code for upload and download zip folder.
stage("Upload Artifact to Jfrog"){
def server = Artifactory.server 'JfrogTAArtifactory'
def uploadSpec = """{
"files": [
{
"pattern": "${env.WORKSPACE}\\${artifactsfoldername}\\API-${env.BUILD_NUMBER}.zip",
"target": "internal-repo/folderName/subFolder/",
"props": "type=zip;status=ready"
}
]
}"""
def buildInfo = server.upload(uploadSpec)
server.publishBuildInfo(buildInfo)
}
stage("Download Artifacts"){
def server = Artifactory.server 'JfrogTAArtifactory'
File folder = new File(downloadArtifactsPath)
FileUtils.cleanDirectory(folder)
def downloadSpec = """{
"files": [
{
"pattern": "internal-repo/folderName/subFolder/API-${env.BUILD_NUMBER}.zip",
"target": "${downloadArtifactsPath}/",
"explode":"true",
"flat": "true"
}
]
}"""
def buildInfo = server.download(downloadSpec)
server.publishBuildInfo(buildInfo)
}
When I try to download I am getting the following exception
java.io.IOException: Calculated MD5 checksum is different from original, Original: '09bb61772231822ebd37a2751c818f60' Calculated: '7e32b3f7e8149fbc8013fd7992f345e5'
at org.jfrog.build.extractor.clientConfiguration.util.DependenciesDownloaderHelper.validateMd5Checksum(DependenciesDownloaderHelper.java:439)
Can any one help me?. I am new to both jenkins and Jfrog
This works for me:
// deploy/upload sublog file in job workspace to artifactory
// using jenkins artifactory plugin
def server = Artifactory.server 'ABCD'
server.username = 'XXXX'
server.password = 'YYYY'
// repo has name jenkinsbuilds/{job}/{build}
def buildnumber = env.BUILD_NUMBER
def jobname = env.JOB_NAME
def targetspec = 'jenkinsbuilds/' + jobname + '/' + buildnumber + '/'
def uploadSpec = '{"files": [{"pattern": "sublog","target": "' + targetspec + '"}]}'
def buildinfo = server.upload uploadSpec
server.publishBuildInfo buildinfo
Note no brackets around 'buildinfo' for server.publishBuildInfo call

Can I publish build info when when pushing a npm .tgz to Artifactory?

How can I publish build info when when pushing a npm registry to Artifactory?
I can do it with Maven using these steps
def rtMaven = Artifactory.newMavenBuild()
def buildInfo = rtMaven.run pom: 'maven-example/pom.xml', goals: 'clean install'
Currently I am just using npm publish
But I would like to have Builds info for my tgz files. Is it possible?
Thanks!
here a sample pipeline code doing simple build.
The only thouble with this script is that i can't figure out how to fill dependencies informations.
node {
stage('Cleaning') {
deleteDir()
}
stage('Preparing') { // for display purposes
// Get some code from Git
git credentialsId: 'MyGitCredential', url: 'http://gitRepourl/MyGitProject.git'
}
stage('Fetch Dependencies') {
// Run the node build
nodejs(nodeJSInstallationName: 'MyNode', configId: 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa') {
sh 'npm install'
}
}
stage('Build') {
// Run the node build
nodejs(nodeJSInstallationName: 'MyNode', configId: 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa') {
sh 'npm pack'
}
}
stage('Test') {
// Run the node build
nodejs(nodeJSInstallationName: 'MyNode', configId: 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa') {
sh 'npm run test'
}
}
stage('Results') {
junit 'test-results.xml' // need to setup karma junit reporter
archive 'myArtefact-*.tgz'
}
stage('Artifactory') {
def uploadSpec = """{
"files": [
{
"pattern": "myArtefact-*.tgz",
"target": "artifactory-npm-repo/myArtefact/"
}
]
}"""
def server = Artifactory.server 'MyArtifactory'
def buildInfo = server.upload(uploadSpec)
buildInfo.retention maxBuilds: 2
server.publishBuildInfo(buildInfo)
}
}
But with this, you've got your build ans artifacts in artifactory

Jenkins pipeline + Artifactory download not downloading

I'm having trouble downloading a build from my artifactory server to my windows jenkins slave node using the jenkins pipeline plugin. It all appears to be going fine, but it doesn't actually download the file. Am I doing something wrong?
I don't see any requests in my Artifactory system logs to download, just to upload.
(2017-04-25 18:39:48,096 [http-nio-8081-exec-2] [INFO ] (o.a.e.UploadServiceImpl:516) - Deploy to 'BUILDS:windows/5840/build.tar.gz' Content-Length: 278600525)
I've been using this as a reference: https://wiki.jenkins-ci.org/pages/viewpage.action?pageId=99910084
Here's the output from my jenkins pipeline:
For pattern: build.tar.gz 1 artifacts were found.
Deploying artifact: http://myartifactory:8081/artifactory/BUILDS/windows/5840/build.tar.gz
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] timeout
Timeout set to expire in 3 min 0 sec
[Pipeline] {
[Pipeline] node
Running on test-windows-0 in C:/jenkinsroot/workspace/test-windows
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] echo
{
"files": [
{
"pattern": "BUILDS/windows/5840/build.tar.gz",
"target": "download/",
}
]
}
[Pipeline] echo
Artifactory Download: BUILDS/windows/5840/build.tar.gz -> download/
The file exists on artifactory.
Here's my jenkins code:
#NonCPS
def downloadArtifactory(String localPath, String repository, String remotePath) {
def downloadSpec = """{
"files": [
{
"pattern": "${repository}/${remotePath}",
"target": "${localPath}",
}
]
}"""
echo "${downloadSpec}"
echo "Artifactory Download: ${repository}/${remotePath} -> ${localPath}"
def server = Artifactory.server("MYARTIFACTORYSERVER")
def buildInfo = server.download spec: downloadSpec
return buildInfo
}
Called with:
downloadArtifactory("download/", "BUILDS", "windows/5840/build.tar.gz")
Removing the NonCPS annotation should solve the problem.
As you can see in this Jenkins issue, Artifactory Jenkins plugin does not support NonCPS.
Please remove the ,(comma) from the line "target": "${localPath}"
,
It works
make it,
def downloadArtifactory(String localPath, String repository, String remotePath) {
def downloadSpec = """{
"files": [
{
"pattern": "${repository}/${remotePath}",
"target": "${localPath}"
}
]
}"""
echo "${downloadSpec}"
echo "Artifactory Download: ${repository}/${remotePath} -> ${localPath}"
def server = Artifactory.server("MYARTIFACTORYSERVER")
def buildInfo = server.download spec: downloadSpec
return buildInfo
}

Resources