Jenkins pipeline groovy project job list - jenkins

I want to list all jobs of my current project within my groovy pipeline script.
Does someone know how to do that? With
env.BRANCH_NAME
I am just getting my current branch.

you can get it from jenkins api, for example the URL:
http://jenkinsHost:8080/job/jobName/api/json?pretty=true&tree=jobs[name]
will produce something like:
{
"_class" : "org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject",
"jobs" : [
{
"_class" : "org.jenkinsci.plugins.workflow.job.WorkflowJob",
"name" : "develop"
},
{
"_class" : "org.jenkinsci.plugins.workflow.job.WorkflowJob",
"name" : "master"
},
{
"_class" : "org.jenkinsci.plugins.workflow.job.WorkflowJob",
"name" : "testing"
}
]
}
and then you can parse the branches names with groovy. (In the above case, "develop", "master" and "testing" are branches)

Related

How to use artifactory query language inside a jenkins pipeline instead of having it call from a file

I have a AQL that is used to delete files from the artifactory. I am designing a pipeline so that it can delete files from artifactory based on the AQL.
I know for artifactory upload and download we can specify the AQL within the pipeline using rtupload/rtdownload, but is there a way to do for delete as well?
#AQL delete.spec
{
"files": [
{
"aql": {
"items.find": {
"repo": {"$eq":"app-java-repo"},
"path": "archives/test/app",
"type": "folder",
"$or": [
{
"$and": [
{
"name": {"$nmatch" : "*build*"}
}
]
}
]
}
}
}
]
}
jf rt del --spec delete.spec
Using the jfrog cli, the spec file can be passed and the files from artifactory repo can be deleted. This is working out manually, but how can the same be achieved by having the AQL in the pipeline stage instead of having AQL as a file.

How to setup BuildBlockerProperty on Jenkins MultiBranch pipeline

I'm trying to prevent the run of branch_A while branch_B is running.
So I succeeded with normal pipeline with BuildBlockerProperty , job_A stay in pending while job_B job is running.
But with multibranch pipeline, it's not working.
Example for BuildBlockerProperty property for branch_A :
properties([
[
$class : 'BuildBlockerProperty',
blockLevel : 'GLOBAL',
blockingJobs : 'branch_B',
scanQueueFor : 'ALL',
useBuildBlocker: true
],
])
I saw on jenkins multibranch doc that is it possible but I don't find a way to do that. So if someone have the solution pls !
I found the solution. When you write the branch name, you should write also the multibranch job name.
Exemple :
properties([
[
$class : 'BuildBlockerProperty',
blockLevel : 'GLOBAL',
blockingJobs : 'multibranch_job_name/branch_B',
scanQueueFor : 'ALL',
useBuildBlocker: true
],
])
But I didn't find a way if you want to run the job only if no other jobs are running. If someone have an idea.

jenkins can't get the list

jenkins parameter plugin
return ["DEV", "TEST", "STAGE", "PROD"]
above, it work but
jenkins can't get the list when you code looks that
def ENVS=["DEV", "TEST", "STAGE", "PROD"]
return ENVS
is there any good way to deal with it ?

I try to promote artifact with Jenkins Artifactory Plug-in and got error 404

I use Jenkins Artifactory Plug-in for promotion. I try to create separate job to promote the artifact. Here the stage of promotion
stage('promote artifact') {
steps {
script {
String buildName = "${BUILD_NAME}"
String buildNumber = "${BUILD_NUMBER}"
def promotionConfig = [
//Mandatory parameters
'buildName' : buildName,
'buildNumber' : buildNumber,
'targetRepo' : "tst",
//Optional parameters
'comment' : "this is the promotion comment",
'sourceRepo' : "dev",
'status' : "Released",
'includeDependencies': true,
'failFast' : true,
'copy' : true
]
// Promote build
server.promote promotionConfig
}
}
}
As BUILD_NAME I try name of commit job and name of artifact at snapshot repository
As BUILD_NUMBER I use number of build like 1 without version number like 1.0.0.
But after all I got
Performing dry run promotion (no changes are made during dry run) ...
ERROR: Promotion failed during dry run (no change in Artifactory was done): HTTP/1.1 404 Not Found
{
"errors" : [ {
"status" : 404,
"message" : "Cannot find builds by the name 'artifact-name' and the number '1'."
} ]
}
Do you have some idea how to run it successfully? Or some flag to get more information about this error?
The reason why it fails for me that I skipped part with upload build info to Artifactory. You should create the upload spec during build task and upload it to Artifactory. BuildInfo will be stored separately and contains service infromation. After that promotion will work fine.
def uploadSpec = """{
"files": [
{
"pattern": "bazinga/*froggy*.zip",
"target": "bazinga-repo/froggy-files/"
}
]
}"""
server.upload(uploadSpec)

How can I promote an artifact from jenkins?

We have artifactory pro and We need to know if We can promote an artifact from Jenkins through the plugin. Currently, We are deploying on artifactory and when We need promote first, We download the artifact from artifactory to jenkins and then We publish it on artifactory with other tag.
Exists some way to promote from Jenkin’s pipeline without API, because we need a solution that not expose our credentials.
Thanks,
You can promote a build inside a jenkins script by using artifactory.promote instead of Artifactory.addInteractivePromotion as shown:
#NonCPS
def promote(artifactory, buildInfo) {
echo "currentBuild.result is ${currentBuild.result}"
def QAPromotionConfig = [
'buildName' : buildInfo.name,
'buildNumber' : buildInfo.number,
'targetRepo' : 'debian-local-qa',
'sourceRepo' : 'debian-local-debug',
'copy' : true
]
def RelPromotionConfig = [
'buildName' : buildInfo.name,
'buildNumber' : buildInfo.number,
'targetRepo' : 'debian-local-release',
'sourceRepo' : 'debian-local-debug',
'copy' : true
]
if(currentBuild.result == "SUCCESS") {
artifactory.promote(QAPromotionConfig)
} else {
Artifactory.addInteractivePromotion(server: artifactory, promotionConfig: QAPromotionConfig, displayName: "Promote to QA")
}
Artifactory.addInteractivePromotion(server: artifactory, promotionConfig: RelPromotionConfig, displayName: "Promote to Release")
}

Resources