Skip to content

Commit f1155f7

Browse files
authored
added a SEED_JOB script variable which provides access to the seed job (#878)
1 parent c09fa19 commit f1155f7

File tree

9 files changed

+73
-12
lines changed

9 files changed

+73
-12
lines changed

docs/Home.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ Browse the Jenkins issue tracker to see any [open issues](https://issues.jenkins
3232
* Updated [Structs Plugin](https://github.com/jenkinsci/structs-plugin) dependency to version 1.2
3333
* Improved support for [[Automatically Generated DSL]]: print deprecation warnings and show deprecated methods in API
3434
viewer
35+
* Added a `SEED_JOB` script variable which provides access to the seed job,
36+
see [Job DSL Commands](Job-DSL-Commands#seed-job)
3537
* Removed anything that has been deprecated in 1.42, see [Migration](Migration#migrating-to-142)
3638
* 1.48 (June 24 2016)
3739
* Added option to ignore missing DSL script files or empty wildcards

docs/Job-DSL-Commands.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,3 +293,21 @@ The `__FILE__` variable is available in scripts only, not in any classes used by
293293
Job DSL scripts are executed on the Jenkins master node, but the seed job's workspace which contains the script files
294294
may reside on a build node. This mean that direct access to the file specified by `__FILE__` may not be possible from a
295295
DSL script. See [Distributed builds](https://wiki.jenkins-ci.org/display/JENKINS/Distributed+builds) for details.
296+
297+
# Seed Job
298+
299+
Access to the seed job is available through the `SEED_JOB` variable. The variable contains a reference to the internal
300+
Jenkins object that represents the seed job. The actual type of the object depends on the type of job that runs the DSL.
301+
For a freestyle project, the object is an instance of `hudson.model.FreeStyleProject`. See the
302+
[Jenkins API Documentation](http://javadoc.jenkins-ci.org/) for details.
303+
304+
The `SEED_JOB` variable is only available in scripts, not in any classes used by a script. And it is only available
305+
when running in Jenkins, e.g. in the "Process Job DSLs" build step.
306+
307+
The following example show how to apply the same quiet period for a generated job as for the seed job.
308+
309+
```groovy
310+
job('example') {
311+
quietPeriod(SEED_JOB.quietPeriod)
312+
}
313+
```

docs/Migration.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## Migrating to 1.49
2+
3+
### JobManagement
4+
5+
The return type of the `getParameters()` method in `javaposse.jobdsl.dsl.JobManagement` changed from
6+
`Map<String, String>` to `Map<String, Object>`.
7+
18
## Migrating to 1.48
29

310
### Pipeline Compatibility

job-dsl-core/src/main/groovy/javaposse/jobdsl/dsl/DslScriptLoader.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ class DslScriptLoader {
274274
binding.setVariable('__FILE__', scriptRequest.scriptPath)
275275
}
276276

277-
jobManagement.parameters.each { String key, String value ->
277+
jobManagement.parameters.each { String key, Object value ->
278278
LOGGER.fine("Binding ${key} to ${value}")
279279
binding.setVariable(key, value)
280280
}

job-dsl-core/src/main/groovy/javaposse/jobdsl/dsl/JobManagement.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ interface JobManagement {
126126
/**
127127
* Map of variables that should be available to the script.
128128
*/
129-
Map<String, String> getParameters()
129+
Map<String, Object> getParameters()
130130

131131
/**
132132
* Logs a deprecation warning for the calling method.

job-dsl-core/src/main/groovy/javaposse/jobdsl/dsl/MockJobManagement.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import hudson.util.VersionNumber
66
* Abstract base class for all non-Jenkins implementations of {@link JobManagement}.
77
*/
88
abstract class MockJobManagement extends AbstractJobManagement {
9-
final Map<String, String> parameters = [:]
9+
final Map<String, Object> parameters = [:]
1010
final Map<String, List<String>> permissions = [
1111
'hudson.security.AuthorizationMatrixProperty': [
1212
'hudson.model.Item.Delete',

job-dsl-plugin/src/main/groovy/javaposse/jobdsl/plugin/InterruptibleJobManagement.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class InterruptibleJobManagement implements JobManagement {
8585
}
8686

8787
@Override
88-
Map<String, String> getParameters() {
88+
Map<String, Object> getParameters() {
8989
delegate.parameters
9090
}
9191

job-dsl-plugin/src/main/groovy/javaposse/jobdsl/plugin/JenkinsJobManagement.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,12 @@ public void createOrUpdateUserContent(UserContent userContent, boolean ignoreExi
236236
}
237237

238238
@Override
239-
public Map<String, String> getParameters() {
240-
return envVars;
239+
public Map<String, Object> getParameters() {
240+
Map<String, Object> result = new HashMap<String, Object>(envVars);
241+
if (project != null && !result.containsKey("SEED_JOB")) {
242+
result.put("SEED_JOB", project);
243+
}
244+
return result;
241245
}
242246

243247
@Override

job-dsl-plugin/src/test/groovy/javaposse/jobdsl/plugin/JenkinsJobManagementSpec.groovy

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import hudson.model.Cause
99
import hudson.model.Failure
1010
import hudson.model.FreeStyleBuild
1111
import hudson.model.FreeStyleProject
12+
import hudson.model.Job
1213
import hudson.model.ListView
1314
import hudson.model.Run
1415
import hudson.model.View
@@ -47,12 +48,24 @@ class JenkinsJobManagementSpec extends Specification {
4748
@Rule
4849
JenkinsRule jenkinsRule = new JenkinsRule()
4950

50-
ByteArrayOutputStream buffer = new ByteArrayOutputStream()
51-
Run build = Mock(Run)
52-
JenkinsJobManagement jobManagement = new JenkinsJobManagement(
53-
new PrintStream(buffer), [:], build, new FilePath(new File('.')), LookupStrategy.JENKINS_ROOT
54-
)
55-
JenkinsJobManagement testJobManagement = new JenkinsJobManagement(new PrintStream(buffer), [:], new File('.'))
51+
ByteArrayOutputStream buffer
52+
Job seedJob
53+
Run build
54+
JenkinsJobManagement jobManagement
55+
JenkinsJobManagement testJobManagement
56+
57+
def setup() {
58+
seedJob = Mock(Job)
59+
build = Mock(Run)
60+
61+
build.parent >> seedJob
62+
63+
buffer = new ByteArrayOutputStream()
64+
jobManagement = new JenkinsJobManagement(
65+
new PrintStream(buffer), [:], build, new FilePath(new File('.')), LookupStrategy.JENKINS_ROOT
66+
)
67+
testJobManagement = new JenkinsJobManagement(new PrintStream(buffer), [:], new File('.'))
68+
}
5669

5770
@WithoutJenkins
5871
def 'createOrUpdateView without name'() {
@@ -837,6 +850,23 @@ class JenkinsJobManagementSpec extends Specification {
837850
project.builds.lastBuild.causes[0].shortDescription == 'Started by a Job DSL script'
838851
}
839852
853+
def 'parameters include SEED_JOB'() {
854+
when:
855+
Map<String, Object> parameters = jobManagement.parameters
856+
857+
then:
858+
parameters.containsKey('SEED_JOB')
859+
parameters.SEED_JOB == seedJob
860+
}
861+
862+
def 'parameters do not include SEED_JOB for testing'() {
863+
when:
864+
Map<String, Object> parameters = testJobManagement.parameters
865+
866+
then:
867+
!parameters.containsKey('SEED_JOB')
868+
}
869+
840870
private static boolean isXmlIdentical(String expected, Node actual) throws Exception {
841871
XMLUnit.ignoreWhitespace = true
842872
XMLUnit.compareXML(loadResource(expected), nodeToString(actual)).identical()

0 commit comments

Comments
 (0)