Jenkins DSL API for copyartifact permissions - jenkins

I am trying to add a call to my jenkins job dsl that will configure the job to give permission to another build to copy artifacts. However, I am unable to find a command for it in the Jenkins Job DSL API:
https://jenkinsci.github.io/job-dsl-plugin/
Here is the option I am trying to set using the DSL:
Does this command exist? Is there anyways to setup my groovy to do this if it doesnt?

There is no built-in DSL to set that permission, but you can use the Dynamic DSL.
The Job DSL API viewer can be opened at http://localhost:8080/plugin/job-dsl/api-viewer/index.html where localhost is your Jenkins host. Search for copyArtifactPermission as an example:
job('example') {
properties {
copyArtifactPermissionProperty {
projectNames('one, two')
}
}
}

is it this one?
job('example') {
steps {
copyArtifacts('upstream') {
includePatterns('*.xml', '*.properties')
excludePatterns('test.xml', 'test.properties')
targetDirectory('files')
flatten()
optional()
buildSelector {
latestSuccessful(true)
}
}
}
}
EDIT
It seems this may have been fixed in the google group for job-dsl
configure { project ->
project / 'properties' / 'hudson.plugins.copyartifact.CopyArtifactPermissionProperty' / 'projectNameList' {
'string' "*-foo"
}
}
I think they may have changed the interface though and you need to provide explicit job names now, but I haven't got the plugin so I can't check

Related

matrix trigger mode in extendedEmail - Jenkins job DSL

I've a multi configuration project on Jenkins(ver. 2.222.1) We use JenkinsDSL(v1.77) to create the free-style jobs from the groovy scripts. Email notifications is with extendedEmail(v2.69)
It seems like trigger mode for notifications cannot be specified in the groovy script but it could be done via the UI as below(ONLY_PARENT or ONLY_CONFIGURATIONS or BOTH)
As I see here, below are the configurations we could specify for extendedEmail in groovy for DSL plugin
job('example') {
publishers {
extendedEmail {
recipientList('me#halfempty.org')
defaultSubject('Oops')
defaultContent('Something broken')
contentType('text/html')
triggers {
beforeBuild()
stillUnstable {
subject('Subject')
content('Body')
sendTo {
developers()
requester()
culprits()
}
}
}
}
}
}
It does not say anything in case of multi configuration project where I need the notifications to be sent only if parent job fails
I believe it exists on hipChatNotifier where we could specify as matrixTriggerMode
Is there a way to do this via extendedEmail publisher as we could do it on the UI?
Seems that this isn't supported by the Job DSL plugin, yet.
You can work around by using a custom configure block. For example
publishers {
extendedEmail {
...
}
}
configure { project -> project / 'publishers' / 'hudson.plugins.emailext.ExtendedEmailPublisher' << {
matrixTriggerMode('ONLY_PARENT')
}
}
Note the << to append a node to the publisher configuration instead of overwriting it.

Retrieving an injected build parameter in Pipeline

I'm specifically working on the example below, but I'm guessing the question is a bit more general.
https://github.com/jenkinsci/poll-mailbox-trigger-plugin
States the following build parameters, are injected into the job
pipeline {
agent any
stages {
stage('Test') {
steps {
echo env.pmt_content
echo ${pmt_content}
}
}
}
}
The above methods don't seem to work for me:
How is it possible to retrieve an injected build parameter in a pipeline job?
As explained in the pipeline-plugin tutorial, you can use params to access the job's build parameters.
print params.pmt_content
print "Content is ${params.pmt_content}"

Jenkins DSL : Job SLack publisher : baseUrl() method not available

