I'm trying to put jobs inside a folder using jenkins DSL script
Now i create a listView and i put inside my jobs here the code i'm using
listView('MyJobsList') {
jobs {
map.each{
name((it.key).trim())
}
}
columns{
status()
weather()
name()
lastSuccess()
lastFailure()
lastDuration()
buildButton()
}
}
i want to do the same thing but this time i want to put the jobs in a folder !!
Please refer the below Job-DSL documentation to create a folder in Jenkins through Job-DSL.
Folder
folder('folder-a') {
description('Folder containing all jobs for folder-a')
}
job('folder-a/job-a') {
// Job config goes here
}
Please, take a look at Jenkins filestructure: https://wiki.jenkins-ci.org/display/JENKINS/Administering+Jenkins
Here you can see where jobs are stored by default (job config and build logs). You can not and should not change this filestructure with DSL script (JobDSL plugin).
Related
Running Jenkins 2.289.1.
I have this pipelineJob Job Dsl setting up Active Choice parameters:
https://plugins.jenkins.io/uno-choice/
pipelineJob("test") {
parameters {
activeChoiceParam('CHOICE-1') {
description('Allows user choose from multiple choices')
filterable()
choiceType('SINGLE_SELECT')
groovyScript {
script('return ["choice1", "choice2", "choice3"];')
fallbackScript('"fallback choice"')
}
}
}
definition {
cpsScm {
scm {
git {
remote {
credentials("${creds}")
url("${gitUrl}")
}
branch("${gitBranch}")
}
}
scriptPath("${pathToFile}")
}
}
}
To make sure I can run Job Dsl in the first place without having to manually approve that I have added the following to jcasc:
jenkins:
security:
globalJobDslSecurityConfiguration:
useScriptSecurity: false
But that is not enough. Before I can run the generated pipeline based on above Job Dsl I still need to manually approve:
How do I configure Job Dsl, jcasc or something else to either disable script approval for anything that goes on in a Job Dsl or automatically approve any script that might be created inside a job dsl?
Hopefully I don't have to hack my way around that like suggested here:
https://stackoverflow.com/a/64364086/363603
I am aware that there is a reason for this feature but its for a local only jenkins that I am using for experimenting and this is currently killing my productivity. Related:
https://issues.jenkins.io/browse/JENKINS-28178?focusedCommentId=376405&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-376405
What worked for me:
Manage Jenkins > Configure Global Security > CSRF Protection (section header -- not sure why) > Enable script security for Job DSL scripts (the name of the option that I disabled).
I'd try to understand the root cause of reason why Jenkins creates such directories as below.. When I try to find coverage report, I realise that it is located in my-application-ms#2 rather than my-application-ms. Meanwhile I checked rest of directories abd there is only SecretFiles which is empty.
So what is the best way to delete rest of directories which current directory should be always my-application-ms .. Should I specify the each dir in post section ?Is there any doubt to delete rest of directories?
my-application-ms
my-application-ms#2
my-application-ms#2#tmp
my-application-ms#tmp
post{
failure{
notifyBuild('FAILED')
}
success{
notifyBuild('SUCCESSFUL')
}
aborted{
notifyBuild('FAILED')
}
always {
deleteDir() /* clean up our workspace */
}
}
I think you should rather use special step type for workspace clean up. cleanWs should do the job for you.
I have a jenkins file as below
pipelineJob('My pipeline job'){
displayName('display name')
logRotator {
numToKeep(10)
daysToKeep(30)
artifactDaysToKeep(7)
artifactNumToKeep(1)
}
definition{
cps {
script(readFileFromWorkspace('./cicd/pipelines/clone_git_code.groovy'))
script(readFileFromWorkspace('./cicd/pipelines/install_dependencies_run_quality_checks.groovy'))
}
}
}
with above jenkinsfile the last script file is replacing other scripts.
Basically I have split tasks into multiple groovy files so that i wont repeat the same code in all jenkinsfile and reuse the same for other jobs as well, like I can now use the clone_git_code.groovy script in dev build as well as QA builds.
You have to use shared libraries (https://jenkins.io/doc/book/pipeline/shared-libraries/). You can define multiple groovy files with classes to return a processed object or simply creating calls with method where you define a step and the execution will be sequential.
I had this same issue when trying to include multiple scripts into a Jenkins job. After doing some research, I found the below solution to be the simplest:
definition {
cps {
script (
ScriptsLibrary.pipelineTest('did it work?') +
ScriptsLibrary.scmConf('repoURL_input', 'accessCredentials', 'activeBranch')
)
}
}
Add the "+" to concatenate the Strings. Got the job done for me :)
I have two Jenkins jobs that tun on separate computers. On computer 1 I read properties file and use it for environment variables. But i need the same file on PC 2 and it only exist on the first one. When the first Jenkins job finishes it starts the second one and it can pass parameters file via job but I have to receive with creation of separate parameter with Parameterized Trigger Plugin for each parameter, and I have a lot and don`t want to do so. Is there simple solution for this issue?
Forget Jenkins 1 and the plugins Parameterized Trigger Plugin. Using Jenkins 2, here's an example of your need:
node ("pc1") {
stage "step1"
stash name: "app", includes: "properties_dir/*"
}
node ("pc2") {
stage "step2"
dir("dir_to_unstash") {
unstash "app"
}
}
We were pretty hooked onto using some plugins which are not supported in pipelines anymore and would like to implement their usage in shared-libraries of our pipelines. One of the main items required for that would be to get hold of Jenkins Instance, can someone share a way to do that ?
Are there any restrictions or proper way to get hold of Jenkins.getActiveInstance() under "src" or "vars" folder ?
I have tried to get Jenkins.getActiveInstance() under src code as well vars code but it returns null, am I missing something here? any help will be appreciated.
thanks
Try 'Hudson.instance'. This pipeline below works for me on Jenkins 2.32.x. You may have to do some script approvals or turn off the sandbox.
pipeline {
agent none
stages{
stage('Instance Info') {
steps {
script {
def jenkinsInstance = Hudson.instance
for (slave in jenkinsInstance.slaves) {
echo "Slave: ${slave.computer.name}"
}
}
}
}
}
}
Bill
This ticket can be closed, there were few issues :
1. Fix the access via (Manage Jenkins -> In script approval)
2. some scripts contain Non-cps code