I use Jenkins 2.63 with and Slack Notifier plugin 2.2
I need to generate Jobs with SlackNotifier by Job DSL but I can not set the Base URL in the DSL, I get this message :
ERROR: (script, line 145) No signature of method: baseUrl() is applicable for argument types: (java.lang.String) values: [https://my.domain.slack.com/services/hooks/jenkins-ci/] Possible solutions: authToken(), authTokenCredentialId(), botUser(), commitInfoChoice(), customMessage(), includeCustomMessage(), includeTestSummary(), notifyAborted(), notifyBackToNormal(), notifyFailure(), notifyNotBuilt(), notifyRegression(), notifyRepeatedFailure(), notifySuccess(), notifyUnstable(), room(), sendAs(), startNotification(), teamDomain() Finished: FAILURE
here is my DSL script
publishers {
def slackParam = new groovy.json.JsonSlurper().parse(new File(channelFile))
slackNotifier {
baseUrl(slackParam.url)
authTokenCredentialId(slackParam.authTokenCredentialId)
includeTestSummary(true)
notifyAborted(true)
notifyBackToNormal(true)
notifyFailure(true)
notifyNotBuilt(true)
notifyRegression(true)
notifyRepeatedFailure(true)
notifyUnstable(true)
room(slackParam.room)
}
}
But in the job config.xml I can find this parameter.
Can anyone help me to set the base URL parameter ?
Thanks a lot.
Using the Configure Block: https://github.com/jenkinsci/job-dsl-plugin/wiki/The-Configure-Block
configure { project ->
project / publishers << 'jenkins.plugins.slack.SlackNotifier' {
baseUrl("https://whatever.slack.com/services/hooks/jenkins-ci/")
room("#room")
notifyAborted(true)
notifyFailure(true)
notifyNotBuilt(false)
notifyUnstable(true)
notifyBackToNormal(true)
notifySuccess(true)
notifyRepeatedFailure(false)
startNotification(false)
includeTestSummary(false)
includeCustomMessage(true)
customMessage("Environment")
sendAs(null)
commitInfoChoice("AUTHORS_AND_TITLES")
teamDomain("yourDomain")
authTokenCredentialId("token")
}
}
Place this outside of your steps at the bottom of your job. You should see the changes reflected in the UI and config.xml for the job.

How to add "single conditional steps" under build section using dsl script

I'm currently trying to develop a DSL script that can create a jenkins job with all required plugins and options.
I think I've almost completed all the section. But, I stuck up under build section where I've to include "conditional steps (single)" under Build.
Actually what I wanted is this
But, what I get is this
Here's the code that I used,
job('Sample_dev') {
steps {
conditionalSteps {
condition {
alwaysRun()
}
}
maven {
goals('install')
}
}
}
You have done few mistakes there:
Using multi-step DSL for achieving single step.
Pushed maven outside context like individual step.
Wrong DSL for Maven Step declaration.
Try following
job('Sample_dev')
{
steps{
singleConditionalBuilder{
condition{
alwaysRun()
}
buildStep {
maven{
targets('install')
name('')
pom('')
properties('')
jvmOptions('')
usePrivateRepository(false)
settings {
standard()
}
globalSettings {
standard()
}
injectBuildVariables(false)
}
}
runner {
fail()
}
}
}
}
The creator has deployed most on this url https://jenkinsci.github.io/job-dsl-plugin. But I would suggest you install in you local instance and access it via http://<your-jenkins-host>:<port> /plugin/job-dsl/api-viewer/index.html as Job DSL support auto generation so there is bright chance that plugin not listed above still has DSL support.

Job DSL Restrict jobs to selected nodes

I am struggling to restrict Jenkins job to specific nodes using Job DSL plugin.
I tried something like :
job("campaign") {
parameters {
stringParam("ARTIFACT_NUMBER", "","")
nodeParam('TEST_HOST') {
defaultNodes(["Slave"])
}
}
steps {
shell('''#!/bin/bash
ARTIFACT_DIR=daily_${ARTIFACT_NUMBER}
echo ${ARTIFACT_DIR}
''')
}
}
but no success. Basically, I want to set the property Restrict where this project can run through Job DSL plugin
The label method sets the Restrict where this project can run on job level:
job('example') {
label('agentA agentB')
}
See the API viewer for details: https://jenkinsci.github.io/job-dsl-plugin/#path/job-label

Resources