├── settings.gradle ├── .gitignore ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── src ├── main │ └── groovy │ │ └── uk │ │ └── gov │ │ └── hmrc │ │ └── jenkinsjobbuilders │ │ └── domain │ │ ├── Setting.groovy │ │ ├── Condition.groovy │ │ ├── scm │ │ ├── Scm.groovy │ │ ├── GitHubComScm.groovy │ │ └── GitHubScm.groovy │ │ ├── step │ │ ├── Step.groovy │ │ ├── GradleStep.groovy │ │ ├── InjectEnvironmentVariablesStep.groovy │ │ ├── BuildDescriptionStep.groovy │ │ ├── MavenStep.groovy │ │ ├── ShellStep.groovy │ │ ├── SbtStep.groovy │ │ ├── RemoteTriggerStep.groovy │ │ ├── RemoteTriggerStep2.groovy │ │ └── AwsCodeBuildStep.groovy │ │ ├── trigger │ │ ├── Trigger.groovy │ │ ├── GitHubPushTrigger.groovy │ │ ├── CronTrigger.groovy │ │ ├── PollTrigger.groovy │ │ ├── GitHubPullRequestTrigger.groovy │ │ ├── UpstreamTrigger.groovy │ │ └── BintrayArtifactTrigger.groovy │ │ ├── configure │ │ ├── Configure.groovy │ │ ├── GatlingReportsPublisher.groovy │ │ ├── SCoverageReportsPublisher.groovy │ │ ├── XvfbBuildWrapper.groovy │ │ ├── CheckStyleReportsPublisher.groovy │ │ ├── InheritanceStrategy.groovy │ │ ├── BaseSCoverageReportsPublisher.groovy │ │ ├── CucumberReportsPublisher.groovy │ │ ├── RemoteToken.groovy │ │ ├── FolderInheritanceStrategy.groovy │ │ ├── ProjectBasedSecurityProperty.groovy │ │ ├── PyEnvBuildWrapper.groovy │ │ ├── InjectEnvironmentJobProperty.groovy │ │ └── ProjectBasedGroupSecurityProperty.groovy │ │ ├── wrapper │ │ ├── Wrapper.groovy │ │ ├── TimestampsWrapper.groovy │ │ ├── UserVariablesWrapper.groovy │ │ ├── ColorizeOutputWrapper.groovy │ │ ├── PreBuildCleanupWrapper.groovy │ │ ├── model │ │ │ ├── SecretText.groovy │ │ │ ├── ConjoinedSecretUsernamePassword.groovy │ │ │ └── SecretUsernamePassword.groovy │ │ ├── NodeJsWrapper.groovy │ │ ├── AbsoluteTimeoutWrapper.groovy │ │ ├── PreScmStepsWrapper.groovy │ │ ├── EnvironmentVariablesWrapper.groovy │ │ └── CredentialsBindings.groovy │ │ ├── publisher │ │ ├── Publisher.groovy │ │ ├── ClaimBrokenBuildsPublisher.groovy │ │ ├── GithubCommitNotifierPublisher.groovy │ │ ├── ArtifactsPublisher.groovy │ │ ├── PostBuildTaskPublisher.groovy │ │ ├── JUnitReportsPublisher.groovy │ │ ├── HtmlReportsPublisher.groovy │ │ ├── JobsTriggerPublisher.groovy │ │ ├── BuildDescriptionPublisher.groovy │ │ ├── CleanWorkspacePostBuildTaskPublisher.groovy │ │ ├── PostBuildScriptPublisher.groovy │ │ ├── CleanXvfbPostBuildTaskPublisher.groovy │ │ └── PostBuildCleanWsPublisher.groovy │ │ ├── variable │ │ ├── EnvironmentVariable.groovy │ │ └── StringEnvironmentVariable.groovy │ │ ├── builder │ │ ├── Builder.groovy │ │ ├── ListViewBuilder.groovy │ │ ├── BuildMonitorViewBuilder.groovy │ │ ├── FolderBuilder.groovy │ │ └── JobBuilder.groovy │ │ ├── parameters │ │ ├── Parameter.groovy │ │ ├── FileParameter.groovy │ │ ├── ChoiceParameter.groovy │ │ ├── TextParameter.groovy │ │ ├── StringParameter.groovy │ │ ├── BooleanParameter.groovy │ │ ├── NodeParameter.groovy │ │ └── ActiveChoiceReactiveParameter.groovy │ │ ├── AbstractBuildDescriptionStep.groovy │ │ ├── authorisation │ │ ├── Permission.groovy │ │ └── GroupPermission.groovy │ │ ├── AbstractBuildDescriptionPublisher.groovy │ │ └── throttle │ │ └── ThrottleConfiguration.groovy └── test │ └── groovy │ └── uk │ └── gov │ └── hmrc │ └── jenkinsjobbuilders │ └── domain │ ├── publisher │ ├── GithubCommitNotifierPublisherSpec.groovy │ ├── JobsTriggerPublisherSpec.groovy │ ├── PostBuildTaskPublisherSpec.groovy │ ├── CleanXvfbPostBuildTaskPublisherSpec.groovy │ ├── CleanWorkspacePostBuildTaskPublisherSpec.groovy │ ├── PostBuildScriptPublisherSpec.groovy │ └── HtmlReportsPublisherSpec.groovy │ ├── authorisation │ ├── PermissionSpec.groovy │ └── GroupPermissionSpec.groovy │ ├── configure │ ├── CheckStyleReportsPublisherSpec.groovy │ ├── BaseSCoverageReportsPublisherSpec.groovy │ ├── RemoteTokenSpec.groovy │ ├── CucumberReportsPublisherSpec.groovy │ ├── SCoverageReportsPublisherSpec.groovy │ ├── XvfbBuildWrapperSpec.groovy │ ├── FolderInheritanceStrategySpec.groovy │ ├── InjectEnvironmentjobPropertySpec.groovy │ ├── PyEnvBuildWrapperSpec.groovy │ ├── ProjectBasedSecurityPropertySpec.groovy │ └── ProjectBasedGroupSecurityPropertySpec.groovy │ ├── builder │ ├── BuildMonitorViewBuilderSpec.groovy │ ├── FolderBuilderSpec.groovy │ └── FolderBuilderGroupPermissionSpec.groovy │ ├── AbstractJobSpec.groovy │ ├── step │ ├── InjectEnvironmentVariablesStepSpec.groovy │ ├── RemoteTriggerStepSpec.groovy │ ├── AwsCodeBuildStepSpec.groovy │ ├── ShellStepSpec.groovy │ └── RemoteTriggerStep2Spec.groovy │ ├── wrapper │ ├── AbsoluteTimeoutWrapperSpec.groovy │ └── TimestampsWrapperSpec.groovy │ ├── trigger │ ├── UpstreamTriggerSpec.groovy │ └── GitHubPullRequestTriggerSpec.groovy │ ├── throttle │ └── ThrottleConfigurationSpec.groovy │ ├── parameters │ ├── TextParametersSpec.groovy │ └── BooleanParametersSpec.groovy │ └── scm │ └── GitHubComScmSpec.groovy ├── repository.yaml ├── Dockerfile ├── Taskfile.yml ├── gradlew.bat ├── README.md └── gradlew /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'jenkins-job-builders' 2 | 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | .gitconfig 3 | .idea 4 | *.ipr 5 | *.iws 6 | *.iml 7 | build 8 | out/* 9 | target/* -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hmrc/jenkins-job-builders/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/Setting.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain 2 | 3 | interface Setting { 4 | 5 | Closure toDsl() 6 | } -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/Condition.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain 2 | 3 | enum Condition { 4 | SUCCESS, UNSTABLE, FAILURE 5 | } 6 | -------------------------------------------------------------------------------- /repository.yaml: -------------------------------------------------------------------------------- 1 | repoVisibility: public_0C3F0CE3E6E6448FAD341E7BFA50FCD333E06A20CFF05FCACE61154DDBBADF71 2 | description: Automated Jenkins job and view configuration, via the Jenkins Job DSL Plugin 3 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/scm/Scm.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.scm 2 | 3 | import uk.gov.hmrc.jenkinsjobbuilders.domain.Setting 4 | 5 | 6 | interface Scm extends Setting { 7 | } -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/step/Step.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.step 2 | 3 | import uk.gov.hmrc.jenkinsjobbuilders.domain.Setting 4 | 5 | 6 | interface Step extends Setting { 7 | } 8 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/trigger/Trigger.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.trigger 2 | 3 | import uk.gov.hmrc.jenkinsjobbuilders.domain.Setting 4 | 5 | interface Trigger extends Setting { 6 | } -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM dockerhub.tax.service.gov.uk/eclipse-temurin:21 2 | 3 | ENV ARTIFACTORY_URI=https://artefacts.tax.service.gov.uk/artifactory 4 | 5 | WORKDIR /app 6 | 7 | COPY . /app/ 8 | 9 | CMD ["/bin/bash", "-c", "./gradlew clean test build -info"] -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/configure/Configure.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.configure 2 | 3 | import uk.gov.hmrc.jenkinsjobbuilders.domain.Setting 4 | 5 | interface Configure extends Setting { 6 | } -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/wrapper/Wrapper.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.wrapper 2 | 3 | import uk.gov.hmrc.jenkinsjobbuilders.domain.Setting 4 | 5 | 6 | interface Wrapper extends Setting { 7 | } 8 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/publisher/Publisher.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.publisher 2 | 3 | import uk.gov.hmrc.jenkinsjobbuilders.domain.Setting 4 | 5 | 6 | interface Publisher extends Setting { 7 | } -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/variable/EnvironmentVariable.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.variable 2 | 3 | public interface EnvironmentVariable { 4 | 5 | String getName() 6 | 7 | String getValue() 8 | } -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/builder/Builder.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.builder 2 | 3 | import javaposse.jobdsl.dsl.DslFactory 4 | 5 | interface Builder { 6 | 7 | T build(DslFactory dslFactory) 8 | } -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/parameters/Parameter.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.parameters 2 | 3 | import uk.gov.hmrc.jenkinsjobbuilders.domain.Setting 4 | 5 | 6 | public interface Parameter extends Setting { 7 | 8 | } -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip 4 | networkTimeout=10000 5 | validateDistributionUrl=true 6 | zipStoreBase=GRADLE_USER_HOME 7 | zipStorePath=wrapper/dists 8 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/wrapper/TimestampsWrapper.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.wrapper 2 | 3 | class TimestampsWrapper implements Wrapper { 4 | 5 | private TimestampsWrapper() { 6 | } 7 | 8 | static Wrapper timestampsWrapper() { 9 | new TimestampsWrapper() 10 | } 11 | 12 | @Override 13 | Closure toDsl() { 14 | return { 15 | timestamps() 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Taskfile.yml: -------------------------------------------------------------------------------- 1 | version: '3' 2 | 3 | env: 4 | ARTIFACTORY_URI: "https://artefacts.tax.service.gov.uk/artifactory" 5 | 6 | tasks: 7 | local-test: 8 | desc: Run gradlew clean test and build command locally 9 | cmds: 10 | - ./gradlew clean test build -info 11 | 12 | docker-test: 13 | desc: Run gradlew clean test and build command in docker container 14 | cmds: 15 | - docker build . --platform linux/arm64 -t jenkins-job-builders 16 | - docker run -it jenkins-job-builders -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/trigger/GitHubPushTrigger.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.trigger 2 | 3 | 4 | final class GitHubPushTrigger implements Trigger { 5 | 6 | private GitHubPushTrigger() { 7 | } 8 | 9 | static GitHubPushTrigger gitHubPushTrigger() { 10 | new GitHubPushTrigger() 11 | } 12 | 13 | @Override 14 | Closure toDsl() { 15 | return { 16 | githubPush() 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/wrapper/UserVariablesWrapper.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.wrapper 2 | 3 | class UserVariablesWrapper implements Wrapper { 4 | 5 | private UserVariablesWrapper() { 6 | } 7 | 8 | static Wrapper userVariablesWrapper() { 9 | new UserVariablesWrapper() 10 | } 11 | 12 | @Override 13 | Closure toDsl() { 14 | return { 15 | buildUserVars() 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/wrapper/ColorizeOutputWrapper.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.wrapper 2 | 3 | class ColorizeOutputWrapper implements Wrapper { 4 | 5 | private ColorizeOutputWrapper() { 6 | } 7 | 8 | static Wrapper colorizeOutputWrapper() { 9 | new ColorizeOutputWrapper() 10 | } 11 | 12 | @Override 13 | Closure toDsl() { 14 | return { 15 | colorizeOutput() 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/wrapper/PreBuildCleanupWrapper.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.wrapper 2 | 3 | class PreBuildCleanupWrapper implements Wrapper { 4 | 5 | private PreBuildCleanupWrapper() { 6 | } 7 | 8 | static Wrapper preBuildCleanUpWrapper() { 9 | new PreBuildCleanupWrapper() 10 | } 11 | 12 | @Override 13 | Closure toDsl() { 14 | return { 15 | preBuildCleanup() 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/wrapper/model/SecretText.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.wrapper.model 2 | 3 | class SecretText { 4 | 5 | final String variable 6 | final String credentials 7 | 8 | SecretText(String variable, String credentials) { 9 | this.variable = variable 10 | this.credentials = credentials 11 | } 12 | 13 | static SecretText secretText(String variable, String credentials) { 14 | new SecretText(variable, credentials) 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/publisher/ClaimBrokenBuildsPublisher.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.publisher 2 | 3 | 4 | final class ClaimBrokenBuildsPublisher implements Publisher { 5 | 6 | private ClaimBrokenBuildsPublisher() {} 7 | 8 | static ClaimBrokenBuildsPublisher claimBrokenBuildsPublisher() { 9 | new ClaimBrokenBuildsPublisher() 10 | } 11 | 12 | @Override 13 | Closure toDsl() { 14 | return { 15 | allowBrokenBuildClaiming() 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/publisher/GithubCommitNotifierPublisher.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.publisher 2 | 3 | 4 | final class GithubCommitNotifierPublisher implements Publisher { 5 | 6 | private GithubCommitNotifierPublisher() { } 7 | 8 | static GithubCommitNotifierPublisher githubCommitStatusPublisher() { 9 | new GithubCommitNotifierPublisher() 10 | } 11 | 12 | @Override 13 | Closure toDsl() { 14 | return { 15 | githubCommitNotifier() 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/AbstractBuildDescriptionStep.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain 2 | 3 | abstract class AbstractBuildDescriptionStep implements Setting { 4 | protected final String description 5 | protected final String regex 6 | 7 | protected AbstractBuildDescriptionStep(String regex, String description) { 8 | this.regex = regex 9 | this.description = description 10 | } 11 | 12 | Closure toDsl() { 13 | return { 14 | buildDescription(regex, description) 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/trigger/CronTrigger.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.trigger 2 | 3 | 4 | final class CronTrigger implements Trigger { 5 | private final String cronPattern 6 | 7 | private CronTrigger(String cronPattern) { 8 | this.cronPattern = cronPattern 9 | } 10 | 11 | static CronTrigger cronTrigger(String cronPattern) { 12 | new CronTrigger(cronPattern) 13 | } 14 | 15 | @Override 16 | Closure toDsl() { 17 | return { 18 | cron(cronPattern) 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/trigger/PollTrigger.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.trigger 2 | 3 | 4 | final class PollTrigger implements Trigger { 5 | private final String pollPattern 6 | 7 | private PollTrigger(String pollPattern) { 8 | this.pollPattern = pollPattern 9 | } 10 | 11 | static PollTrigger pollTrigger(String pollPattern) { 12 | new PollTrigger(pollPattern) 13 | } 14 | 15 | @Override 16 | Closure toDsl() { 17 | return { 18 | scm(pollPattern) 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/wrapper/NodeJsWrapper.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.wrapper 2 | 3 | class NodeJsWrapper implements Wrapper { 4 | 5 | private final String version 6 | 7 | private NodeJsWrapper(String version) { 8 | this.version = version 9 | } 10 | 11 | static Wrapper nodeJsWrapper(String version = '0.10.28') { 12 | new NodeJsWrapper(version) 13 | } 14 | 15 | @Override 16 | Closure toDsl() { 17 | return { 18 | nodejs("node $version") 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/configure/GatlingReportsPublisher.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.configure 2 | 3 | final class GatlingReportsPublisher implements Configure { 4 | 5 | private GatlingReportsPublisher() {} 6 | 7 | Closure toDsl() { 8 | return { 9 | it / 'publishers' / 'io.gatling.jenkins.GatlingPublisher' { 10 | 'enabled'('true') 11 | } 12 | } 13 | } 14 | 15 | static GatlingReportsPublisher gatlingReportsPublisher() { 16 | new GatlingReportsPublisher() 17 | } 18 | } -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/step/GradleStep.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.step 2 | 3 | 4 | class GradleStep implements Step { 5 | private final String command 6 | 7 | private GradleStep(String command) { 8 | this.command = command 9 | } 10 | 11 | static Step gradleStep(String command) { 12 | new GradleStep(command) 13 | } 14 | 15 | @Override 16 | Closure toDsl() { 17 | return { 18 | gradle(command, '', true) { 19 | fromRootBuildScriptDir('true') 20 | } 21 | 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/wrapper/AbsoluteTimeoutWrapper.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.wrapper 2 | 3 | class AbsoluteTimeoutWrapper implements Wrapper { 4 | 5 | private final int minutes 6 | 7 | AbsoluteTimeoutWrapper(int minutes) { 8 | this.minutes = minutes 9 | } 10 | 11 | static AbsoluteTimeoutWrapper timeoutWrapper(int minutes) { 12 | new AbsoluteTimeoutWrapper(minutes) 13 | } 14 | 15 | @Override 16 | Closure toDsl() { 17 | return { 18 | timeout { 19 | absolute(minutes) 20 | failBuild() 21 | } 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/step/InjectEnvironmentVariablesStep.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.step 2 | 3 | 4 | class InjectEnvironmentVariablesStep implements Step { 5 | private final String fileName 6 | 7 | private InjectEnvironmentVariablesStep(String fileName) { 8 | this.fileName = fileName 9 | } 10 | 11 | static Step injectEnvironmentVariablesStep(String fileName) { 12 | new InjectEnvironmentVariablesStep(fileName) 13 | } 14 | 15 | @Override 16 | Closure toDsl() { 17 | return { 18 | environmentVariables { 19 | propertiesFile(this.fileName) 20 | } 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/parameters/FileParameter.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.parameters 2 | 3 | 4 | class FileParameter implements Parameter { 5 | 6 | private final String location 7 | private final String description 8 | 9 | private FileParameter(String location, String description) { 10 | this.description = description 11 | this.location = location 12 | } 13 | 14 | static FileParameter fileParameter(String location, String description = null) { 15 | new FileParameter(location, description) 16 | } 17 | 18 | @Override 19 | Closure toDsl() { 20 | return { 21 | fileParam(location, description) 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/wrapper/model/ConjoinedSecretUsernamePassword.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.wrapper.model 2 | 3 | class ConjoinedSecretUsernamePassword { 4 | 5 | final String variableName 6 | final String credentials 7 | 8 | ConjoinedSecretUsernamePassword(String variableName, String credentials) { 9 | this.variableName = variableName 10 | this.credentials = credentials 11 | } 12 | 13 | static ConjoinedSecretUsernamePassword conjoinedSecretUsernamePassword(String variableName, 14 | String credentials) { 15 | new ConjoinedSecretUsernamePassword(variableName, credentials) 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/step/BuildDescriptionStep.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.step; 2 | 3 | import uk.gov.hmrc.jenkinsjobbuilders.domain.AbstractBuildDescriptionStep 4 | 5 | final class BuildDescriptionStep extends AbstractBuildDescriptionStep implements Step { 6 | 7 | protected BuildDescriptionStep(String regex, String description) { 8 | super(regex, description) 9 | } 10 | 11 | static BuildDescriptionStep buildDescriptionByTextStep(String description) { 12 | return new BuildDescriptionStep('', description) 13 | } 14 | 15 | static BuildDescriptionStep buildDescriptionByRegexStep(String regex) { 16 | return new BuildDescriptionStep(regex, '') 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/publisher/ArtifactsPublisher.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.publisher 2 | 3 | 4 | final class ArtifactsPublisher implements Publisher { 5 | private final String artifactsPattern 6 | 7 | private ArtifactsPublisher(String artifactsPattern) { 8 | this.artifactsPattern = artifactsPattern 9 | } 10 | 11 | static ArtifactsPublisher artifactsPublisher(String artifactsPattern) { 12 | new ArtifactsPublisher(artifactsPattern) 13 | } 14 | 15 | @Override 16 | Closure toDsl() { 17 | return { 18 | archiveArtifacts { 19 | pattern(artifactsPattern) 20 | allowEmpty(true) 21 | } 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/step/MavenStep.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.step 2 | 3 | import static uk.gov.hmrc.jenkinsjobbuilders.domain.step.ShellStep.shellStep 4 | 5 | class MavenStep implements Step { 6 | 7 | private final Step step 8 | 9 | private MavenStep(String ... commands) { 10 | this.step = shellStep(commands.inject('mkdir -p \${WORKSPACE}/tmp') { 11 | string, command -> string + "\n/opt/apache-maven-3.2.1/bin/mvn $command -Djava.io.tmpdir=\"\${WORKSPACE}/tmp\"" 12 | }) 13 | } 14 | 15 | static Step mavenStep(String ... commands) { 16 | new MavenStep(commands) 17 | } 18 | 19 | @Override 20 | Closure toDsl() { 21 | step.toDsl() 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/wrapper/PreScmStepsWrapper.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.wrapper 2 | 3 | import uk.gov.hmrc.jenkinsjobbuilders.domain.step.Step 4 | 5 | class PreScmStepsWrapper implements Wrapper { 6 | private final List steps 7 | 8 | private PreScmStepsWrapper(List steps) { 9 | this.steps = steps 10 | } 11 | 12 | static Wrapper preScmStepsWrapper(List steps) { 13 | new PreScmStepsWrapper(steps) 14 | } 15 | 16 | @Override 17 | Closure toDsl() { 18 | return { 19 | preScmSteps { 20 | this.steps.each { 21 | steps(it.toDsl()) 22 | } 23 | } 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/variable/StringEnvironmentVariable.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.variable 2 | 3 | class StringEnvironmentVariable implements EnvironmentVariable { 4 | 5 | private final String name 6 | private final String value 7 | 8 | private StringEnvironmentVariable(String name, String value) { 9 | this.name = name 10 | this.value = value 11 | } 12 | 13 | static StringEnvironmentVariable stringEnvironmentVariable(String name, String value) { 14 | new StringEnvironmentVariable(name, value) 15 | } 16 | 17 | @Override 18 | String getName() { 19 | return name 20 | } 21 | 22 | @Override 23 | String getValue() { 24 | return value 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/publisher/PostBuildTaskPublisher.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.publisher 2 | 3 | final class PostBuildTaskPublisher implements Publisher { 4 | private final String logText 5 | private final String script 6 | 7 | private PostBuildTaskPublisher(String logText, String script) { 8 | this.logText = logText 9 | this.script = script 10 | } 11 | 12 | static PostBuildTaskPublisher postBuildTaskPublisher(String logText, String script) { 13 | new PostBuildTaskPublisher(logText, script) 14 | } 15 | 16 | @Override 17 | Closure toDsl() { 18 | return { 19 | postBuildTask { 20 | task(this.logText, this.script, true, false) 21 | } 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/authorisation/Permission.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.authorisation 2 | 3 | import uk.gov.hmrc.jenkinsjobbuilders.domain.Setting 4 | 5 | class Permission implements Setting { 6 | 7 | final String permission 8 | final String ldapIdentifier 9 | 10 | private Permission(String ldapIdentifier, String permission) { 11 | this.ldapIdentifier = ldapIdentifier 12 | this.permission = permission 13 | } 14 | 15 | static Permission permissionSetting(String ldapIdentifier, String permission) { 16 | new Permission(ldapIdentifier, permission) 17 | } 18 | 19 | @Override 20 | Closure toDsl() { 21 | return { 22 | delegate.permission(permission, ldapIdentifier) 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/publisher/JUnitReportsPublisher.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.publisher 2 | 3 | 4 | final class JUnitReportsPublisher implements Publisher { 5 | private final String junitReportsPattern 6 | 7 | private JUnitReportsPublisher(String junitReportsPattern) { 8 | this.junitReportsPattern = junitReportsPattern 9 | } 10 | 11 | static JUnitReportsPublisher jUnitReportsPublisher(String junitReportsPattern) { 12 | new JUnitReportsPublisher(junitReportsPattern) 13 | } 14 | 15 | @Override 16 | Closure toDsl() { 17 | return { 18 | archiveJunit(junitReportsPattern) { 19 | testDataPublishers { 20 | allowClaimingOfFailedTests() 21 | } 22 | } 23 | } 24 | } 25 | } -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/authorisation/GroupPermission.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.authorisation 2 | 3 | import uk.gov.hmrc.jenkinsjobbuilders.domain.Setting 4 | 5 | class GroupPermission implements Setting { 6 | 7 | final String permission 8 | final String ldapIdentifier 9 | 10 | private GroupPermission(String ldapIdentifier, String permission) { 11 | this.ldapIdentifier = ldapIdentifier 12 | this.permission = permission 13 | } 14 | 15 | static GroupPermission permissionSetting(String ldapIdentifier, String permission) { 16 | new GroupPermission(ldapIdentifier, permission) 17 | } 18 | 19 | @Override 20 | Closure toDsl() { 21 | return { 22 | delegate.groupPermission(permission, ldapIdentifier) 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/parameters/ChoiceParameter.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.parameters 2 | 3 | 4 | class ChoiceParameter implements Parameter { 5 | 6 | private final String name 7 | private final List values 8 | private final String description 9 | 10 | private ChoiceParameter(String name, List values, String description) { 11 | this.description = description 12 | this.values = values 13 | this.name = name 14 | } 15 | 16 | static ChoiceParameter choiceParameter(String name, List values, String description = null) { 17 | new ChoiceParameter(name, values, description) 18 | } 19 | 20 | @Override 21 | Closure toDsl() { 22 | return { 23 | choiceParam(name, values, description) 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/parameters/TextParameter.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.parameters 2 | 3 | 4 | class TextParameter implements Parameter { 5 | 6 | private final String name 7 | private final String defaultValue 8 | private final String description 9 | 10 | private TextParameter(String name, String defaultValue, String description) { 11 | this.description = description 12 | this.defaultValue = defaultValue 13 | this.name = name 14 | } 15 | 16 | static TextParameter textParameter(String name, String defaultValue, String description = null) { 17 | new TextParameter(name, defaultValue, description) 18 | } 19 | 20 | @Override 21 | Closure toDsl() { 22 | return { 23 | textParam(name, defaultValue, description) 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/parameters/StringParameter.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.parameters 2 | 3 | 4 | class StringParameter implements Parameter { 5 | 6 | private final String name 7 | private final String defaultValue 8 | private final String description 9 | 10 | private StringParameter(String name, String defaultValue, String description) { 11 | this.description = description 12 | this.defaultValue = defaultValue 13 | this.name = name 14 | } 15 | 16 | static StringParameter stringParameter(String name, String defaultValue, String description = null) { 17 | new StringParameter(name, defaultValue, description) 18 | } 19 | 20 | @Override 21 | Closure toDsl() { 22 | return { 23 | stringParam(name, defaultValue, description) 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/parameters/BooleanParameter.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.parameters 2 | 3 | 4 | class BooleanParameter implements Parameter { 5 | 6 | private final String name 7 | private final boolean defaultValue 8 | private final String description 9 | 10 | private BooleanParameter(String name, boolean defaultValue, String description) { 11 | this.description = description 12 | this.defaultValue = defaultValue 13 | this.name = name 14 | } 15 | 16 | static BooleanParameter booleanParameter(String name, boolean defaultValue, String description = null) { 17 | new BooleanParameter(name, defaultValue, description) 18 | } 19 | 20 | @Override 21 | Closure toDsl() { 22 | return { 23 | booleanParam(name, defaultValue, description) 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/configure/SCoverageReportsPublisher.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.configure 2 | 3 | final class SCoverageReportsPublisher implements Configure { 4 | 5 | private final String scalaVersion 6 | 7 | private SCoverageReportsPublisher(final String scalaVersion) { 8 | this.scalaVersion = scalaVersion 9 | } 10 | 11 | Closure toDsl() { 12 | new BaseSCoverageReportsPublisher( 13 | "target/scala-${scalaVersion}/scoverage-report", 14 | 'scoverage.xml' 15 | ).toDsl() 16 | } 17 | 18 | static SCoverageReportsPublisher sCoverageReportsPublisher() { 19 | new SCoverageReportsPublisher("2.11") 20 | } 21 | 22 | static SCoverageReportsPublisher sCoverageReportsPublisher(final String scalaVersion) { 23 | new SCoverageReportsPublisher(scalaVersion) 24 | } 25 | } -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/AbstractBuildDescriptionPublisher.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain 2 | 3 | abstract class AbstractBuildDescriptionPublisher implements Setting { 4 | protected final String description 5 | protected final String regex 6 | protected final String regularExpressionForFailed 7 | protected final String descriptionForFailed 8 | 9 | protected AbstractBuildDescriptionPublisher(String regex, String description, String regularExpressionForFailed, String descriptionForFailed) { 10 | this.regex = regex 11 | this.description = description 12 | this.regularExpressionForFailed = regularExpressionForFailed 13 | this.descriptionForFailed = descriptionForFailed 14 | } 15 | 16 | Closure toDsl() { 17 | return { 18 | buildDescription(regex, description, regularExpressionForFailed, descriptionForFailed) 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /src/test/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/publisher/GithubCommitNotifierPublisherSpec.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.publisher 2 | 3 | import javaposse.jobdsl.dsl.Job 4 | import uk.gov.hmrc.jenkinsjobbuilders.domain.AbstractJobSpec 5 | import uk.gov.hmrc.jenkinsjobbuilders.domain.builder.JobBuilder 6 | 7 | import static GithubCommitNotifierPublisher.githubCommitStatusPublisher 8 | 9 | class GithubCommitNotifierPublisherSpec extends AbstractJobSpec { 10 | 11 | void 'test XML output'() { 12 | given: 13 | JobBuilder jobBuilder = new JobBuilder('test-job', 'test-job-description'). 14 | withPublishers(githubCommitStatusPublisher()) 15 | 16 | when: 17 | Job job = jobBuilder.build(JOB_PARENT) 18 | 19 | then: 20 | with(job.node) { 21 | publishers.'com.cloudbees.jenkins.GitHubCommitNotifier' 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/wrapper/model/SecretUsernamePassword.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.wrapper.model 2 | 3 | class SecretUsernamePassword { 4 | 5 | final String userVariableName 6 | final String passwordVariableName 7 | final String credentials 8 | 9 | SecretUsernamePassword(String userVariableName, String passwordVariableName, String credentials) { 10 | this.userVariableName = userVariableName 11 | this.passwordVariableName = passwordVariableName 12 | this.credentials = credentials 13 | } 14 | 15 | static SecretUsernamePassword secretUsernamePassword(String userVariableName, 16 | String passwordVariableName, 17 | String credentials) { 18 | new SecretUsernamePassword(userVariableName, passwordVariableName, credentials) 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/scm/GitHubComScm.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.scm 2 | 3 | import static uk.gov.hmrc.jenkinsjobbuilders.domain.scm.GitHubScm.gitHubScm 4 | 5 | final class GitHubComScm implements Scm { 6 | private final Scm scm 7 | 8 | private GitHubComScm(String repository, String credentials, String branch, String refspec, int depth, String name = null) { 9 | this.scm = gitHubScm('github.com', repository, branch, 'ssh', refspec, credentials, name, depth) 10 | } 11 | 12 | static GitHubComScm gitHubComScm(String repository, String credentials) { 13 | gitHubComScm(repository, credentials, 'master') 14 | } 15 | 16 | static GitHubComScm gitHubComScm(String repository, String credentials, String branch, int depth = 0, String refspec = null) { 17 | new GitHubComScm(repository, credentials, branch, refspec, depth) 18 | } 19 | 20 | @Override 21 | Closure toDsl() { 22 | scm.toDsl() 23 | } 24 | } -------------------------------------------------------------------------------- /src/test/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/authorisation/PermissionSpec.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.authorisation 2 | 3 | import javaposse.jobdsl.dsl.Job 4 | import uk.gov.hmrc.jenkinsjobbuilders.domain.AbstractJobSpec 5 | import uk.gov.hmrc.jenkinsjobbuilders.domain.builder.JobBuilder 6 | 7 | import static uk.gov.hmrc.jenkinsjobbuilders.domain.authorisation.Permission.permissionSetting 8 | 9 | class PermissionSpec extends AbstractJobSpec { 10 | 11 | void 'test XML output'() { 12 | given: 13 | JobBuilder jobBuilder = new JobBuilder('test-job', 'test-job-description'). 14 | withPermissions(permissionSetting("dev-tools", 'hudson.model.Item.Read')) 15 | 16 | 17 | when: 18 | Job job = jobBuilder.build(JOB_PARENT) 19 | 20 | then: 21 | with(job.node) { 22 | properties.'hudson.security.AuthorizationMatrixProperty'.permission.text() == 'hudson.model.Item.Read:dev-tools' 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/test/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/configure/CheckStyleReportsPublisherSpec.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.configure 2 | 3 | import javaposse.jobdsl.dsl.Job 4 | import spock.lang.Specification 5 | import uk.gov.hmrc.jenkinsjobbuilders.domain.AbstractJobSpec 6 | import uk.gov.hmrc.jenkinsjobbuilders.domain.builder.JobBuilder 7 | 8 | 9 | import static CheckStyleReportsPublisher.checkStyleReportsPublisher 10 | 11 | class CheckStyleReportsPublisherSpec extends AbstractJobSpec { 12 | 13 | void 'test XML output'() { 14 | given: 15 | JobBuilder jobBuilder = new JobBuilder('test-job', 'test-job-description'). 16 | withConfigures(checkStyleReportsPublisher()) 17 | 18 | when: 19 | Job job = jobBuilder.build(JOB_PARENT) 20 | 21 | then: 22 | with(job.node) { 23 | publishers.'hudson.plugins.checkstyle.CheckStylePublisher'.pluginName.text() == '[CHECKSTYLE]' 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/test/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/builder/BuildMonitorViewBuilderSpec.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.builder 2 | 3 | import javaposse.jobdsl.dsl.View 4 | import uk.gov.hmrc.jenkinsjobbuilders.domain.AbstractJobSpec 5 | 6 | class BuildMonitorViewBuilderSpec extends AbstractJobSpec { 7 | 8 | def 'it should create a view with appropriate configuration'() { 9 | given: 10 | final BuildMonitorViewBuilder buildMonitorViewBuilder = new BuildMonitorViewBuilder("foo-team-name", "foo-view-name") 11 | .withJobs("foo-job-1-name", "foo-job-2-name") 12 | 13 | when: 14 | View buildMonitorView = buildMonitorViewBuilder.build(JOB_PARENT) 15 | 16 | then: 17 | with(buildMonitorView.node) { 18 | "false" == it.recurse.text() 19 | "false" == it.config.displayCommitters.text() 20 | it.jobNames[0].text().contains("foo-job-1-name") 21 | it.jobNames[0].text().contains("foo-job-2-name") 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/configure/XvfbBuildWrapper.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.configure 2 | 3 | final class XvfbBuildWrapper implements Configure { 4 | 5 | private final boolean parallel 6 | 7 | private XvfbBuildWrapper(boolean parallel) { 8 | this.parallel = parallel 9 | } 10 | 11 | Closure toDsl() { 12 | return { 13 | it / 'buildWrappers' / 'org.jenkinsci.plugins.xvfb.XvfbBuildWrapper' { 14 | 'installationName'('default') 15 | 'screen'('1920x1080x24') 16 | 'displayNameOffset'('1') 17 | 'debug'('true') 18 | 'timeout'('0') 19 | 'parallelBuild'("$parallel") 20 | 'shutdownWithBuild'('true') 21 | } 22 | } 23 | } 24 | 25 | static XvfbBuildWrapper xvfbBuildWrapper() { 26 | new XvfbBuildWrapper(false) 27 | } 28 | 29 | static XvfbBuildWrapper parallelXvfbBuildWrapper() { 30 | new XvfbBuildWrapper(true) 31 | } 32 | } -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/parameters/NodeParameter.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.parameters 2 | 3 | 4 | class NodeParameter implements Parameter { 5 | 6 | private final String name 7 | private final List nodes 8 | private final String triggerType 9 | 10 | private NodeParameter(String name, List nodes, String triggerType) { 11 | this.name = name 12 | this.nodes = nodes 13 | this.triggerType = triggerType 14 | } 15 | 16 | static NodeParameter nodeParameter(String name, List nodes, String triggerType) { 17 | new NodeParameter(name, nodes, triggerType) 18 | } 19 | 20 | @Override 21 | Closure toDsl() { 22 | return { 23 | nodeParam(name) { 24 | description(name) 25 | defaultNodes(nodes) 26 | allowedNodes(nodes) 27 | trigger(this.triggerType) 28 | eligibility('IgnoreOfflineNodeEligibility') 29 | } 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /src/test/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/AbstractJobSpec.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain 2 | 3 | import javaposse.jobdsl.dsl.JobManagement 4 | import javaposse.jobdsl.dsl.JobParent 5 | import javaposse.jobdsl.plugin.JenkinsJobManagement 6 | import org.junit.ClassRule 7 | import org.jvnet.hudson.test.JenkinsRule 8 | import spock.lang.Shared 9 | import spock.lang.Specification 10 | 11 | abstract class AbstractJobSpec extends Specification { 12 | 13 | protected static final JobManagement JOB_MANAGEMENT = new JenkinsJobManagement(System.out, [:], new File('.')) 14 | 15 | protected static final JobParent JOB_PARENT = createJobParent() 16 | 17 | @Shared 18 | @ClassRule 19 | JenkinsRule jenkinsRule = new JenkinsRule() 20 | 21 | private static final createJobParent() { 22 | JobParent jobParent = new JobParent() { 23 | @Override 24 | Object run() { 25 | return null 26 | } 27 | } 28 | 29 | jobParent.setJm(JOB_MANAGEMENT) 30 | jobParent 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/test/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/step/InjectEnvironmentVariablesStepSpec.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.step 2 | 3 | import javaposse.jobdsl.dsl.Job 4 | import spock.lang.Specification 5 | import uk.gov.hmrc.jenkinsjobbuilders.domain.AbstractJobSpec 6 | 7 | import uk.gov.hmrc.jenkinsjobbuilders.domain.builder.JobBuilder 8 | 9 | import static uk.gov.hmrc.jenkinsjobbuilders.domain.step.InjectEnvironmentVariablesStep.injectEnvironmentVariablesStep 10 | 11 | class InjectEnvironmentVariablesStepSpec extends AbstractJobSpec { 12 | 13 | void 'test XML output'() { 14 | given: 15 | JobBuilder jobBuilder = new JobBuilder('test-job', 'test-job-description'). 16 | withSteps(injectEnvironmentVariablesStep('env.properties')) 17 | 18 | when: 19 | Job job = jobBuilder.build(JOB_PARENT) 20 | 21 | then: 22 | with(job.node) { 23 | builders.'EnvInjectBuilder' [0].info.propertiesFilePath.text().contains("env.properties") 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/configure/CheckStyleReportsPublisher.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.configure 2 | 3 | final class CheckStyleReportsPublisher implements Configure { 4 | 5 | private CheckStyleReportsPublisher() {} 6 | 7 | static CheckStyleReportsPublisher checkStyleReportsPublisher() { 8 | new CheckStyleReportsPublisher() 9 | } 10 | 11 | Closure toDsl() { 12 | return { 13 | it / 'publishers' / 'hudson.plugins.checkstyle.CheckStylePublisher' { 14 | 'pluginName'('[CHECKSTYLE]') 15 | 'thresholdLimit'('low') 16 | 'canRunOnFailed'('false') 17 | 'usePreviousBuildAsReference'('false') 18 | 'useStableBuildAsReference'('false') 19 | 'useDeltaValues'('false') 20 | 'shouldDetectModules'('false') 21 | 'dontComputeNew'('true') 22 | 'doNotResolveRelativePaths'('true') 23 | 'pattern'('**/scalastyle-result.xml') 24 | } 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /src/test/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/wrapper/AbsoluteTimeoutWrapperSpec.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.wrapper 2 | 3 | import javaposse.jobdsl.dsl.Job 4 | import spock.lang.Specification 5 | import uk.gov.hmrc.jenkinsjobbuilders.domain.AbstractJobSpec 6 | import uk.gov.hmrc.jenkinsjobbuilders.domain.builder.JobBuilder 7 | 8 | 9 | class AbsoluteTimeoutWrapperSpec extends AbstractJobSpec { 10 | 11 | void 'test XML output'() { 12 | given: 13 | JobBuilder jobBuilder = new JobBuilder('test-job', 'test-job-description'). 14 | withWrappers(AbsoluteTimeoutWrapper.timeoutWrapper(30)) 15 | 16 | when: 17 | Job job = jobBuilder.build(JOB_PARENT) 18 | 19 | then: 20 | with(job.node) { 21 | buildWrappers.'hudson.plugins.build__timeout.BuildTimeoutWrapper'.strategy[0].attribute('class') == 'hudson.plugins.build_timeout.impl.AbsoluteTimeOutStrategy' 22 | buildWrappers.'hudson.plugins.build__timeout.BuildTimeoutWrapper'.strategy.timeoutMinutes.text() == '30' 23 | } 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/trigger/GitHubPullRequestTrigger.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.trigger 2 | 3 | 4 | final class GitHubPullRequestTrigger implements Trigger { 5 | private final String orgWhitelist 6 | private final String checkName 7 | 8 | private GitHubPullRequestTrigger(String checkName, String orgWhitelist) { 9 | this.orgWhitelist = orgWhitelist 10 | this.checkName = checkName 11 | } 12 | 13 | static GitHubPullRequestTrigger gitHubPullRequestTrigger(String checkName, String orgWhitelist) { 14 | new GitHubPullRequestTrigger(checkName, orgWhitelist) 15 | } 16 | 17 | @Override 18 | Closure toDsl() { 19 | return { 20 | githubPullRequest { 21 | admin("") 22 | cron("") 23 | orgWhitelist(this.orgWhitelist) 24 | useGitHubHooks() 25 | extensions { 26 | commitStatus { 27 | context(this.checkName) 28 | } 29 | } 30 | } 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/test/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/configure/BaseSCoverageReportsPublisherSpec.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.configure 2 | 3 | import uk.gov.hmrc.jenkinsjobbuilders.domain.AbstractJobSpec 4 | import uk.gov.hmrc.jenkinsjobbuilders.domain.builder.JobBuilder 5 | 6 | class BaseSCoverageReportsPublisherSpec extends AbstractJobSpec { 7 | 8 | void 'test XML output'() { 9 | given: 10 | def coverageReportDir = "target/scoverage-report" 11 | def coverageReportFile = "scoverage.xml" 12 | def jobBuilder = 13 | new JobBuilder('test-job', 'test-job-description') 14 | .withConfigures(new BaseSCoverageReportsPublisher(coverageReportDir, coverageReportFile)) 15 | 16 | when: 17 | def job = jobBuilder.build(JOB_PARENT) 18 | 19 | then: 20 | with(job.node) { 21 | publishers.'org.jenkinsci.plugins.scoverage.ScoveragePublisher'.reportDir.text() == 'target/scoverage-report' 22 | publishers.'org.jenkinsci.plugins.scoverage.ScoveragePublisher'.reportFile.text() == 'scoverage.xml' 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/test/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/trigger/UpstreamTriggerSpec.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.trigger 2 | 3 | import javaposse.jobdsl.dsl.Job 4 | import uk.gov.hmrc.jenkinsjobbuilders.domain.AbstractJobSpec 5 | import uk.gov.hmrc.jenkinsjobbuilders.domain.Condition 6 | import uk.gov.hmrc.jenkinsjobbuilders.domain.builder.JobBuilder 7 | 8 | import static uk.gov.hmrc.jenkinsjobbuilders.domain.trigger.UpstreamTrigger.upstreamTrigger 9 | 10 | class UpstreamTriggerSpec extends AbstractJobSpec { 11 | 12 | void 'test XML output'() { 13 | given: 14 | JobBuilder jobBuilder = 15 | new JobBuilder('test', 'description') 16 | .withTriggers(upstreamTrigger('upstream-job1,upstream-job2', Condition.SUCCESS)) 17 | 18 | when: 19 | Job job = jobBuilder.build(JOB_PARENT) 20 | 21 | then: 22 | with(job.node) { 23 | triggers.'jenkins.triggers.ReverseBuildTrigger'.upstreamProjects.text() == "upstream-job1,upstream-job2" 24 | triggers.'jenkins.triggers.ReverseBuildTrigger'.threshold.name.text() == "SUCCESS" 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/step/ShellStep.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.step 2 | 3 | 4 | class ShellStep implements Step { 5 | private final String command 6 | private final Integer unstableReturnCode 7 | 8 | private ShellStep(String command, Integer unstableReturnCode=null) { 9 | this.command = command 10 | this.unstableReturnCode = unstableReturnCode 11 | } 12 | 13 | /** 14 | * @param command shell cmd to run 15 | */ 16 | static Step shellStep(String command) { 17 | new ShellStep(command) 18 | } 19 | 20 | /** 21 | * @param command shell cmd to run 22 | * @param unstableReturnCode (optional) build will be marked as unstable if given return code set 23 | */ 24 | static Step shellStep(String command, int unstableReturnCode) { 25 | new ShellStep(command, unstableReturnCode) 26 | } 27 | 28 | @Override 29 | Closure toDsl() { 30 | return { 31 | shell { 32 | command(this.command) 33 | unstableReturn(this.unstableReturnCode) 34 | } 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/test/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/configure/RemoteTokenSpec.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.configure 2 | 3 | import javaposse.jobdsl.dsl.Job 4 | import uk.gov.hmrc.jenkinsjobbuilders.domain.AbstractJobSpec 5 | import uk.gov.hmrc.jenkinsjobbuilders.domain.builder.JobBuilder 6 | import uk.gov.hmrc.jenkinsjobbuilders.domain.step.RemoteTriggerStep 7 | 8 | class RemoteTokenSpec extends AbstractJobSpec { 9 | 10 | void 'test XML output'() { 11 | given: 12 | JobBuilder jobBuilder = new JobBuilder('test-job', 'test-job-description'). 13 | withSteps(RemoteTriggerStep.remoteTriggerStep("https://example.com", "test-job", [id: "test-id"], false)). 14 | withConfigures(RemoteToken.remoteToken('remote-trigger-token')) 15 | 16 | when: 17 | Job job = jobBuilder.build(JOB_PARENT) 18 | 19 | then: 20 | with(job.node) { 21 | builders.'org.jenkinsci.plugins.ParameterizedRemoteTrigger.RemoteBuildConfiguration' 22 | builders.'org.jenkinsci.plugins.ParameterizedRemoteTrigger.RemoteBuildConfiguration'.token.text() == "remote-trigger-token" 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/test/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/authorisation/GroupPermissionSpec.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.authorisation 2 | 3 | import javaposse.jobdsl.dsl.Job 4 | import uk.gov.hmrc.jenkinsjobbuilders.domain.AbstractJobSpec 5 | import uk.gov.hmrc.jenkinsjobbuilders.domain.builder.JobBuilder 6 | 7 | import static uk.gov.hmrc.jenkinsjobbuilders.domain.authorisation.GroupPermission.permissionSetting 8 | 9 | class GroupPermissionSpec extends AbstractJobSpec { 10 | 11 | void 'test XML output'() { 12 | given: 13 | JobBuilder jobBuilder = new JobBuilder('test-job', 'test-job-description'). 14 | withGroupPermissions(permissionSetting("dev-tools", 'hudson.model.Item.Read')) 15 | 16 | 17 | when: 18 | Job job = jobBuilder.build(JOB_PARENT) 19 | 20 | then: 21 | with(job.node) { 22 | def authorizationMatrixProperty = properties.'hudson.security.AuthorizationMatrixProperty' 23 | 24 | authorizationMatrixProperty.permission.text() == 'GROUP:hudson.model.Item.Read:dev-tools' 25 | authorizationMatrixProperty.blocksInheritance.text() == "false" 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/trigger/UpstreamTrigger.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.trigger 2 | 3 | import uk.gov.hmrc.jenkinsjobbuilders.domain.Condition 4 | 5 | class UpstreamTrigger implements Trigger { 6 | String upstreamJobs 7 | Condition condition 8 | 9 | private UpstreamTrigger(String upstreamJobs, Condition condition) { 10 | this.upstreamJobs = upstreamJobs 11 | this.condition = condition 12 | } 13 | 14 | /** 15 | * Creates a {@link jenkins.triggers.ReverseBuildTrigger} based on the successful completion of any of a list of upstream jobs. 16 | * @param upstreamJobs String with a comma separated list of jobs 17 | * @param {@link uk.gov.hmrc.jenkinsjobbuilders.domain.Condition} one of SUCCESS, UNSTABLE, FAILURE, defaults to SUCCESS 18 | * @return UpstreamTrigger 19 | */ 20 | static UpstreamTrigger upstreamTrigger(String upstreamJobs, Condition condition = Condition.SUCCESS) { 21 | new UpstreamTrigger(upstreamJobs, condition) 22 | } 23 | 24 | @Override 25 | Closure toDsl() { 26 | return { 27 | upstream(upstreamJobs, condition.name()) 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/step/SbtStep.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.step 2 | 3 | 4 | import static uk.gov.hmrc.jenkinsjobbuilders.domain.step.ShellStep.shellStep 5 | 6 | class SbtStep implements Step { 7 | 8 | private static final String EOL = System.getProperty("line.separator") 9 | private final Step step 10 | 11 | private SbtStep(String bashScript, List sbtCommands, String tmpDir) { 12 | this.step = shellStep(sbtCommands.inject(bashScriptString(bashScript, tmpDir)) { 13 | string, item -> string + EOL + "sbt $item -Djava.io.tmpdir=$tmpDir" 14 | }) 15 | } 16 | 17 | private static String bashScriptString(String bashScript, String tmpDir) { 18 | bashScript.isEmpty() ? "mkdir -p $tmpDir" : bashScript + EOL + "mkdir -p $tmpDir" 19 | } 20 | 21 | static Step sbtStep(List commands, String tmpDir) { 22 | sbtStep("", commands, tmpDir) 23 | } 24 | 25 | static Step sbtStep(String bashScript, List commands, String tmpDir) { 26 | new SbtStep(bashScript, commands, tmpDir) 27 | } 28 | 29 | @Override 30 | Closure toDsl() { 31 | step.toDsl() 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/test/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/throttle/ThrottleConfigurationSpec.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.throttle 2 | 3 | import javaposse.jobdsl.dsl.Job 4 | import spock.lang.Specification 5 | import uk.gov.hmrc.jenkinsjobbuilders.domain.AbstractJobSpec 6 | 7 | import uk.gov.hmrc.jenkinsjobbuilders.domain.builder.JobBuilder 8 | 9 | class ThrottleConfigurationSpec extends AbstractJobSpec { 10 | 11 | void 'test XML output'() { 12 | given: 13 | JobBuilder jobBuilder = new JobBuilder('test-job', 'test-job-description'). 14 | withThrottle(['deployment'], 0, 1, false) 15 | 16 | 17 | when: 18 | Job job = jobBuilder.build(JOB_PARENT) 19 | 20 | then: 21 | with(job.node) { 22 | properties.'hudson.plugins.throttleconcurrents.ThrottleJobProperty'.maxConcurrentPerNode.text() == '0' 23 | properties.'hudson.plugins.throttleconcurrents.ThrottleJobProperty'.maxConcurrentTotal.text() == '1' 24 | properties.'hudson.plugins.throttleconcurrents.ThrottleJobProperty'.throttleEnabled.text() == 'true' 25 | properties.'hudson.plugins.throttleconcurrents.ThrottleJobProperty'.categories.string.text() == 'deployment' 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/test/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/configure/CucumberReportsPublisherSpec.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.configure 2 | 3 | import javaposse.jobdsl.dsl.Job 4 | import uk.gov.hmrc.jenkinsjobbuilders.domain.AbstractJobSpec 5 | import uk.gov.hmrc.jenkinsjobbuilders.domain.builder.JobBuilder 6 | 7 | import static CucumberReportsPublisher.cucumberReportsPublisher 8 | 9 | class CucumberReportsPublisherSpec extends AbstractJobSpec { 10 | 11 | void 'test XML output'() { 12 | given: 13 | JobBuilder jobBuilder = new JobBuilder('test-job', 'test-job-description'). 14 | withConfigures(cucumberReportsPublisher()) 15 | 16 | when: 17 | Job job = jobBuilder.build(JOB_PARENT) 18 | 19 | then: 20 | with(job.node) { 21 | publishers.'net.masterthought.jenkins.CucumberReportPublisher'.jsonReportDirectory.text() == 'target' 22 | publishers.'net.masterthought.jenkins.CucumberReportPublisher'.fileIncludePattern.text() == 'cucumber.json' 23 | publishers.'net.masterthought.jenkins.CucumberReportPublisher'.fileExcludePattern.text() == '' 24 | publishers.'net.masterthought.jenkins.CucumberReportPublisher'.buildStatus.text() == 'FAILURE' 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/test/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/parameters/TextParametersSpec.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.parameters 2 | 3 | import javaposse.jobdsl.dsl.Job 4 | import spock.lang.Specification 5 | import uk.gov.hmrc.jenkinsjobbuilders.domain.AbstractJobSpec 6 | import uk.gov.hmrc.jenkinsjobbuilders.domain.builder.JobBuilder 7 | 8 | class TextParametersSpec extends AbstractJobSpec { 9 | 10 | void 'test XML output'() { 11 | given: 12 | JobBuilder jobBuilder = new JobBuilder('test-job', 'test-job-description'). 13 | withParameters(TextParameter.textParameter("test-param", "", "some text parameter")) 14 | 15 | when: 16 | Job job = jobBuilder.build(JOB_PARENT) 17 | 18 | then: 19 | with(job.node) { 20 | properties.'hudson.model.ParametersDefinitionProperty'.parameterDefinitions.'hudson.model.TextParameterDefinition'[0].name.text() == 'test-param' 21 | properties.'hudson.model.ParametersDefinitionProperty'.parameterDefinitions.'hudson.model.TextParameterDefinition'[0].defaultValue.text() == '' 22 | properties.'hudson.model.ParametersDefinitionProperty'.parameterDefinitions.'hudson.model.TextParameterDefinition'[0].description.text() == 'some text parameter' 23 | } 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/publisher/HtmlReportsPublisher.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.publisher 2 | 3 | 4 | final class HtmlReportsPublisher implements Publisher { 5 | private final Map htmlReportDirs 6 | private boolean keep 7 | private boolean alwaysLinkLatest 8 | 9 | private HtmlReportsPublisher(Map htmlReportDirs, boolean keep = false, boolean alwaysLinkLatest = false) { 10 | this.htmlReportDirs = htmlReportDirs 11 | this.keep = keep 12 | this.alwaysLinkLatest = alwaysLinkLatest 13 | } 14 | 15 | static HtmlReportsPublisher htmlReportsPublisher(Map htmlReportDirs, boolean keep = false, boolean alwaysLinkLatest = false) { 16 | new HtmlReportsPublisher(htmlReportDirs, keep, alwaysLinkLatest) 17 | } 18 | 19 | @Override 20 | Closure toDsl() { 21 | return { 22 | publishHtml { 23 | htmlReportDirs.each { dir, name -> 24 | report(dir) { 25 | reportName(name) 26 | allowMissing(true) 27 | keepAll(keep) 28 | alwaysLinkToLastBuild(alwaysLinkLatest) 29 | } 30 | } 31 | } 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/test/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/wrapper/TimestampsWrapperSpec.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.wrapper 2 | 3 | import javaposse.jobdsl.dsl.Job 4 | import spock.lang.Specification 5 | import uk.gov.hmrc.jenkinsjobbuilders.domain.AbstractJobSpec 6 | import uk.gov.hmrc.jenkinsjobbuilders.domain.builder.JobBuilder 7 | 8 | import static uk.gov.hmrc.jenkinsjobbuilders.domain.wrapper.TimestampsWrapper.timestampsWrapper 9 | 10 | class TimestampsWrapperSpec extends AbstractJobSpec { 11 | 12 | void 'test with timstamper'() { 13 | given: 14 | JobBuilder jobBuilder = new JobBuilder('test-job', 'test-job-description') 15 | .withWrappers(timestampsWrapper()) 16 | 17 | when: 18 | Job job = jobBuilder.build(JOB_PARENT) 19 | 20 | then: 21 | with(job.node) { 22 | buildWrappers.'hudson.plugins.timestamper.TimestamperBuildWrapper' 23 | } 24 | } 25 | 26 | void 'test without timstamper'() { 27 | given: 28 | JobBuilder jobBuilder = new JobBuilder('test-job', 'test-job-description') 29 | 30 | when: 31 | Job job = jobBuilder.build(JOB_PARENT) 32 | 33 | then: 34 | with(job.node) { 35 | ! buildWrappers.'hudson.plugins.timestamper.TimestamperBuildWrapper' 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/throttle/ThrottleConfiguration.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.throttle 2 | 3 | import uk.gov.hmrc.jenkinsjobbuilders.domain.Setting 4 | 5 | class ThrottleConfiguration implements Setting { 6 | 7 | final List categoriesList 8 | final int maxConcurrentPerNode 9 | final int maxConcurrentTotal 10 | final boolean disableThrottle 11 | 12 | private ThrottleConfiguration(List categories, int maxConcurrentPerNode, int maxConcurrentTotal, boolean disableThrottle) { 13 | this.categoriesList = categories 14 | this.maxConcurrentPerNode = maxConcurrentPerNode 15 | this.maxConcurrentTotal = maxConcurrentTotal 16 | this.disableThrottle = disableThrottle 17 | } 18 | 19 | static ThrottleConfiguration throttleConfiguration(List categories, int maxConcurrentPerNode, int maxConcurrentTotal, boolean throttleDisabled) { 20 | new ThrottleConfiguration(categories, maxConcurrentPerNode, maxConcurrentTotal, throttleDisabled) 21 | } 22 | 23 | @Override 24 | Closure toDsl() { 25 | return { 26 | maxPerNode(this.maxConcurrentPerNode) 27 | maxTotal(this.maxConcurrentTotal) 28 | categories(this.categoriesList) 29 | throttleDisabled(this.disableThrottle) 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/builder/ListViewBuilder.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.builder 2 | 3 | import javaposse.jobdsl.dsl.DslFactory 4 | import javaposse.jobdsl.dsl.View 5 | import javaposse.jobdsl.dsl.views.ListView 6 | 7 | final class ListViewBuilder implements Builder { 8 | 9 | private final String name 10 | private String jobsRegex 11 | private boolean scoverageReports = false 12 | private Boolean recurseSubFolders = false 13 | 14 | ListViewBuilder(String name) { 15 | this.name = name 16 | } 17 | 18 | ListViewBuilder withJobsRegex(String jobsRegex) { 19 | this.jobsRegex = jobsRegex 20 | this 21 | } 22 | 23 | ListViewBuilder withRecurseSubFolders() { 24 | this.recurseSubFolders = true 25 | this 26 | } 27 | 28 | @Override 29 | View build(DslFactory dslFactory) { 30 | dslFactory.listView(this.name) { 31 | recurse(this.recurseSubFolders) 32 | jobs { 33 | regex(this.jobsRegex) 34 | } 35 | columns { 36 | status() 37 | weather() 38 | name() 39 | lastSuccess() 40 | lastFailure() 41 | lastDuration() 42 | buildButton() 43 | } 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/test/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/parameters/BooleanParametersSpec.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.parameters 2 | 3 | import javaposse.jobdsl.dsl.Job 4 | import spock.lang.Specification 5 | import uk.gov.hmrc.jenkinsjobbuilders.domain.AbstractJobSpec 6 | import uk.gov.hmrc.jenkinsjobbuilders.domain.builder.JobBuilder 7 | 8 | 9 | class BooleanParametersSpec extends AbstractJobSpec { 10 | 11 | void 'test XML output'() { 12 | given: 13 | JobBuilder jobBuilder = new JobBuilder('test-job', 'test-job-description'). 14 | withParameters(BooleanParameter.booleanParameter("test-param", false, "some boolean parameter")) 15 | 16 | when: 17 | Job job = jobBuilder.build(JOB_PARENT) 18 | 19 | then: 20 | with(job.node) { 21 | properties.'hudson.model.ParametersDefinitionProperty'.parameterDefinitions.'hudson.model.BooleanParameterDefinition'[0].name.text() == 'test-param' 22 | properties.'hudson.model.ParametersDefinitionProperty'.parameterDefinitions.'hudson.model.BooleanParameterDefinition'[0].defaultValue.text() == 'false' 23 | properties.'hudson.model.ParametersDefinitionProperty'.parameterDefinitions.'hudson.model.BooleanParameterDefinition'[0].description.text() == 'some boolean parameter' 24 | } 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/configure/InheritanceStrategy.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 HM Revenue & Customs 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.gov.hmrc.jenkinsjobbuilders.domain.configure 18 | 19 | 20 | enum InheritanceStrategy { 21 | INHERIT_GLOBAL_STRATEGY("org.jenkinsci.plugins.matrixauth.inheritance.InheritGlobalStrategy"), 22 | INHERIT_PARENT_STRATEGY("org.jenkinsci.plugins.matrixauth.inheritance.InheritParentStrategy"), 23 | NON_INHERITING_STRATEGY("org.jenkinsci.plugins.matrixauth.inheritance.NonInheritingStrategy") 24 | 25 | private final String className 26 | 27 | InheritanceStrategy(final String className) { 28 | this.className = className 29 | } 30 | 31 | String getClassName() { 32 | return className 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/trigger/BintrayArtifactTrigger.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.trigger; 2 | 3 | public class BintrayArtifactTrigger implements Trigger { 4 | private final String cronSchedule 5 | private final String subject 6 | private final String repo 7 | private final List packagesToPoll 8 | 9 | private BintrayArtifactTrigger(String cronSchedule, String subject, String repo, List packagesToPoll) { 10 | this.cronSchedule = cronSchedule 11 | this.subject = subject 12 | this.repo = repo 13 | this.packagesToPoll = packagesToPoll 14 | } 15 | 16 | static BintrayArtifactTrigger bintrayArtifactTrigger(String cron, String subject, String repo, List packagesToPoll) { 17 | new BintrayArtifactTrigger(cron, subject, repo, packagesToPoll) 18 | } 19 | 20 | @Override 21 | Closure toDsl() { 22 | return { 23 | urlTrigger{ 24 | cron(cronSchedule) 25 | 26 | packagesToPoll.each{ 27 | url(String.format("https://api.bintray.com/packages/%s/%s/%s/", subject, repo, it)){ 28 | inspection('json'){ 29 | path('latest_version') 30 | } 31 | } 32 | } 33 | } 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/publisher/JobsTriggerPublisher.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.publisher 2 | 3 | import static java.util.Collections.emptyMap 4 | 5 | 6 | final class JobsTriggerPublisher implements Publisher { 7 | private final def name 8 | private final def condition 9 | private final def parameters = new HashMap() 10 | 11 | private JobsTriggerPublisher(String name, String condition, Map parameters) { 12 | this.name = name 13 | this.condition = condition 14 | this.parameters.putAll(parameters) 15 | } 16 | 17 | static JobsTriggerPublisher jobsTriggerPublisher(String name, String condition = 'SUCCESS', Map parameters = emptyMap()) { 18 | new JobsTriggerPublisher(name, condition, parameters) 19 | } 20 | 21 | @Override 22 | Closure toDsl() { 23 | return { 24 | downstreamParameterized { 25 | trigger(name) { 26 | condition(this.condition) 27 | if(parameters.isEmpty()) { 28 | triggerWithNoParameters() 29 | } 30 | else { 31 | parameters { 32 | predefinedProps(this.parameters) 33 | } 34 | } 35 | } 36 | } 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/test/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/step/RemoteTriggerStepSpec.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.step 2 | 3 | import javaposse.jobdsl.dsl.Job 4 | import uk.gov.hmrc.jenkinsjobbuilders.domain.AbstractJobSpec 5 | import uk.gov.hmrc.jenkinsjobbuilders.domain.builder.JobBuilder 6 | 7 | class RemoteTriggerStepSpec extends AbstractJobSpec { 8 | 9 | void 'test XML output'() { 10 | given: 11 | JobBuilder jobBuilder = new JobBuilder('test-job', 'test-job-description'). 12 | withSteps(RemoteTriggerStep.remoteTriggerStep("https://example.com", "test-job", [id: "test-id"], false)) 13 | 14 | when: 15 | Job job = jobBuilder.build(JOB_PARENT) 16 | 17 | then: 18 | with(job.node) { 19 | builders.'org.jenkinsci.plugins.ParameterizedRemoteTrigger.RemoteBuildConfiguration' 20 | builders.'org.jenkinsci.plugins.ParameterizedRemoteTrigger.RemoteBuildConfiguration'.remoteJenkinsName.text() == "https://example.com" 21 | builders.'org.jenkinsci.plugins.ParameterizedRemoteTrigger.RemoteBuildConfiguration'.job.text() == "test-job" 22 | builders.'org.jenkinsci.plugins.ParameterizedRemoteTrigger.RemoteBuildConfiguration'.parameters.text() == "id=test-id" 23 | builders.'org.jenkinsci.plugins.ParameterizedRemoteTrigger.RemoteBuildConfiguration'.shouldNotFailBuild.text() == "true" 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/configure/BaseSCoverageReportsPublisher.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.configure 2 | 3 | /** 4 | * Allows fine-grained control of setting the location of the generated coverage report 5 | * to be published. 6 | * 7 | * Since version 1.9.0 of `sbt-scoverage`, reports may be configured to be written to 8 | * arbitrary locations (which, as such, no longer imposes a `scalac` version specific 9 | * directory structure.) 10 | */ 11 | final class BaseSCoverageReportsPublisher implements Configure { 12 | 13 | private final String coverageReportDir 14 | private final String coverageReportFile 15 | 16 | BaseSCoverageReportsPublisher(String coverageReportDir, String coverageReportFile) { 17 | this.coverageReportDir = coverageReportDir 18 | this.coverageReportFile = coverageReportFile 19 | } 20 | 21 | @Override 22 | Closure toDsl() { 23 | return { 24 | it / 'publishers' / 'org.jenkinsci.plugins.scoverage.ScoveragePublisher' { 25 | 'reportDir'(coverageReportDir) 26 | 'reportFile'(coverageReportFile) 27 | } 28 | } 29 | } 30 | 31 | static final BaseSCoverageReportsPublisher versionAgnosticSCoverageReportsPublisher() { 32 | new BaseSCoverageReportsPublisher( 33 | "target/scoverage-report", 34 | "scoverage.xml" 35 | ) 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/publisher/BuildDescriptionPublisher.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.publisher 2 | 3 | import uk.gov.hmrc.jenkinsjobbuilders.domain.AbstractBuildDescriptionPublisher 4 | 5 | final class BuildDescriptionPublisher extends AbstractBuildDescriptionPublisher implements Publisher { 6 | 7 | 8 | private BuildDescriptionPublisher(String regex, String description, String regularExpressionForFailed = '', String descriptionForFailed = '') { 9 | super(regex, description, regularExpressionForFailed, descriptionForFailed) 10 | } 11 | 12 | static BuildDescriptionPublisher buildDescriptionByTextPublisher(String description, String regularExpressionForFailed = '', String descriptionForFailed = '') { 13 | buildDescriptionPublisher('', description, regularExpressionForFailed, descriptionForFailed) 14 | } 15 | 16 | static BuildDescriptionPublisher buildDescriptionByRegexPublisher(String regex, String regularExpressionForFailed = '', String descriptionForFailed = '') { 17 | buildDescriptionPublisher(regex, '', regularExpressionForFailed, descriptionForFailed) 18 | } 19 | 20 | private static BuildDescriptionPublisher buildDescriptionPublisher(String regex, String description, String regularExpressionForFailed = '', String descriptionForFailed = '') { 21 | new BuildDescriptionPublisher(regex, description, regularExpressionForFailed, descriptionForFailed) 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/test/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/trigger/GitHubPullRequestTriggerSpec.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.trigger 2 | 3 | import javaposse.jobdsl.dsl.Job 4 | import spock.lang.Specification 5 | import uk.gov.hmrc.jenkinsjobbuilders.domain.AbstractJobSpec 6 | import uk.gov.hmrc.jenkinsjobbuilders.domain.builder.JobBuilder 7 | 8 | 9 | import static uk.gov.hmrc.jenkinsjobbuilders.domain.trigger.GitHubPullRequestTrigger.gitHubPullRequestTrigger 10 | 11 | class GitHubPullRequestTriggerSpec extends AbstractJobSpec { 12 | 13 | /* 14 | Ideally we would test this functionality, but the org.jenkins-ci.plugins:ghprb plugin it depends on causes 15 | other plugins to be downgraded, breaking other tests 16 | */ 17 | 18 | /* 19 | void 'test XML output'() { 20 | given: 21 | JobBuilder jobBuilder = new JobBuilder('test-job', 'test-job-description'). 22 | withTriggers(gitHubPullRequestTrigger('my-context', 'my-github-org')) 23 | 24 | when: 25 | Job job = jobBuilder.build(JOB_PARENT) 26 | 27 | then: 28 | with(job.node) { 29 | triggers.'org.jenkinsci.plugins.ghprb.GhprbTrigger'.orgslist.text() == "my-github-org" 30 | triggers.'org.jenkinsci.plugins.ghprb.GhprbTrigger'.extensions.'org.jenkinsci.plugins.ghprb.extensions.status.GhprbSimpleStatus'.commitStatusContext.text() == "my-context" 31 | } 32 | } 33 | */ 34 | } 35 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/configure/CucumberReportsPublisher.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.configure 2 | 3 | final class CucumberReportsPublisher implements Configure { 4 | 5 | private CucumberReportsPublisher() {} 6 | 7 | private String jsonReportDirectory = 'target' 8 | private String fileIncludePattern = 'cucumber.json' 9 | private String fileExcludePattern = '' 10 | 11 | CucumberReportsPublisher withJsonReportDirectory(String jsonReportDirectory) { 12 | this.jsonReportDirectory = jsonReportDirectory 13 | this 14 | } 15 | 16 | CucumberReportsPublisher withFileIncludePattern(String fileIncludePattern) { 17 | this.fileIncludePattern = fileIncludePattern 18 | this 19 | } 20 | 21 | CucumberReportsPublisher withFileExludePattern(String fileExcludePattern) { 22 | this.fileExcludePattern = fileExcludePattern 23 | this 24 | } 25 | 26 | Closure toDsl() { 27 | return { 28 | it / 'publishers' / 'net.masterthought.jenkins.CucumberReportPublisher' { 29 | 'jsonReportDirectory'(jsonReportDirectory) 30 | 'fileIncludePattern'(fileIncludePattern) 31 | 'fileExcludePattern'(fileExcludePattern) 32 | 'buildStatus'('FAILURE') 33 | } 34 | } 35 | } 36 | 37 | static CucumberReportsPublisher cucumberReportsPublisher() { 38 | new CucumberReportsPublisher() 39 | } 40 | 41 | } -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/publisher/CleanWorkspacePostBuildTaskPublisher.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.publisher 2 | 3 | import static uk.gov.hmrc.jenkinsjobbuilders.domain.publisher.PostBuildTaskPublisher.postBuildTaskPublisher 4 | 5 | class CleanWorkspacePostBuildTaskPublisher implements Publisher { 6 | private final Publisher publisher 7 | 8 | private CleanWorkspacePostBuildTaskPublisher() { 9 | this.publisher = postBuildTaskPublisher('Building(.*)', """\ 10 | |#!/bin/bash 11 | |cd \${WORKSPACE} 12 | |find . -maxdepth 1 -not -path . -not -path '*target*' -not -path '*logs*' -not -path '*results*' -type d | xargs rm -rf 13 | |if [ -d target ]; then 14 | | find target -mindepth 2 -not -path '*report*' -not -path '*gatling*' \\( -type f -o -type d -empty \\) -delete 15 | |fi 16 | """.stripMargin()) 17 | } 18 | 19 | static Publisher cleanWorkspacePostBuildTaskPublisher() { 20 | new CleanWorkspacePostBuildTaskPublisher() 21 | } 22 | 23 | @Override 24 | Closure toDsl() { 25 | return publisher.toDsl() 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/test/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/step/AwsCodeBuildStepSpec.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.step 2 | 3 | import javaposse.jobdsl.dsl.Job 4 | import uk.gov.hmrc.jenkinsjobbuilders.domain.AbstractJobSpec 5 | import uk.gov.hmrc.jenkinsjobbuilders.domain.builder.JobBuilder 6 | import uk.gov.hmrc.jenkinsjobbuilders.domain.configure.Configure 7 | 8 | 9 | class AwsCodeBuildStepSpec extends AbstractJobSpec { 10 | 11 | private JobBuilder jobBuilder 12 | 13 | def setup() { 14 | jobBuilder = new JobBuilder('test-job', 'test-job-description') 15 | } 16 | 17 | 18 | def "it should add a code builder step to the job"() { 19 | given: 20 | final AwsCodeBuildStep step = new AwsCodeBuildStepBuilder() 21 | .withCredentialsId("some-credentials-id") 22 | .withProjectName("test-project-name") 23 | .build() 24 | jobBuilder.withConfigures([step] as List) 25 | 26 | when: 27 | Job job = jobBuilder.build(JOB_PARENT) 28 | 29 | then: 30 | with(job.node) { 31 | builders.CodeBuilder[0].'credentialsType'.text().contains('jenkins') 32 | builders.CodeBuilder[0].'credentialsId'.text().contains('some-credentials-id') 33 | builders.CodeBuilder[0].'projectName'.text().contains('test-project-name') 34 | builders.CodeBuilder[0].'region'.text().contains('eu-west-2') 35 | builders.CodeBuilder[0].'gitCloneDepthOverride'.text()contains('') 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/test/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/configure/SCoverageReportsPublisherSpec.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.configure 2 | 3 | import javaposse.jobdsl.dsl.Job 4 | import spock.lang.Specification 5 | import uk.gov.hmrc.jenkinsjobbuilders.domain.AbstractJobSpec 6 | import uk.gov.hmrc.jenkinsjobbuilders.domain.builder.JobBuilder 7 | 8 | 9 | import static SCoverageReportsPublisher.sCoverageReportsPublisher 10 | 11 | class SCoverageReportsPublisherSpec extends AbstractJobSpec { 12 | 13 | void 'test XML output'() { 14 | given: 15 | JobBuilder jobBuilder = new JobBuilder('test-job', 'test-job-description'). 16 | withConfigures(sCoverageReportsPublisher()) 17 | 18 | when: 19 | Job job = jobBuilder.build(JOB_PARENT) 20 | 21 | then: 22 | with(job.node) { 23 | publishers.'org.jenkinsci.plugins.scoverage.ScoveragePublisher'.reportDir.text() == 'target/scala-2.11/scoverage-report' 24 | } 25 | } 26 | 27 | void 'test XML output with scala-2.12'() { 28 | given: 29 | JobBuilder jobBuilder = new JobBuilder('test-job', 'test-job-description'). 30 | withConfigures(sCoverageReportsPublisher("2.12")) 31 | 32 | when: 33 | Job job = jobBuilder.build(JOB_PARENT) 34 | 35 | then: 36 | with(job.node) { 37 | publishers.'org.jenkinsci.plugins.scoverage.ScoveragePublisher'.reportDir.text() == 'target/scala-2.12/scoverage-report' 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/test/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/publisher/JobsTriggerPublisherSpec.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.publisher 2 | 3 | import javaposse.jobdsl.dsl.Job 4 | import spock.lang.Specification 5 | import uk.gov.hmrc.jenkinsjobbuilders.domain.AbstractJobSpec 6 | 7 | import uk.gov.hmrc.jenkinsjobbuilders.domain.builder.JobBuilder 8 | 9 | import static uk.gov.hmrc.jenkinsjobbuilders.domain.publisher.JobsTriggerPublisher.* 10 | 11 | class JobsTriggerPublisherSpec extends AbstractJobSpec { 12 | 13 | void 'test XML output'() { 14 | given: 15 | JobBuilder jobBuilder = new JobBuilder('test-job', 'test-job-description'). 16 | withPublishers(jobsTriggerPublisher('test-project', 17 | 'FAILED', ['key1': 'value1', 'key2': 'value2'])) 18 | 19 | when: 20 | Job job = jobBuilder.build(JOB_PARENT) 21 | 22 | then: 23 | with(job.node) { 24 | 25 | def buildTriggerConfig = publishers 26 | .'hudson.plugins.parameterizedtrigger.BuildTrigger'.configs 27 | .'hudson.plugins.parameterizedtrigger.BuildTriggerConfig' 28 | buildTriggerConfig.projects.text() == 'test-project' 29 | buildTriggerConfig.condition.text() == 'FAILED' 30 | buildTriggerConfig.configs.'hudson.plugins.parameterizedtrigger.PredefinedBuildParameters' 31 | .properties.text() == "key1=value1\nkey2=value2" 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/test/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/step/ShellStepSpec.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.step 2 | 3 | import javaposse.jobdsl.dsl.Job 4 | import spock.lang.Specification 5 | import uk.gov.hmrc.jenkinsjobbuilders.domain.AbstractJobSpec 6 | import uk.gov.hmrc.jenkinsjobbuilders.domain.builder.JobBuilder 7 | 8 | 9 | import static uk.gov.hmrc.jenkinsjobbuilders.domain.step.ShellStep.shellStep 10 | 11 | class ShellStepSpec extends AbstractJobSpec { 12 | 13 | void 'test XML output when no unstableReturnCode set'() { 14 | given: 15 | JobBuilder jobBuilder = new JobBuilder('test-job', 'test-job-description'). 16 | withSteps(shellStep('test-shell')) 17 | 18 | when: 19 | Job job = jobBuilder.build(JOB_PARENT) 20 | 21 | then: 22 | with(job.node) { 23 | builders.'hudson.tasks.Shell' [0].command.text().contains("test-shell") 24 | builders.'hudson.tasks.Shell' [0].unstableReturn == [] 25 | } 26 | } 27 | 28 | void 'test optional unstableReturnCode'() { 29 | given: 30 | JobBuilder jobBuilder = new JobBuilder('test-job', 'test-job-description'). 31 | withSteps(shellStep('test-shell', 22)) 32 | 33 | when: 34 | Job job = jobBuilder.build(JOB_PARENT) 35 | 36 | then: 37 | with(job.node) { 38 | builders.'hudson.tasks.Shell' [0].command.text().contains("test-shell") 39 | builders.'hudson.tasks.Shell' [0].unstableReturn.text() == 22.toString() 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/test/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/publisher/PostBuildTaskPublisherSpec.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.publisher 2 | 3 | import javaposse.jobdsl.dsl.Job 4 | import spock.lang.Specification 5 | import uk.gov.hmrc.jenkinsjobbuilders.domain.AbstractJobSpec 6 | import uk.gov.hmrc.jenkinsjobbuilders.domain.builder.JobBuilder 7 | 8 | 9 | import static uk.gov.hmrc.jenkinsjobbuilders.domain.publisher.PostBuildTaskPublisher.postBuildTaskPublisher 10 | 11 | class PostBuildTaskPublisherSpec extends AbstractJobSpec { 12 | 13 | void 'test XML output'() { 14 | given: 15 | JobBuilder jobBuilder = new JobBuilder('test-job', 'test-job-description'). 16 | withPublishers(postBuildTaskPublisher("test-log-text", 'test-script')) 17 | 18 | when: 19 | Job job = jobBuilder.build(JOB_PARENT) 20 | 21 | then: 22 | with(job.node) { 23 | publishers.'hudson.plugins.postbuildtask.PostbuildTask'.tasks.'hudson.plugins.postbuildtask.TaskProperties'.logTexts.'hudson.plugins.postbuildtask.LogProperties'.logText.text() == 'test-log-text' 24 | publishers.'hudson.plugins.postbuildtask.PostbuildTask'.tasks.'hudson.plugins.postbuildtask.TaskProperties'.EscalateStatus.text() == 'true' 25 | publishers.'hudson.plugins.postbuildtask.PostbuildTask'.tasks.'hudson.plugins.postbuildtask.TaskProperties'.RunIfJobSuccessful.text() == 'false' 26 | publishers.'hudson.plugins.postbuildtask.PostbuildTask'.tasks.'hudson.plugins.postbuildtask.TaskProperties'.script.text() == 'test-script' 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/configure/RemoteToken.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.configure 2 | 3 | /* 4 | * Copyright 2018 HM Revenue & Customs 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | import uk.gov.hmrc.jenkinsjobbuilders.domain.configure.Configure 20 | 21 | final class RemoteToken implements Configure { 22 | 23 | private final String remoteToken 24 | 25 | private RemoteToken(String token) { 26 | this.remoteToken = token 27 | } 28 | 29 | static RemoteToken remoteToken(String token) { 30 | new RemoteToken(token) 31 | } 32 | 33 | @Override 34 | Closure toDsl() { 35 | return { 36 | // using findAll here, to ensure that all RemoteBuildConfiguration entries are updated - the / operator only finds the first child 37 | (it / builders).value().findAll { it.name() == 'org.jenkinsci.plugins.ParameterizedRemoteTrigger.RemoteBuildConfiguration' }.each { config -> 38 | (config / token).setValue(remoteToken) 39 | } 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/wrapper/EnvironmentVariablesWrapper.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.wrapper 2 | 3 | import uk.gov.hmrc.jenkinsjobbuilders.domain.variable.EnvironmentVariable 4 | 5 | class EnvironmentVariablesWrapper implements Wrapper { 6 | private final String variablesFile 7 | private final String scriptContent 8 | private final String groovyScriptContent 9 | private final List variables 10 | 11 | private EnvironmentVariablesWrapper(String variablesFile, List variables, 12 | String scriptContent, String groovyScriptContent) { 13 | this.variablesFile = variablesFile 14 | this.variables = variables 15 | this.scriptContent = scriptContent 16 | this.groovyScriptContent = groovyScriptContent 17 | } 18 | 19 | static Wrapper environmentVariablesWrapper(String variablesFile, List variables, 20 | String scriptContent = '', String groovyScriptContent = '') { 21 | new EnvironmentVariablesWrapper(variablesFile, variables, scriptContent, groovyScriptContent) 22 | } 23 | 24 | @Override 25 | Closure toDsl() { 26 | return { 27 | environmentVariables { 28 | variables.each { variable -> 29 | env(variable.name, variable.value) 30 | } 31 | propertiesFile(variablesFile) 32 | script(scriptContent) 33 | groovy(groovyScriptContent) 34 | } 35 | 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/test/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/publisher/CleanXvfbPostBuildTaskPublisherSpec.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.publisher 2 | 3 | import javaposse.jobdsl.dsl.Job 4 | import spock.lang.Specification 5 | import uk.gov.hmrc.jenkinsjobbuilders.domain.AbstractJobSpec 6 | import uk.gov.hmrc.jenkinsjobbuilders.domain.builder.JobBuilder 7 | 8 | 9 | import static uk.gov.hmrc.jenkinsjobbuilders.domain.publisher.CleanXvfbPostBuildTaskPublisher.cleanXvfbPostBuildTaskPublisher 10 | 11 | class CleanXvfbPostBuildTaskPublisherSpec extends AbstractJobSpec { 12 | 13 | void 'test XML output'() { 14 | given: 15 | JobBuilder jobBuilder = new JobBuilder('test-job', 'test-job-description'). 16 | withPublishers(cleanXvfbPostBuildTaskPublisher()) 17 | 18 | when: 19 | Job job = jobBuilder.build(JOB_PARENT) 20 | 21 | then: 22 | with(job.node) { 23 | publishers.'hudson.plugins.postbuildtask.PostbuildTask'.tasks.'hudson.plugins.postbuildtask.TaskProperties'.logTexts.'hudson.plugins.postbuildtask.LogProperties'.logText.text() == 'Xvfb starting(.*)' 24 | publishers.'hudson.plugins.postbuildtask.PostbuildTask'.tasks.'hudson.plugins.postbuildtask.TaskProperties'.EscalateStatus.text() == 'true' 25 | publishers.'hudson.plugins.postbuildtask.PostbuildTask'.tasks.'hudson.plugins.postbuildtask.TaskProperties'.RunIfJobSuccessful.text() == 'false' 26 | publishers.'hudson.plugins.postbuildtask.PostbuildTask'.tasks.'hudson.plugins.postbuildtask.TaskProperties'.script.text().contains('pkill Xvfb') 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/publisher/PostBuildScriptPublisher.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.publisher 2 | 3 | import javaposse.jobdsl.dsl.DslFactory 4 | import uk.gov.hmrc.jenkinsjobbuilders.domain.publisher.Publisher 5 | 6 | final class PostBuildScriptPublisher implements Publisher { 7 | private final ArrayList results 8 | private final String script 9 | private final Boolean stopOnFailure 10 | 11 | private PostBuildScriptPublisher(String script, ArrayList results, Boolean stopOnFailure) { 12 | this.script = script 13 | this.results = results 14 | this.stopOnFailure = stopOnFailure 15 | } 16 | 17 | static PostBuildScriptPublisher postBuildScriptPublisher(String script, ArrayList results, Boolean stopOnFailure = false) { 18 | new PostBuildScriptPublisher(script, results, stopOnFailure) 19 | } 20 | 21 | 22 | 23 | @Override 24 | Closure toDsl() { 25 | return { 26 | postBuildScript { 27 | buildSteps { 28 | postBuildStep { 29 | stopOnFailure(this.stopOnFailure) 30 | results(results*.name()) 31 | buildSteps { 32 | shell { 33 | command(script) 34 | } 35 | } 36 | } 37 | } 38 | markBuildUnstable(false) 39 | } 40 | } 41 | } 42 | } 43 | 44 | enum Result { 45 | SUCCESS, 46 | UNSTABLE, 47 | FAILURE, 48 | NOT_BUILT, 49 | ABORTED 50 | } 51 | -------------------------------------------------------------------------------- /src/test/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/publisher/CleanWorkspacePostBuildTaskPublisherSpec.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.publisher 2 | 3 | import javaposse.jobdsl.dsl.Job 4 | import spock.lang.Specification 5 | import uk.gov.hmrc.jenkinsjobbuilders.domain.AbstractJobSpec 6 | import uk.gov.hmrc.jenkinsjobbuilders.domain.builder.JobBuilder 7 | 8 | 9 | import static uk.gov.hmrc.jenkinsjobbuilders.domain.publisher.CleanWorkspacePostBuildTaskPublisher.cleanWorkspacePostBuildTaskPublisher 10 | 11 | class CleanWorkspacePostBuildTaskPublisherSpec extends AbstractJobSpec { 12 | 13 | void 'test XML output'() { 14 | given: 15 | JobBuilder jobBuilder = new JobBuilder('test-job', 'test-job-description'). 16 | withPublishers(cleanWorkspacePostBuildTaskPublisher()) 17 | 18 | when: 19 | Job job = jobBuilder.build(JOB_PARENT) 20 | 21 | then: 22 | with(job.node) { 23 | publishers.'hudson.plugins.postbuildtask.PostbuildTask'.tasks.'hudson.plugins.postbuildtask.TaskProperties'.logTexts.'hudson.plugins.postbuildtask.LogProperties'.logText.text() == 'Building(.*)' 24 | publishers.'hudson.plugins.postbuildtask.PostbuildTask'.tasks.'hudson.plugins.postbuildtask.TaskProperties'.EscalateStatus.text() == 'true' 25 | publishers.'hudson.plugins.postbuildtask.PostbuildTask'.tasks.'hudson.plugins.postbuildtask.TaskProperties'.RunIfJobSuccessful.text() == 'false' 26 | publishers.'hudson.plugins.postbuildtask.PostbuildTask'.tasks.'hudson.plugins.postbuildtask.TaskProperties'.script.text().contains('xargs rm -rf') 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/publisher/CleanXvfbPostBuildTaskPublisher.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.publisher 2 | 3 | import static uk.gov.hmrc.jenkinsjobbuilders.domain.publisher.PostBuildTaskPublisher.postBuildTaskPublisher 4 | 5 | 6 | final class CleanXvfbPostBuildTaskPublisher implements Publisher { 7 | private final Publisher publisher 8 | 9 | private CleanXvfbPostBuildTaskPublisher() { 10 | this.publisher = postBuildTaskPublisher('Xvfb starting(.*)', """\ 11 | |#!/bin/bash 12 | |ps cax | grep Xvfb > /dev/null 13 | |if [ \$? -eq 0 ]; then 14 | | echo "Cleaning up Xvfb" 15 | | pkill Xvfb 16 | | if [ -e /tmp/.X99-lock ] 17 | | then 18 | | unlink /tmp/.X99-lock 19 | | fi 20 | |fi 21 | """.stripMargin()) 22 | } 23 | 24 | static Publisher cleanXvfbPostBuildTaskPublisher() { 25 | new CleanXvfbPostBuildTaskPublisher() 26 | } 27 | 28 | @Override 29 | Closure toDsl() { 30 | return publisher.toDsl() 31 | } 32 | } -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/builder/BuildMonitorViewBuilder.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.builder 2 | 3 | import javaposse.jobdsl.dsl.DslFactory 4 | import javaposse.jobdsl.dsl.View 5 | 6 | 7 | final class BuildMonitorViewBuilder implements Builder { 8 | 9 | private final String name 10 | private String [] jobs 11 | private String includeRegex 12 | private Boolean recurseSubFolders = false 13 | 14 | BuildMonitorViewBuilder(String nameOrTeam, String name = '') { 15 | 16 | // nameOrTeam is there for backwards compatibility, where the name was the first parameter 17 | if(name == '') { 18 | this.name = nameOrTeam 19 | } else { 20 | this.name = "$nameOrTeam/$name" 21 | } 22 | } 23 | 24 | BuildMonitorViewBuilder withJobs(String ... jobs) { 25 | this.jobs = jobs 26 | this 27 | } 28 | 29 | BuildMonitorViewBuilder withJobsMatching(String includeRegex) { 30 | this.includeRegex = includeRegex 31 | this 32 | } 33 | 34 | BuildMonitorViewBuilder recurseSubFolders() { 35 | this.recurseSubFolders = true 36 | this 37 | } 38 | 39 | @Override 40 | View build(DslFactory dslFactory) { 41 | dslFactory.buildMonitorView(this.name) { 42 | recurse(recurseSubFolders) 43 | jobs { 44 | names(this.jobs) 45 | if(includeRegex != null) { 46 | regex(includeRegex) 47 | } 48 | } 49 | configure { node -> 50 | node / config { 51 | displayCommitters 'false' 52 | } 53 | } 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/test/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/configure/XvfbBuildWrapperSpec.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.configure 2 | 3 | import javaposse.jobdsl.dsl.Job 4 | import spock.lang.Specification 5 | import uk.gov.hmrc.jenkinsjobbuilders.domain.AbstractJobSpec 6 | import uk.gov.hmrc.jenkinsjobbuilders.domain.builder.JobBuilder 7 | 8 | 9 | import static XvfbBuildWrapper.parallelXvfbBuildWrapper 10 | import static XvfbBuildWrapper.xvfbBuildWrapper 11 | 12 | class XvfbBuildWrapperSpec extends AbstractJobSpec { 13 | 14 | void 'test XML output'() { 15 | given: 16 | JobBuilder jobBuilder = new JobBuilder('test-job', 'test-job-description'). 17 | withConfigures(xvfbBuildWrapper()) 18 | 19 | when: 20 | Job job = jobBuilder.build(JOB_PARENT) 21 | 22 | then: 23 | with(job.node) { 24 | buildWrappers.'org.jenkinsci.plugins.xvfb.XvfbBuildWrapper'.parallelBuild.text() == 'false' 25 | buildWrappers.'org.jenkinsci.plugins.xvfb.XvfbBuildWrapper'.displayName.isEmpty() 26 | buildWrappers.'org.jenkinsci.plugins.xvfb.XvfbBuildWrapper'.displayNameOffset.text() == '1' 27 | buildWrappers.'org.jenkinsci.plugins.xvfb.XvfbBuildWrapper'.screen.text() == '1920x1080x24' 28 | } 29 | } 30 | 31 | void 'test parallel XML output'() { 32 | given: 33 | JobBuilder jobBuilder = new JobBuilder('test-job', 'test-job-description'). 34 | withConfigures(parallelXvfbBuildWrapper()) 35 | 36 | when: 37 | Job job = jobBuilder.build(JOB_PARENT) 38 | 39 | then: 40 | with(job.node) { 41 | buildWrappers.'org.jenkinsci.plugins.xvfb.XvfbBuildWrapper'.parallelBuild.text() == 'true' 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/configure/FolderInheritanceStrategy.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 HM Revenue & Customs 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.gov.hmrc.jenkinsjobbuilders.domain.configure 18 | 19 | class FolderInheritanceStrategy implements Configure { 20 | 21 | private final InheritanceStrategy inheritanceStrategy 22 | 23 | private FolderInheritanceStrategy(final InheritanceStrategy inheritanceStrategy) { 24 | this.inheritanceStrategy = inheritanceStrategy 25 | } 26 | 27 | @Override 28 | Closure toDsl() { 29 | return { folder -> 30 | /** 31 | * The inheritanceStrategy node _must_ be added as the 0th element, otherwise Jenkins doesn't recognise it when it 32 | * parses the XML. 33 | */ 34 | final Node matrix = folder / 'properties' / 'com.cloudbees.hudson.plugins.folder.properties.AuthorizationMatrixProperty' 35 | final NodeList matrixChildren = matrix.value() 36 | matrixChildren.add(0, new Node(null, "inheritanceStrategy", ['class': inheritanceStrategy.className])) 37 | } 38 | } 39 | 40 | static FolderInheritanceStrategy folderInheritanceStrategy(final InheritanceStrategy inheritanceStrategy) { 41 | return new FolderInheritanceStrategy(inheritanceStrategy) 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/publisher/PostBuildCleanWsPublisher.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.publisher 2 | 3 | class PostBuildCleanWsPublisher implements Publisher { 4 | 5 | private final boolean cleanAbortedBuild 6 | private final boolean cleanFailBuild 7 | private final boolean cleanNotBuilt 8 | private final boolean cleanSuccessfulBuild 9 | private final boolean cleanUnstableBuild 10 | private final boolean notFailBuild 11 | 12 | private PostBuildCleanWsPublisher(boolean cleanAbortedBuild, boolean cleanFailBuild, boolean cleanNotBuilt, boolean cleanSuccessfulBuild, boolean cleanUnstableBuild, boolean notFailBuild) { 13 | this.cleanAbortedBuild = cleanAbortedBuild 14 | this.cleanFailBuild = cleanFailBuild 15 | this.cleanNotBuilt = cleanNotBuilt 16 | this.cleanSuccessfulBuild = cleanSuccessfulBuild 17 | this.cleanUnstableBuild = cleanUnstableBuild 18 | this.notFailBuild = notFailBuild 19 | } 20 | 21 | static Publisher postBuildCleanWsPublisher(boolean cleanAbortedBuild = true, boolean cleanFailBuild = true, boolean cleanNotBuilt = true, boolean cleanSuccessfulBuild = true, boolean cleanUnstableBuild = true, boolean notFailBuild = true) { 22 | new PostBuildCleanWsPublisher(cleanAbortedBuild, cleanFailBuild, cleanNotBuilt, cleanSuccessfulBuild, cleanUnstableBuild, notFailBuild) 23 | } 24 | 25 | @Override 26 | Closure toDsl() { 27 | return { 28 | cleanWs { 29 | cleanWhenAborted(this.cleanAbortedBuild) 30 | cleanWhenFailure(this.cleanFailBuild) 31 | cleanWhenNotBuilt(this.cleanNotBuilt) 32 | cleanWhenSuccess(this.cleanSuccessfulBuild) 33 | cleanWhenUnstable(this.cleanUnstableBuild) 34 | notFailBuild(this.notFailBuild) 35 | } 36 | } 37 | } 38 | } -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/step/RemoteTriggerStep.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.step 2 | 3 | /* 4 | * Copyright 2018 HM Revenue & Customs 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | import uk.gov.hmrc.jenkinsjobbuilders.domain.step.Step 20 | 21 | class RemoteTriggerStep implements Step { 22 | private final String remoteHost 23 | private final String remoteJob 24 | private final Map parameterMap 25 | private final boolean failIfRemoteBuildFails 26 | 27 | private RemoteTriggerStep(String remoteHost, String remoteJob, Map parameters, boolean failIfRemoteBuildFails) { 28 | this.remoteHost = remoteHost 29 | this.remoteJob = remoteJob 30 | this.parameterMap = parameters 31 | this.failIfRemoteBuildFails = failIfRemoteBuildFails 32 | } 33 | 34 | static Step remoteTriggerStep(String remoteHost, String remoteJob, Map parameters, boolean failIfRemoteBuildFails = true) { 35 | new RemoteTriggerStep(remoteHost, remoteJob, parameters, failIfRemoteBuildFails) 36 | } 37 | 38 | @Override 39 | Closure toDsl() { 40 | return { 41 | remoteTrigger(this.remoteHost, this.remoteJob) { 42 | pollInterval(10) 43 | parameters(this.parameterMap) 44 | blockBuildUntilComplete() 45 | shouldNotFailBuild(!failIfRemoteBuildFails) 46 | } 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/configure/ProjectBasedSecurityProperty.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 HM Revenue & Customs 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.gov.hmrc.jenkinsjobbuilders.domain.configure 18 | 19 | import uk.gov.hmrc.jenkinsjobbuilders.domain.authorisation.Permission 20 | 21 | class ProjectBasedSecurityProperty implements Configure { 22 | 23 | private final InheritanceStrategy inheritanceStrategy 24 | private final Set permissions 25 | 26 | private ProjectBasedSecurityProperty(final InheritanceStrategy inheritanceStrategy, 27 | final Set permissions) { 28 | this.inheritanceStrategy = inheritanceStrategy 29 | this.permissions = permissions 30 | } 31 | 32 | static ProjectBasedSecurityProperty enableProjectBasedSecurity(final InheritanceStrategy inheritanceStrategy, 33 | final Set permissions) { 34 | return new ProjectBasedSecurityProperty(inheritanceStrategy, 35 | permissions) 36 | } 37 | 38 | @Override 39 | Closure toDsl() { 40 | return { 41 | it / 'properties' / 'hudson.security.AuthorizationMatrixProperty' { 42 | 'inheritanceStrategy'('class': inheritanceStrategy.className) 43 | permissions.each { permission -> 44 | delegate.permission("${permission.permission}:${permission.ldapIdentifier}") 45 | } 46 | } 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/test/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/configure/FolderInheritanceStrategySpec.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.configure 2 | 3 | import javaposse.jobdsl.dsl.Folder 4 | import uk.gov.hmrc.jenkinsjobbuilders.domain.AbstractJobSpec 5 | import uk.gov.hmrc.jenkinsjobbuilders.domain.authorisation.Permission 6 | import uk.gov.hmrc.jenkinsjobbuilders.domain.builder.FolderBuilder 7 | 8 | import static uk.gov.hmrc.jenkinsjobbuilders.domain.authorisation.Permission.permissionSetting 9 | import static uk.gov.hmrc.jenkinsjobbuilders.domain.configure.FolderInheritanceStrategy.folderInheritanceStrategy 10 | import static uk.gov.hmrc.jenkinsjobbuilders.domain.configure.InheritanceStrategy.NON_INHERITING_STRATEGY 11 | 12 | class FolderInheritanceStrategySpec extends AbstractJobSpec { 13 | 14 | def "text XML output"() { 15 | given: 16 | final InheritanceStrategy inheritanceStrategy = NON_INHERITING_STRATEGY 17 | 18 | final Set permissions = [ 19 | permissionSetting("ci-admin", "hudson.model.Item.Read") 20 | ] 21 | 22 | FolderBuilder jobBuilder = new FolderBuilder('test-folder') 23 | .withPermissions(permissions) 24 | .withConfigures([folderInheritanceStrategy(inheritanceStrategy)]) 25 | 26 | when: 27 | Folder folder = jobBuilder.build(JOB_PARENT) 28 | 29 | then: 30 | with(folder.node) { 31 | final NodeList configuredPermissions = it.'properties'.'com.cloudbees.hudson.plugins.folder.properties.AuthorizationMatrixProperty'[0].value() 32 | configuredPermissions.size() == 2 33 | 34 | final Node inheritanceStrategyNode = configuredPermissions.first() 35 | 'inheritanceStrategy' == inheritanceStrategyNode.name() 36 | "org.jenkinsci.plugins.matrixauth.inheritance.NonInheritingStrategy" == inheritanceStrategyNode["@class"] 37 | 38 | final Node permissionNode = configuredPermissions.last() 39 | 'permission' == permissionNode.name() && 40 | "hudson.model.Item.Read:ci-admin" == permissionNode.text() 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/configure/PyEnvBuildWrapper.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.configure 2 | 3 | class PyEnvBuildWrapper implements Configure { 4 | 5 | private final String defaultPythonVersion 6 | private final boolean ignoreProjectPythonVersion 7 | private final List pipSpecfication 8 | 9 | private PyEnvBuildWrapper(final String defaultPythonVersion, 10 | final boolean ignoreProjectPythonVersion, 11 | final List pipSpecfication) { 12 | 13 | this.defaultPythonVersion = defaultPythonVersion 14 | this.ignoreProjectPythonVersion = ignoreProjectPythonVersion 15 | this.pipSpecfication = pipSpecfication 16 | } 17 | 18 | static PyEnvBuildWrapper pyEnvBuildWrapper(final String defaultPythonVersion = null, 19 | final boolean ignoreProjectPythonVersion = false, 20 | final List pipSpecfication = []) { 21 | new PyEnvBuildWrapper(defaultPythonVersion, 22 | ignoreProjectPythonVersion, 23 | pipSpecfication) 24 | } 25 | 26 | Closure toDsl() { 27 | return { 28 | it / 'buildWrappers' / 'ruby-proxy-object' / 'ruby-object'('ruby-class': 'Jenkins::Tasks::BuildWrapperProxy', pluginid: 'pyenv') { 29 | 'pluginid'('pyenv', [pluginid: 'pyenv', 'ruby-class': 'String']) 30 | object('ruby-class': 'PyenvWrapper', pluginid: 'pyenv') { 31 | pyenv__root('$HOME/.pyenv', [pluginid: 'pyenv', 'ruby-class': 'String']) 32 | pip__list(pipSpecfication.sort().join(','), [pluginid: 'pyenv', 'ruby-class': 'String']) 33 | pyenv__revision('master', [pluginid: 'pyenv', 'ruby-class': 'String']) 34 | version(defaultPythonVersion, [pluginid: 'pyenv', 'ruby-class': 'String']) 35 | ignore__local__version('false', [pluginid: 'pyenv', 'ruby-class': ignoreProjectPythonVersion ? 'TrueClass' : 'FalseClass']) 36 | pyenv__repository('https://github.com/yyuu/pyenv.git', [pluginid: 'pyenv', 'ruby-class': 'String']) 37 | } 38 | } 39 | } 40 | } 41 | } 42 | 43 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/configure/InjectEnvironmentJobProperty.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.configure 2 | 3 | final class InjectEnvironmentJobProperty implements Configure { 4 | 5 | private InjectEnvironmentJobProperty() {} 6 | 7 | private String propertiesContent = '' 8 | private String propertiesFilePath = '' 9 | private String scriptFilePath = '' 10 | private String scriptContent = '' 11 | 12 | InjectEnvironmentJobProperty withPropertiesContent(String propertiesContent) { 13 | this.propertiesContent = propertiesContent 14 | this 15 | } 16 | 17 | InjectEnvironmentJobProperty withPropertiesFilePath(String propertiesFilePath) { 18 | this.propertiesFilePath = propertiesFilePath 19 | this 20 | } 21 | 22 | InjectEnvironmentJobProperty withScriptFilePath(String scriptFilePath) { 23 | this.scriptFilePath = scriptFilePath 24 | this 25 | } 26 | 27 | InjectEnvironmentJobProperty withScriptContent(String scriptContent) { 28 | this.scriptContent = scriptContent 29 | this 30 | } 31 | 32 | Closure toDsl() { 33 | return { 34 | it / 'properties' << 'EnvInjectJobProperty' { 35 | info { 36 | loadFilesFromMaster false 37 | if ( this.propertiesContent != '' ) { 38 | propertiesContent this.propertiesContent 39 | } 40 | if ( this.propertiesFilePath != '' ) { 41 | propertiesFilePath this.propertiesFilePath 42 | } 43 | if ( this.scriptFilePath != '' ) { 44 | scriptFilePath this.scriptFilePath 45 | } 46 | if ( this.scriptContent != '' ) { 47 | scriptContent this.scriptContent 48 | } 49 | } 50 | keepBuildVariables true 51 | keepJenkinsSystemVariables true 52 | overrideBuildParameters false 53 | on true 54 | } 55 | } 56 | } 57 | 58 | 59 | static InjectEnvironmentJobProperty injectEnvironmentJobProperty() { 60 | new InjectEnvironmentJobProperty() 61 | } 62 | 63 | } -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/configure/ProjectBasedGroupSecurityProperty.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 HM Revenue & Customs 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.gov.hmrc.jenkinsjobbuilders.domain.configure 18 | 19 | import uk.gov.hmrc.jenkinsjobbuilders.domain.configure.InheritanceStrategy 20 | import uk.gov.hmrc.jenkinsjobbuilders.domain.authorisation.GroupPermission 21 | 22 | class ProjectBasedGroupSecurityProperty implements Configure { 23 | 24 | private final InheritanceStrategy inheritanceStrategy 25 | private final Set permissions 26 | 27 | private ProjectBasedGroupSecurityProperty(final InheritanceStrategy inheritanceStrategy, 28 | final Set permissions) { 29 | this.inheritanceStrategy = inheritanceStrategy 30 | this.permissions = permissions 31 | } 32 | 33 | static ProjectBasedGroupSecurityProperty enableProjectBasedSecurity( final InheritanceStrategy inheritanceStrategy, 34 | final Set permissions) { 35 | return new ProjectBasedGroupSecurityProperty(inheritanceStrategy, 36 | permissions) 37 | } 38 | 39 | @Override 40 | Closure toDsl() { 41 | return { 42 | it / 'properties' / 'hudson.security.AuthorizationMatrixProperty' { 43 | 'inheritanceStrategy'('class': inheritanceStrategy.className) 44 | permissions.each { permission -> 45 | delegate.permission("GROUP:${permission.permission}:${permission.ldapIdentifier}") 46 | } 47 | } 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/test/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/step/RemoteTriggerStep2Spec.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.step 2 | 3 | import javaposse.jobdsl.dsl.Job 4 | import uk.gov.hmrc.jenkinsjobbuilders.domain.AbstractJobSpec 5 | import uk.gov.hmrc.jenkinsjobbuilders.domain.builder.JobBuilder 6 | 7 | class RemoteTriggerStep2Spec extends AbstractJobSpec { 8 | 9 | void 'test XML output'() { 10 | given: 11 | JobBuilder jobBuilder = new JobBuilder('test-job', 'test-job-description'). 12 | withConfigures( 13 | RemoteTriggerStep2.remoteTriggerStep("https://example.com", "test-job", [id: "test-id", foo: "bar"], false) 14 | .withRemoteJobToken("remote-token") 15 | ) 16 | 17 | when: 18 | Job job = jobBuilder.build(JOB_PARENT) 19 | 20 | then: 21 | with(job.node) { 22 | builders.'org.jenkinsci.plugins.ParameterizedRemoteTrigger.RemoteBuildConfiguration' 23 | builders.'org.jenkinsci.plugins.ParameterizedRemoteTrigger.RemoteBuildConfiguration'.remoteJenkinsName.text() == "https://example.com" 24 | builders.'org.jenkinsci.plugins.ParameterizedRemoteTrigger.RemoteBuildConfiguration'.job.text() == "test-job" 25 | builders.'org.jenkinsci.plugins.ParameterizedRemoteTrigger.RemoteBuildConfiguration'.token.text() == "remote-token" 26 | builders.'org.jenkinsci.plugins.ParameterizedRemoteTrigger.RemoteBuildConfiguration'.shouldNotFailBuild.text() == "true" 27 | builders.'org.jenkinsci.plugins.ParameterizedRemoteTrigger.RemoteBuildConfiguration'.parameters2.parameters.'org.jenkinsci.plugins.ParameterizedRemoteTrigger.parameters2.MapParameter'[0].name.text() == "id" 28 | builders.'org.jenkinsci.plugins.ParameterizedRemoteTrigger.RemoteBuildConfiguration'.parameters2.parameters.'org.jenkinsci.plugins.ParameterizedRemoteTrigger.parameters2.MapParameter'[0].value.text() == "test-id" 29 | builders.'org.jenkinsci.plugins.ParameterizedRemoteTrigger.RemoteBuildConfiguration'.parameters2.parameters.'org.jenkinsci.plugins.ParameterizedRemoteTrigger.parameters2.MapParameter'[1].name.text() == "foo" 30 | builders.'org.jenkinsci.plugins.ParameterizedRemoteTrigger.RemoteBuildConfiguration'.parameters2.parameters.'org.jenkinsci.plugins.ParameterizedRemoteTrigger.parameters2.MapParameter'[1].value.text() == "bar" 31 | 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/test/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/configure/InjectEnvironmentjobPropertySpec.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.configure 2 | 3 | import javaposse.jobdsl.dsl.Job 4 | import uk.gov.hmrc.jenkinsjobbuilders.domain.AbstractJobSpec 5 | import uk.gov.hmrc.jenkinsjobbuilders.domain.builder.JobBuilder 6 | 7 | import static InjectEnvironmentJobProperty.injectEnvironmentJobProperty 8 | 9 | class InjectEnvironmentJobPropertySpec extends AbstractJobSpec { 10 | 11 | void 'test XML output'() { 12 | given: 13 | JobBuilder jobBuilder = new JobBuilder('test-job', 'test-job-description'). 14 | withConfigures(injectEnvironmentJobProperty()) 15 | 16 | when: 17 | Job job = jobBuilder.build(JOB_PARENT) 18 | 19 | then: 20 | println(job.node) 21 | with(job.node) { 22 | properties.EnvInjectJobProperty.info.propertiesContent.text() == '' 23 | properties.EnvInjectJobProperty.info.propertiesFilePath.text() == '' 24 | properties.EnvInjectJobProperty.info.scriptFilePath.text() == '' 25 | properties.EnvInjectJobProperty.info.scriptContent.text() == '' 26 | } 27 | } 28 | void 'test XML output with parameters'() { 29 | given: 30 | JobBuilder jobBuilder = new JobBuilder('test-job', 'test-job-description'). 31 | withConfigures(injectEnvironmentJobProperty() 32 | .withPropertiesContent('PropertyContent') 33 | .withPropertiesFilePath('PropertyFile') 34 | .withScriptFilePath('PropertyScriptFile') 35 | .withScriptContent('PropertyScript') 36 | ) 37 | 38 | when: 39 | Job job = jobBuilder.build(JOB_PARENT) 40 | 41 | then: 42 | println(job.node) 43 | with(job.node) { 44 | properties.EnvInjectJobProperty.info.propertiesContent.text() == 'PropertyContent' 45 | properties.EnvInjectJobProperty.info.propertiesFilePath.text() == 'PropertyFile' 46 | properties.EnvInjectJobProperty.info.scriptFilePath.text() == 'PropertyScriptFile' 47 | properties.EnvInjectJobProperty.info.scriptContent.text() == 'PropertyScript' 48 | } 49 | } 50 | } 51 | 52 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/parameters/ActiveChoiceReactiveParameter.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.parameters 2 | 3 | final class ActiveChoiceReactiveParameter implements Parameter { 4 | 5 | final String name 6 | final String choiceType 7 | final String description 8 | final String referencedParameters 9 | final String script 10 | final String fallbackScript 11 | final boolean filterable 12 | 13 | private ActiveChoiceReactiveParameter(String name, 14 | String choiceType, 15 | String referencedParameters, 16 | String script, 17 | String fallbackScript, 18 | boolean filterable, 19 | String description) { 20 | this.name = name 21 | this.choiceType = choiceType 22 | this.description = description 23 | this.filterable = filterable 24 | this.script = script 25 | this.fallbackScript = fallbackScript 26 | this.referencedParameters = referencedParameters 27 | } 28 | 29 | static ActiveChoiceReactiveParameter activeChoiceReactiveParameter(String name, 30 | String choiceType, 31 | String referencedParameters, 32 | String script, 33 | String fallbackScript = null, 34 | boolean filterable = false, 35 | String description = null) { 36 | new ActiveChoiceReactiveParameter(name, choiceType, referencedParameters, script, fallbackScript, filterable, description) 37 | } 38 | 39 | @Override 40 | Closure toDsl() { 41 | return { 42 | activeChoiceReactiveParam(this.name) { 43 | description(this.description) 44 | filterable(this.filterable) 45 | choiceType(this.choiceType) 46 | groovyScript { 47 | script(this.script) 48 | fallbackScript(this.fallbackScript) 49 | } 50 | referencedParameter(this.referencedParameters) 51 | } 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/wrapper/CredentialsBindings.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.wrapper 2 | 3 | import uk.gov.hmrc.jenkinsjobbuilders.domain.Setting 4 | import uk.gov.hmrc.jenkinsjobbuilders.domain.wrapper.model.ConjoinedSecretUsernamePassword 5 | import uk.gov.hmrc.jenkinsjobbuilders.domain.wrapper.model.SecretText 6 | import uk.gov.hmrc.jenkinsjobbuilders.domain.wrapper.model.SecretUsernamePassword 7 | 8 | class CredentialsBindings implements Setting { 9 | 10 | private final List secretTexts 11 | 12 | private final List secretUsernamePasswords 13 | 14 | private final List conjoinedSecretUsernamePassword 15 | 16 | CredentialsBindings(List secretTexts, List secretUsernamePasswords, 17 | List conjoinedSecretUsernamePassword) { 18 | this.secretTexts = secretTexts 19 | this.secretUsernamePasswords = secretUsernamePasswords 20 | this.conjoinedSecretUsernamePassword = conjoinedSecretUsernamePassword 21 | } 22 | 23 | static CredentialsBindings credentialsBindings(List secretTexts, 24 | List secretUsernamePasswords, 25 | List conjoinedSecretUsernamePassword = []) { 26 | new CredentialsBindings(secretTexts, secretUsernamePasswords, conjoinedSecretUsernamePassword) 27 | } 28 | 29 | static CredentialsBindings combineCredentialsBindings(CredentialsBindings first, CredentialsBindings second) { 30 | if (first == null) { 31 | return second 32 | } else if (second == null) { 33 | return first 34 | } else { 35 | return credentialsBindings( 36 | first.secretTexts + second.secretTexts, 37 | first.secretUsernamePasswords + second.secretUsernamePasswords, 38 | first.conjoinedSecretUsernamePassword + second.conjoinedSecretUsernamePassword 39 | ) 40 | } 41 | } 42 | 43 | @Override 44 | Closure toDsl() { 45 | return { 46 | credentialsBinding { 47 | secretTexts.each { secretText -> 48 | string(secretText.variable, secretText.credentials) 49 | } 50 | conjoinedSecretUsernamePassword.each { conjoinedSecretUsernamePassword -> 51 | usernamePassword(conjoinedSecretUsernamePassword.variableName, conjoinedSecretUsernamePassword.credentials) 52 | } 53 | secretUsernamePasswords.each { s -> 54 | usernamePassword(s.userVariableName, s.passwordVariableName, s.credentials) 55 | } 56 | } 57 | } 58 | } 59 | } -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%"=="" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%"=="" set DIRNAME=. 29 | @rem This is normally unused 30 | set APP_BASE_NAME=%~n0 31 | set APP_HOME=%DIRNAME% 32 | 33 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 34 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 35 | 36 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 37 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 38 | 39 | @rem Find java.exe 40 | if defined JAVA_HOME goto findJavaFromJavaHome 41 | 42 | set JAVA_EXE=java.exe 43 | %JAVA_EXE% -version >NUL 2>&1 44 | if %ERRORLEVEL% equ 0 goto execute 45 | 46 | echo. 47 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 48 | echo. 49 | echo Please set the JAVA_HOME variable in your environment to match the 50 | echo location of your Java installation. 51 | 52 | goto fail 53 | 54 | :findJavaFromJavaHome 55 | set JAVA_HOME=%JAVA_HOME:"=% 56 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 57 | 58 | if exist "%JAVA_EXE%" goto execute 59 | 60 | echo. 61 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 62 | echo. 63 | echo Please set the JAVA_HOME variable in your environment to match the 64 | echo location of your Java installation. 65 | 66 | goto fail 67 | 68 | :execute 69 | @rem Setup the command line 70 | 71 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 72 | 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if %ERRORLEVEL% equ 0 goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | set EXIT_CODE=%ERRORLEVEL% 85 | if %EXIT_CODE% equ 0 set EXIT_CODE=1 86 | if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% 87 | exit /b %EXIT_CODE% 88 | 89 | :mainEnd 90 | if "%OS%"=="Windows_NT" endlocal 91 | 92 | :omega 93 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/scm/GitHubScm.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.scm 2 | 3 | final class GitHubScm implements Scm { 4 | private final String repository 5 | private final String protocol 6 | private final String host 7 | private final String branch 8 | private final String refspec 9 | private final String credentials 10 | private final String name 11 | private final int depth 12 | private final boolean honorRefspec 13 | private final boolean pullTags 14 | private final int timeout 15 | 16 | private GitHubScm(String host, String repository, String branch, String protocol, String refspec, String credentials, String name, int depth, boolean honorRefspec, boolean pullTags, int timeout) { 17 | this.branch = branch 18 | this.host = host 19 | this.protocol = protocol 20 | this.repository = repository 21 | this.refspec = refspec 22 | this.credentials = credentials 23 | this.name = name 24 | this.depth = depth 25 | this.honorRefspec = honorRefspec 26 | this.pullTags = pullTags 27 | this.timeout = timeout 28 | 29 | } 30 | 31 | static GitHubScm gitHubScm(String host, String repository, String branch, String protocol) { 32 | gitHubScm(host, repository, branch, protocol, null, null, null) 33 | } 34 | 35 | static GitHubScm gitHubScm(String host, String repository, String branch, String protocol, String refspec, String credentials, String name = null, int depth = 0, boolean honorRefspec = false, boolean pullTags = true) { 36 | new GitHubScm(host, repository, branch, protocol, refspec, credentials, name, depth, honorRefspec, pullTags, 0) 37 | } 38 | 39 | static GitHubScm gitHubScm(String host, String repository, String branch, String protocol, String refspec, String credentials, int timeout, String name = null, int depth = 0, boolean honorRefspec = false, boolean pullTags = true) { 40 | new GitHubScm(host, repository, branch, protocol, refspec, credentials, name, depth, honorRefspec, pullTags, timeout) 41 | } 42 | 43 | @Override 44 | Closure toDsl() { 45 | return { 46 | git { 47 | remote { 48 | github(repository, protocol, host) 49 | if (this.refspec != null) { 50 | refspec(this.refspec) 51 | } 52 | if (this.credentials != null) { 53 | credentials(this.credentials) 54 | } 55 | if (this.name != null) { 56 | name(this.name) 57 | } 58 | extensions { 59 | cloneOptions { 60 | honorRefspec(this.honorRefspec) 61 | noTags(!this.pullTags) 62 | if (this.depth > 0) { 63 | depth(this.depth) 64 | shallow(true) 65 | } 66 | if (this.timeout != 0) { 67 | timeout(this.timeout) 68 | } 69 | } 70 | } 71 | } 72 | branch(this.branch) 73 | } 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/test/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/publisher/PostBuildScriptPublisherSpec.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.publisher 2 | 3 | import javaposse.jobdsl.dsl.Job 4 | import uk.gov.hmrc.jenkinsjobbuilders.domain.AbstractJobSpec 5 | import uk.gov.hmrc.jenkinsjobbuilders.domain.builder.JobBuilder 6 | import uk.gov.hmrc.jenkinsjobbuilders.domain.publisher.Result 7 | 8 | import static uk.gov.hmrc.jenkinsjobbuilders.domain.publisher.PostBuildScriptPublisher.postBuildScriptPublisher 9 | 10 | class PostBuildScriptPublisherSpec extends AbstractJobSpec { 11 | 12 | void 'test XML output with empty build stage result is still valid but it never runs'() { 13 | given: 14 | JobBuilder jobBuilder = new JobBuilder('test-job', 'test-job-description') 15 | .withPublishers(postBuildScriptPublisher( 16 | 'test-script', 17 | [] 18 | )) 19 | 20 | when: 21 | Job job = jobBuilder.build(JOB_PARENT) 22 | 23 | then: 24 | with(job.node) { 25 | def postBuildScript = publishers.'org.jenkinsci.plugins.postbuildscript.PostBuildScript' 26 | postBuildScript.config.markBuildUnstable.text() == 'false' 27 | def postBuildStep = postBuildScript.config.buildSteps.'org.jenkinsci.plugins.postbuildscript.model.PostBuildStep' 28 | postBuildStep.buildSteps.'hudson.tasks.Shell'.command.text() == 'test-script' 29 | postBuildStep.results.text() == '' 30 | } 31 | } 32 | 33 | void 'test XML output with one build stage result'() { 34 | given: 35 | JobBuilder jobBuilder = new JobBuilder('test-job', 'test-job-description') 36 | .withPublishers(postBuildScriptPublisher( 37 | 'test-script', 38 | [Result.SUCCESS] 39 | )) 40 | 41 | when: 42 | Job job = jobBuilder.build(JOB_PARENT) 43 | 44 | then: 45 | with(job.node) { 46 | def postBuildScript = publishers.'org.jenkinsci.plugins.postbuildscript.PostBuildScript' 47 | postBuildScript.config.markBuildUnstable.text() == 'false' 48 | def postBuildStep = postBuildScript.config.buildSteps.'org.jenkinsci.plugins.postbuildscript.model.PostBuildStep' 49 | postBuildStep.buildSteps.'hudson.tasks.Shell'.command.text() == 'test-script' 50 | postBuildStep.results.string[0].text() == 'SUCCESS' 51 | } 52 | } 53 | 54 | void 'test XML output with two build stage results'() { 55 | given: 56 | JobBuilder jobBuilder = new JobBuilder('test-job', 'test-job-description') 57 | .withPublishers(postBuildScriptPublisher( 58 | 'test-script', 59 | [Result.SUCCESS,Result.UNSTABLE] 60 | )) 61 | 62 | when: 63 | Job job = jobBuilder.build(JOB_PARENT) 64 | 65 | then: 66 | with(job.node) { 67 | def postBuildScript = publishers.'org.jenkinsci.plugins.postbuildscript.PostBuildScript' 68 | postBuildScript.config.markBuildUnstable.text() == 'false' 69 | def postBuildStep = postBuildScript.config.buildSteps.'org.jenkinsci.plugins.postbuildscript.model.PostBuildStep' 70 | postBuildStep.buildSteps.'hudson.tasks.Shell'.command.text() == 'test-script' 71 | postBuildStep.results.string[0].text() == 'SUCCESS' 72 | postBuildStep.results.string[1].text() == 'UNSTABLE' 73 | } 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/step/RemoteTriggerStep2.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.step 2 | 3 | /* 4 | * Copyright 2018 HM Revenue & Customs 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | /* this class is for configuring a parameterized remote trigger step with plugin version 3.1.6.2+ */ 20 | 21 | import uk.gov.hmrc.jenkinsjobbuilders.domain.configure.Configure 22 | 23 | class RemoteTriggerStep2 implements Configure { 24 | private final String remoteHost 25 | private final String remoteJob 26 | private final Map parameterMap 27 | private final boolean failIfRemoteBuildFails 28 | private String credentialName = null 29 | private String token = null 30 | 31 | private RemoteTriggerStep2(String remoteHost, String remoteJob, Map parameters, boolean failIfRemoteBuildFails) { 32 | this.remoteHost = remoteHost 33 | this.remoteJob = remoteJob 34 | this.parameterMap = parameters 35 | this.failIfRemoteBuildFails = failIfRemoteBuildFails 36 | } 37 | 38 | static Configure remoteTriggerStep(String remoteHost, String remoteJob, Map parameters, boolean failIfRemoteBuildFails = true) { 39 | new RemoteTriggerStep2(remoteHost, remoteJob, parameters, failIfRemoteBuildFails) 40 | } 41 | 42 | RemoteTriggerStep2 withCredential(String credentialName) { 43 | this.credentialName = credentialName 44 | this 45 | } 46 | 47 | RemoteTriggerStep2 withRemoteJobToken(String token) { 48 | this.token = token 49 | this 50 | } 51 | 52 | @Override 53 | Closure toDsl() { 54 | return { 55 | it / builders / 'org.jenkinsci.plugins.ParameterizedRemoteTrigger.RemoteBuildConfiguration' { 56 | 'remoteJenkinsName'(this.remoteHost) 57 | 'job'(this.remoteJob) 58 | 'pollInterval'(10) 59 | 'blockBuildUntilComplete'(true) 60 | 'shouldNotFailBuild'(!this.failIfRemoteBuildFails) 61 | if (this.credentialName != null) { 62 | 'auth2'(class: 'org.jenkinsci.plugins.ParameterizedRemoteTrigger.auth2.CredentialsAuth') { 63 | credentials(this.credentialName) 64 | } 65 | } 66 | if (this.token != null) { 67 | 'token'(this.token) 68 | } 69 | 'parameters2'(class: 'org.jenkinsci.plugins.ParameterizedRemoteTrigger.parameters2.MapParameters') { 70 | parameters { 71 | this.parameterMap.each { n, v -> 72 | 'org.jenkinsci.plugins.ParameterizedRemoteTrigger.parameters2.MapParameter' { 73 | 'name'(n) 74 | 'value'(v) 75 | } 76 | } 77 | } 78 | } 79 | } 80 | } 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/test/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/configure/PyEnvBuildWrapperSpec.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.configure 2 | 3 | import javaposse.jobdsl.dsl.Job 4 | import uk.gov.hmrc.jenkinsjobbuilders.domain.AbstractJobSpec 5 | import uk.gov.hmrc.jenkinsjobbuilders.domain.builder.JobBuilder 6 | import static uk.gov.hmrc.jenkinsjobbuilders.domain.configure.PyEnvBuildWrapper.pyEnvBuildWrapper 7 | 8 | class PyEnvBuildWrapperSpec extends AbstractJobSpec { 9 | void 'test default XML output'() { 10 | given: 11 | JobBuilder jobBuilder = new JobBuilder('test-job', 'test-job-description'). 12 | withConfigures(pyEnvBuildWrapper()) 13 | 14 | when: 15 | Job job = jobBuilder.build(JOB_PARENT) 16 | 17 | then: 18 | with(job.node) { 19 | def rubyObject = buildWrappers.'ruby-proxy-object'.'ruby-object' 20 | rubyObject.'@ruby-class'.text() == 'Jenkins::Tasks::BuildWrapperProxy' 21 | rubyObject.'@pluginid'.text() == 'pyenv' 22 | 23 | rubyObject.pluginid.'@pluginid'.text() == 'pyenv' 24 | rubyObject.pluginid.'@ruby-class'.text() == 'String' 25 | rubyObject.pluginid.text() == 'pyenv' 26 | 27 | rubyObject.object.'@ruby-class'.text() == 'PyenvWrapper' 28 | rubyObject.object.'@pluginid'.text() == 'pyenv' 29 | 30 | rubyObject.object.'pyenv__root'.'@pluginid'.text() == 'pyenv' 31 | rubyObject.object.'pyenv__root'.'@ruby-class'.text() == 'String' 32 | rubyObject.object.'pyenv__root'.text() == '$HOME/.pyenv' 33 | 34 | rubyObject.object.'pip__list'.'@pluginid'.text() == 'pyenv' 35 | rubyObject.object.'pip__list'.'@ruby-class'.text() == 'String' 36 | rubyObject.object.'pip__list'.text() == '' 37 | 38 | rubyObject.object.'pyenv__revision'.'@pluginid'.text() == 'pyenv' 39 | rubyObject.object.'pyenv__revision'.'@ruby-class'.text() == 'String' 40 | rubyObject.object.'pyenv__revision'.text() == 'master' 41 | 42 | rubyObject.object.'version'.'@pluginid'.text() == 'pyenv' 43 | rubyObject.object.'version'.'@ruby-class'.text() == 'String' 44 | rubyObject.object.'version'.text() == 'null' 45 | 46 | rubyObject.object.'ignore__local__version'.'@pluginid'.text() == 'pyenv' 47 | rubyObject.object.'ignore__local__version'.'@ruby-class'.text() == 'FalseClass' 48 | 49 | rubyObject.object.'pyenv__repository'.'@pluginid'.text() == 'pyenv' 50 | rubyObject.object.'pyenv__repository'.'@ruby-class'.text() == 'String' 51 | rubyObject.object.'pyenv__repository'.text() == 'https://github.com/yyuu/pyenv.git' 52 | } 53 | } 54 | 55 | void 'test overridden XML output'() { 56 | given: 57 | final String defaultPythonVersion = "2.7.14" 58 | final List pipSpecification = ['tox', 'retry==0.9.2'] 59 | JobBuilder jobBuilder = new JobBuilder('test-job', 'test-job-description'). 60 | withConfigures(pyEnvBuildWrapper(defaultPythonVersion, 61 | true, 62 | pipSpecification)) 63 | 64 | when: 65 | Job job = jobBuilder.build(JOB_PARENT) 66 | 67 | then: 68 | with(job.node) { 69 | def rubyObject = buildWrappers.'ruby-proxy-object'.'ruby-object' 70 | rubyObject.object.'pip__list'.text() == 'retry==0.9.2,tox' 71 | 72 | rubyObject.object.'version'.text() == defaultPythonVersion 73 | 74 | rubyObject.object.'ignore__local__version'.'@ruby-class'.text() == 'TrueClass' 75 | } 76 | } 77 | 78 | } 79 | -------------------------------------------------------------------------------- /src/test/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/configure/ProjectBasedSecurityPropertySpec.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 HM Revenue & Customs 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.gov.hmrc.jenkinsjobbuilders.domain.configure 18 | 19 | import javaposse.jobdsl.dsl.Job 20 | import uk.gov.hmrc.jenkinsjobbuilders.domain.AbstractJobSpec 21 | import uk.gov.hmrc.jenkinsjobbuilders.domain.authorisation.Permission 22 | import uk.gov.hmrc.jenkinsjobbuilders.domain.builder.JobBuilder 23 | 24 | import static uk.gov.hmrc.jenkinsjobbuilders.domain.configure.InheritanceStrategy.NON_INHERITING_STRATEGY 25 | import static uk.gov.hmrc.jenkinsjobbuilders.domain.configure.ProjectBasedSecurityProperty.enableProjectBasedSecurity 26 | 27 | class ProjectBasedSecurityPropertySpec extends AbstractJobSpec { 28 | 29 | void 'test default XML output'() { 30 | final Set desiredPermissions = [Permission.permissionSetting("viewer", "hudson.model.Item.Read"), 31 | Permission.permissionSetting("admin", "hudson.model.Item.Build"), 32 | Permission.permissionSetting("admin", "hudson.model.Item.Cancel"), 33 | Permission.permissionSetting("admin", "hudson.model.Item.Configure"), 34 | Permission.permissionSetting("admin", "hudson.model.Item.Delete"), 35 | Permission.permissionSetting("admin", "hudson.model.Item.Discover"), 36 | Permission.permissionSetting("admin", "hudson.model.Item.Read"), 37 | Permission.permissionSetting("admin", "hudson.model.Item.Workspace"), 38 | Permission.permissionSetting("admin", "hudson.model.Run.Delete"), 39 | Permission.permissionSetting("admin", "hudson.model.Run.Update"), 40 | Permission.permissionSetting("admin", "hudson.scm.SCM.Tag")] 41 | given: 42 | JobBuilder jobBuilder = new JobBuilder('test-job', 'test-job-description'). 43 | withConfigures(enableProjectBasedSecurity(NON_INHERITING_STRATEGY, 44 | desiredPermissions)) 45 | 46 | when: 47 | Job job = jobBuilder.build(JOB_PARENT) 48 | 49 | then: 50 | with(job.node) { 51 | def authorizationMatrixProperty = properties.'hudson.security.AuthorizationMatrixProperty' 52 | authorizationMatrixProperty.inheritanceStrategy.'@class'.text() == 'org.jenkinsci.plugins.matrixauth.inheritance.NonInheritingStrategy' 53 | desiredPermissions.size() == authorizationMatrixProperty.permission.size() 54 | desiredPermissions.each { expectedPermission -> 55 | assert authorizationMatrixProperty.permission.find() { actualPermission -> 56 | "${expectedPermission.permission}:${expectedPermission.ldapIdentifier}" == actualPermission.text() 57 | } 58 | } 59 | } 60 | } 61 | } 62 | 63 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/builder/FolderBuilder.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 HM Revenue & Customs 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.gov.hmrc.jenkinsjobbuilders.domain.builder 18 | 19 | import javaposse.jobdsl.dsl.DslFactory 20 | import javaposse.jobdsl.dsl.DslScriptException 21 | import javaposse.jobdsl.dsl.Folder 22 | import uk.gov.hmrc.jenkinsjobbuilders.domain.authorisation.Permission 23 | import uk.gov.hmrc.jenkinsjobbuilders.domain.authorisation.GroupPermission 24 | import uk.gov.hmrc.jenkinsjobbuilders.domain.configure.Configure 25 | 26 | final class FolderBuilder implements Builder { 27 | 28 | private final String name 29 | private String displayName 30 | 31 | private String description 32 | private String primaryView 33 | private final Set permissions = [] 34 | private final Set groupPermissions = [] 35 | private final List configures = [] 36 | 37 | /** 38 | * 39 | * @param name This doesn't get used in the Jenkins GUI but instead acts as a unique key. 40 | * Unless there are multiple views with the same displayName this and displayName can share the same value. 41 | */ 42 | FolderBuilder(String name) { 43 | this.name = name 44 | this.displayName = name 45 | this.description = "Folder for ${name} jobs." 46 | } 47 | 48 | FolderBuilder withDisplayName(final String displayName) { 49 | this.displayName = displayName 50 | return this 51 | } 52 | 53 | FolderBuilder withDescription(final String description) { 54 | this.description = description 55 | return this 56 | } 57 | 58 | FolderBuilder withPrimaryView(final String primaryView) { 59 | this.primaryView = primaryView 60 | return this 61 | } 62 | 63 | FolderBuilder withPermissions(final Set perms) { 64 | this.permissions.addAll(perms) 65 | return this 66 | } 67 | 68 | FolderBuilder withGroupPermissions(final Set perms) { 69 | this.groupPermissions.addAll(perms) 70 | return this 71 | } 72 | 73 | FolderBuilder withConfigures(final List configures) { 74 | this.configures.addAll(configures) 75 | return this 76 | } 77 | 78 | @Override 79 | Folder build(DslFactory dslFactory) { 80 | dslFactory.folder(this.name) { 81 | authorization { 82 | this.permissions.each { Permission perm -> 83 | try { 84 | permission(perm.permission, perm.ldapIdentifier) 85 | } 86 | catch (DslScriptException e) { 87 | throw new DslScriptException("Could not assign permission: ${perm.permission} to LDAP identifier: ${perm.ldapIdentifier}", e) 88 | } 89 | } 90 | this.groupPermissions.each { GroupPermission groupPerm -> 91 | try { 92 | groupPermission(groupPerm.permission, groupPerm.ldapIdentifier) 93 | } 94 | catch (DslScriptException e) { 95 | throw new DslScriptException("Could not assign group permission: ${groupPerm.permission} to LDAP identifier: ${groupPerm.ldapIdentifier}", e) 96 | } 97 | } 98 | } 99 | this.configures.each { 100 | configure(it.toDsl()) 101 | } 102 | description(this.description) 103 | displayName(this.displayName) 104 | if(this.primaryView) { 105 | primaryView(this.primaryView) 106 | } 107 | } 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /src/test/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/builder/FolderBuilderSpec.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.builder 2 | 3 | import javaposse.jobdsl.dsl.DslScriptException 4 | import javaposse.jobdsl.dsl.Folder 5 | import uk.gov.hmrc.jenkinsjobbuilders.domain.AbstractJobSpec 6 | import uk.gov.hmrc.jenkinsjobbuilders.domain.authorisation.Permission 7 | 8 | import static uk.gov.hmrc.jenkinsjobbuilders.domain.authorisation.Permission.permissionSetting 9 | import static uk.gov.hmrc.jenkinsjobbuilders.domain.configure.FolderInheritanceStrategy.folderInheritanceStrategy 10 | import static uk.gov.hmrc.jenkinsjobbuilders.domain.configure.InheritanceStrategy.NON_INHERITING_STRATEGY 11 | 12 | class FolderBuilderSpec extends AbstractJobSpec { 13 | 14 | def 'it should create a folder using appropriate defaults for optional values'() { 15 | given: 16 | final String folderName = "my-first-folder" 17 | final FolderBuilder folderBuilder = new FolderBuilder(folderName) 18 | 19 | when: 20 | Folder folder = folderBuilder.build(JOB_PARENT) 21 | 22 | then: 23 | with(folder.node) { 24 | folderName == it.displayName.text() 25 | "Folder for ${folderName} jobs." == it.description.text() 26 | 'all' == it.folderViews.primaryView.text() 27 | ! it.'properties'.'com.cloudbees.hudson.plugins.folder.properties.AuthorizationMatrixProperty'.inheritanceStrategy 28 | } 29 | } 30 | 31 | def 'it correctly configures a fully populated example'() { 32 | given: 33 | final String folderName = "my-first-folder" 34 | final String folderDescription = "my-first-folder-description" 35 | final String displayName = "my-first-folder-display-name" 36 | final String primaryView = "this-is-my-primary-view" 37 | final Set permissions = [ 38 | permissionSetting("ci-admin", "hudson.model.Item.Read"), 39 | permissionSetting("ci-admin", "hudson.model.Item.Build"), 40 | permissionSetting("ci-admin", "hudson.model.Item.Workspace"), 41 | permissionSetting("ci-admin", "hudson.model.Item.Delete"), 42 | permissionSetting("ci-admin", "hudson.model.Item.Configure"), 43 | permissionSetting("ci-admin", "hudson.model.Item.Discover"), 44 | permissionSetting("ci-admin", "hudson.model.Item.Cancel"), 45 | ] 46 | 47 | final FolderBuilder folderBuilder = new FolderBuilder(folderName) 48 | .withDescription(folderDescription) 49 | .withDisplayName(displayName) 50 | .withPrimaryView(primaryView) 51 | .withPermissions(permissions) 52 | .withConfigures([folderInheritanceStrategy(NON_INHERITING_STRATEGY)]) 53 | 54 | when: 55 | Folder folder = folderBuilder.build(JOB_PARENT) 56 | 57 | then: 58 | with(folder.node) { 59 | it.displayName.text() == displayName 60 | it.description.text() == folderDescription 61 | it.folderViews.primaryView.text() == primaryView 62 | final NodeList configuredPermissions = it.'properties'.'com.cloudbees.hudson.plugins.folder.properties.AuthorizationMatrixProperty'[0].value() 63 | configuredPermissions.size() == 8 64 | 65 | final def inheritanceStrategy = configuredPermissions.first() 66 | 'inheritanceStrategy' == inheritanceStrategy.name() 67 | "org.jenkinsci.plugins.matrixauth.inheritance.NonInheritingStrategy" == inheritanceStrategy["@class"] 68 | 69 | permissions.each { permission -> 70 | configuredPermissions.tail().find { configuredPermission -> 71 | 'permission' == configuredPermission.name() && 72 | "${permission.permission}:${permission.ldapIdentifier}" == configuredPermission.text() 73 | } 74 | } 75 | } 76 | } 77 | 78 | def 'it should throw an exception when it cannot map a permission, identifiying the permission that cannot be used'() { 79 | given: 80 | final String folderName = "my-first-folder" 81 | final Set permissions = [permissionSetting("ci-admin", "hudson.model.Item.MadeUpPermission")] 82 | final FolderBuilder folderBuilder = new FolderBuilder(folderName).withPermissions(permissions) 83 | 84 | when: 85 | folderBuilder.build(JOB_PARENT) 86 | 87 | then: 88 | DslScriptException e = thrown() 89 | e.message.contains("Could not assign permission: hudson.model.Item.MadeUpPermission to LDAP identifier: ci-admin") 90 | e.cause instanceof DslScriptException 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /src/test/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/builder/FolderBuilderGroupPermissionSpec.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.builder 2 | 3 | import javaposse.jobdsl.dsl.DslScriptException 4 | import javaposse.jobdsl.dsl.Folder 5 | import uk.gov.hmrc.jenkinsjobbuilders.domain.AbstractJobSpec 6 | import uk.gov.hmrc.jenkinsjobbuilders.domain.authorisation.GroupPermission 7 | 8 | import static uk.gov.hmrc.jenkinsjobbuilders.domain.authorisation.GroupPermission.permissionSetting 9 | import static uk.gov.hmrc.jenkinsjobbuilders.domain.configure.FolderInheritanceStrategy.folderInheritanceStrategy 10 | import static uk.gov.hmrc.jenkinsjobbuilders.domain.configure.InheritanceStrategy.NON_INHERITING_STRATEGY 11 | 12 | class FolderBuilderGroupPermissionSpec extends AbstractJobSpec { 13 | 14 | def 'it should create a folder using appropriate defaults for optional values'() { 15 | given: 16 | final String folderName = "my-first-folder" 17 | final FolderBuilder folderBuilder = new FolderBuilder(folderName) 18 | 19 | when: 20 | Folder folder = folderBuilder.build(JOB_PARENT) 21 | 22 | then: 23 | with(folder.node) { 24 | folderName == it.displayName.text() 25 | "Folder for ${folderName} jobs." == it.description.text() 26 | 'all' == it.folderViews.primaryView.text() 27 | ! it.'properties'.'com.cloudbees.hudson.plugins.folder.properties.AuthorizationMatrixProperty'.inheritanceStrategy 28 | } 29 | } 30 | 31 | def 'it correctly configures a fully populated example'() { 32 | given: 33 | final String folderName = "my-first-folder" 34 | final String folderDescription = "my-first-folder-description" 35 | final String displayName = "my-first-folder-display-name" 36 | final String primaryView = "this-is-my-primary-view" 37 | final Set permissions = [ 38 | permissionSetting("ci-admin", "hudson.model.Item.Read"), 39 | permissionSetting("ci-admin", "hudson.model.Item.Build"), 40 | permissionSetting("ci-admin", "hudson.model.Item.Workspace"), 41 | permissionSetting("ci-admin", "hudson.model.Item.Delete"), 42 | permissionSetting("ci-admin", "hudson.model.Item.Configure"), 43 | permissionSetting("ci-admin", "hudson.model.Item.Discover"), 44 | permissionSetting("ci-admin", "hudson.model.Item.Cancel"), 45 | ] 46 | 47 | final FolderBuilder folderBuilder = new FolderBuilder(folderName) 48 | .withDescription(folderDescription) 49 | .withDisplayName(displayName) 50 | .withPrimaryView(primaryView) 51 | .withGroupPermissions(permissions) 52 | .withConfigures([folderInheritanceStrategy(NON_INHERITING_STRATEGY)]) 53 | 54 | when: 55 | Folder folder = folderBuilder.build(JOB_PARENT) 56 | 57 | then: 58 | with(folder.node) { 59 | it.displayName.text() == displayName 60 | it.description.text() == folderDescription 61 | it.folderViews.primaryView.text() == primaryView 62 | final NodeList configuredPermissions = it.'properties'.'com.cloudbees.hudson.plugins.folder.properties.AuthorizationMatrixProperty'[0].value() 63 | configuredPermissions.size() == 8 64 | 65 | final def inheritanceStrategy = configuredPermissions.first() 66 | 'inheritanceStrategy' == inheritanceStrategy.name() 67 | "org.jenkinsci.plugins.matrixauth.inheritance.NonInheritingStrategy" == inheritanceStrategy["@class"] 68 | 69 | permissions.each { permission -> 70 | configuredPermissions.tail().find { configuredPermission -> 71 | 'permission' == configuredPermission.name() && 72 | "GROUP:${permission.permission}:${permission.ldapIdentifier}" == configuredPermission.text() 73 | } 74 | } 75 | } 76 | } 77 | 78 | def 'it should throw an exception when it cannot map a permission, identifiying the permission that cannot be used'() { 79 | given: 80 | final String folderName = "my-first-folder" 81 | final Set permissions = [permissionSetting("ci-admin", "hudson.model.Item.MadeUpPermission")] 82 | final FolderBuilder folderBuilder = new FolderBuilder(folderName).withGroupPermissions(permissions) 83 | 84 | when: 85 | folderBuilder.build(JOB_PARENT) 86 | 87 | then: 88 | DslScriptException e = thrown() 89 | e.message.contains("Could not assign group permission: hudson.model.Item.MadeUpPermission to LDAP identifier: ci-admin") 90 | e.cause instanceof DslScriptException 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /src/test/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/scm/GitHubComScmSpec.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.scm 2 | 3 | import javaposse.jobdsl.dsl.Job 4 | import uk.gov.hmrc.jenkinsjobbuilders.domain.AbstractJobSpec 5 | import uk.gov.hmrc.jenkinsjobbuilders.domain.builder.JobBuilder 6 | import static uk.gov.hmrc.jenkinsjobbuilders.domain.scm.GitHubScm.gitHubScm 7 | 8 | class GitHubComScmSpec extends AbstractJobSpec { 9 | private JobBuilder jobBuilder 10 | 11 | def setup() { 12 | jobBuilder = new JobBuilder('test-job', 'test-job-description') 13 | } 14 | 15 | 16 | def "default configuration"() { 17 | given: 18 | jobBuilder.withScm(gitHubScm("host", "repository", "branch", "ssh", "refspec", "credentials", "name")) 19 | 20 | when: 21 | Job job = jobBuilder.build(JOB_PARENT) 22 | 23 | then: 24 | with(job.node) { 25 | scm.extensions.'hudson.plugins.git.extensions.impl.CloneOption'.depth.text() == "0" 26 | scm.extensions.'hudson.plugins.git.extensions.impl.CloneOption'.shallow.text() == "false" 27 | scm.extensions.'hudson.plugins.git.extensions.impl.CloneOption'.noTags.text() == "false" 28 | scm.extensions.'hudson.plugins.git.extensions.impl.CloneOption'.honorRefspec.text() == "false" 29 | } 30 | } 31 | 32 | def "depth and honorRefspec configured"() { 33 | given: 34 | jobBuilder.withScm(gitHubScm("host", "repository", "branch", "ssh", "refspec", "credentials", "name", 3, true)) 35 | 36 | when: 37 | Job job = jobBuilder.build(JOB_PARENT) 38 | 39 | then: 40 | with(job.node) { 41 | scm.extensions.'hudson.plugins.git.extensions.impl.CloneOption'.depth.text() == "3" 42 | scm.extensions.'hudson.plugins.git.extensions.impl.CloneOption'.shallow 43 | scm.extensions.'hudson.plugins.git.extensions.impl.CloneOption'.noTags.text() == "false" 44 | scm.extensions.'hudson.plugins.git.extensions.impl.CloneOption'.honorRefspec 45 | } 46 | } 47 | 48 | def "honorRefspec configured"() { 49 | given: 50 | jobBuilder.withScm(gitHubScm("host", "repository", "branch", "ssh", "refspec", "credentials", "name", 0, true)) 51 | 52 | when: 53 | Job job = jobBuilder.build(JOB_PARENT) 54 | 55 | then: 56 | with(job.node) { 57 | scm.extensions.'hudson.plugins.git.extensions.impl.CloneOption'.depth.text() == "0" 58 | scm.extensions.'hudson.plugins.git.extensions.impl.CloneOption'.shallow.text() == "false" 59 | scm.extensions.'hudson.plugins.git.extensions.impl.CloneOption'.noTags.text() == "false" 60 | scm.extensions.'hudson.plugins.git.extensions.impl.CloneOption'.honorRefspec 61 | } 62 | } 63 | 64 | def "depth configured"() { 65 | given: 66 | jobBuilder.withScm(gitHubScm("host", "repository", "branch", "ssh", "refspec", "credentials", "name", 9)) 67 | 68 | when: 69 | Job job = jobBuilder.build(JOB_PARENT) 70 | 71 | then: 72 | with(job.node) { 73 | scm.extensions.'hudson.plugins.git.extensions.impl.CloneOption'.depth.text() == "9" 74 | scm.extensions.'hudson.plugins.git.extensions.impl.CloneOption'.shallow 75 | scm.extensions.'hudson.plugins.git.extensions.impl.CloneOption'.noTags.text() == "false" 76 | scm.extensions.'hudson.plugins.git.extensions.impl.CloneOption'.honorRefspec.text() == "false" 77 | } 78 | } 79 | 80 | def "pullTags configured"() { 81 | given: 82 | jobBuilder.withScm(gitHubScm("host", "repository", "branch", "ssh", "refspec", "credentials", "name", 0, false, false)) 83 | 84 | when: 85 | Job job = jobBuilder.build(JOB_PARENT) 86 | 87 | then: 88 | with(job.node) { 89 | scm.extensions.'hudson.plugins.git.extensions.impl.CloneOption'.depth.text() == "0" 90 | scm.extensions.'hudson.plugins.git.extensions.impl.CloneOption'.shallow.text() == "false" 91 | scm.extensions.'hudson.plugins.git.extensions.impl.CloneOption'.noTags 92 | scm.extensions.'hudson.plugins.git.extensions.impl.CloneOption'.honorRefspec.text() == "false" 93 | } 94 | } 95 | 96 | def "Timeout configured"() { 97 | given: 98 | jobBuilder.withScm(gitHubScm("host", "repository", "branch", "ssh", "refspec", "credentials", 20, "name", 0, false, false)) 99 | 100 | when: 101 | Job job = jobBuilder.build(JOB_PARENT) 102 | 103 | then: 104 | with(job.node) { 105 | scm.extensions.'hudson.plugins.git.extensions.impl.CloneOption'.depth.text() == "0" 106 | scm.extensions.'hudson.plugins.git.extensions.impl.CloneOption'.shallow.text() == "false" 107 | scm.extensions.'hudson.plugins.git.extensions.impl.CloneOption'.noTags 108 | scm.extensions.'hudson.plugins.git.extensions.impl.CloneOption'.honorRefspec.text() == "false" 109 | scm.extensions.'hudson.plugins.git.extensions.impl.CloneOption'.timeout.text() == "20" 110 | } 111 | } 112 | } -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/step/AwsCodeBuildStep.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.step 2 | 3 | import groovy.transform.builder.Builder 4 | import groovy.transform.builder.ExternalStrategy 5 | import uk.gov.hmrc.jenkinsjobbuilders.domain.configure.Configure 6 | 7 | @Deprecated 8 | final class AwsCodeBuildStep implements Configure { 9 | 10 | String credentialsType 11 | String credentialsId 12 | String projectName 13 | String proxyHost 14 | String proxyPort 15 | String awsAccessKey 16 | String awsSecretKey 17 | String region 18 | String sourceControlType 19 | String localSourcePath 20 | String workspaceSubdir 21 | String sourceVersion 22 | String sseAlgorithm 23 | String gitCloneDepthOverride 24 | String reportBuildStatusOverride 25 | String secondarySourcesOverride 26 | String secondarySourcesVersionOverride 27 | String artifactTypeOverride 28 | String artifactLocationOverride 29 | String artifactNameOverride 30 | String artifactNamespaceOverride 31 | String artifactPackagingOverride 32 | String artifactPathOverride 33 | String artifactEncryptionDisabledOverride 34 | String overrideArtifactName 35 | String secondaryArtifactsOverride 36 | String environmentTypeOverride 37 | String imageOverride 38 | String computeTypeOverride 39 | String certificateOverride 40 | String cacheTypeOverride 41 | String cacheLocationOverride 42 | String cloudWatchLogsStatusOverride 43 | String cloudWatchLogsGroupNameOverride 44 | String cloudWatchLogsStreamNameOverride 45 | String s3LogsStatusOverride 46 | String s3LogsLocationOverride 47 | String serviceRoleOverride 48 | String privilegedModeOverride 49 | String sourceTypeOverride 50 | String sourceLocationOverride 51 | String insecureSslOverride 52 | String envVariables 53 | String envParameters 54 | String buildSpecFile 55 | String buildTimeoutOverride 56 | String cwlStreamingDisabled 57 | 58 | @Override 59 | Closure toDsl() { 60 | return { 61 | it / 'builders' / 'CodeBuilder' { 62 | 'credentialsType'(credentialsType ?: 'jenkins') 63 | 'credentialsId'(credentialsId ?: '') 64 | 'projectName'(projectName ?: '') 65 | 'proxyHost'(proxyHost ?: '') 66 | 'proxyPort'(proxyPort ?: '') 67 | 'region'(region ?: 'eu-west-2') 68 | 'awsAccessKey'(awsAccessKey ?: '') 69 | 'awsSecretKey'(awsSecretKey ?: '') 70 | 'sourceControlType'(sourceControlType ?: '') 71 | 'localSourcePath'(localSourcePath ?: '') 72 | 'workspaceSubdir'(workspaceSubdir ?: '') 73 | 'sourceVersion'(sourceVersion ?: '') 74 | 'sseAlgorithm'(sseAlgorithm ?: '') 75 | 'gitCloneDepthOverride'(gitCloneDepthOverride ?: '') 76 | 'reportBuildStatusOverride'(reportBuildStatusOverride ?: '') 77 | 'secondarySourcesOverride'(secondarySourcesOverride ?: '') 78 | 'secondarySourcesVersionOverride'(secondarySourcesVersionOverride ?: '') 79 | 'artifactTypeOverride'(artifactTypeOverride ?: '') 80 | 'artifactLocationOverride'(artifactLocationOverride ?: '') 81 | 'artifactNameOverride'(artifactNameOverride ?: '') 82 | 'artifactNamespaceOverride'(artifactNamespaceOverride ?: '') 83 | 'artifactPackagingOverride'(artifactPackagingOverride ?: '') 84 | 'artifactPathOverride'(artifactPathOverride ?: '') 85 | 'artifactEncryptionDisabledOverride'(artifactEncryptionDisabledOverride ?: '') 86 | 'overrideArtifactName'(overrideArtifactName ?: '') 87 | 'secondaryArtifactsOverride'(secondaryArtifactsOverride ?: '') 88 | 'environmentTypeOverride'(environmentTypeOverride ?: '') 89 | 'imageOverride'(imageOverride ?: '') 90 | 'computeTypeOverride'(computeTypeOverride ?: '') 91 | 'certificateOverride'(certificateOverride ?: '') 92 | 'cacheTypeOverride'(cacheTypeOverride ?: '') 93 | 'cacheLocationOverride'(cacheLocationOverride ?: '') 94 | 'cloudWatchLogsStatusOverride'(cloudWatchLogsStatusOverride ?: '') 95 | 'cloudWatchLogsGroupNameOverride'(cloudWatchLogsGroupNameOverride ?: '') 96 | 'cloudWatchLogsStreamNameOverride'(cloudWatchLogsStreamNameOverride ?: '') 97 | 's3LogsStatusOverride'(s3LogsStatusOverride ?: '') 98 | 's3LogsLocationOverride'(s3LogsLocationOverride ?: '') 99 | 'serviceRoleOverride'(serviceRoleOverride ?: '') 100 | 'privilegedModeOverride'(privilegedModeOverride ?: '') 101 | 'sourceTypeOverride'(sourceTypeOverride ?: '') 102 | 'sourceLocationOverride'(sourceLocationOverride ?: '') 103 | 'insecureSslOverride'(insecureSslOverride ?: '') 104 | 'envVariables'(envVariables ?: '') 105 | 'envParameters'(envParameters ?: '') 106 | 'buildSpecFile'(buildSpecFile ?: '') 107 | 'buildTimeoutOverride'(buildTimeoutOverride ?: '') 108 | 'cwlStreamingDisabled'(cwlStreamingDisabled ?: '') 109 | } 110 | } 111 | } 112 | } 113 | 114 | @Builder(forClass = AwsCodeBuildStep, 115 | builderStrategy = ExternalStrategy, 116 | prefix = "with") 117 | class AwsCodeBuildStepBuilder { } 118 | -------------------------------------------------------------------------------- /src/test/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/publisher/HtmlReportsPublisherSpec.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.publisher 2 | 3 | import javaposse.jobdsl.dsl.Job 4 | import uk.gov.hmrc.jenkinsjobbuilders.domain.AbstractJobSpec 5 | import uk.gov.hmrc.jenkinsjobbuilders.domain.builder.JobBuilder 6 | 7 | import static uk.gov.hmrc.jenkinsjobbuilders.domain.publisher.HtmlReportsPublisher.htmlReportsPublisher 8 | 9 | class HtmlReportsPublisherSpec extends AbstractJobSpec { 10 | 11 | void 'With keepAll'() { 12 | given: 13 | JobBuilder jobBuilder = new JobBuilder('test-job', 'test-job-description'). 14 | withPublishers(htmlReportsPublisher(['target/fun-browser-test-reports/html-report': 'ScalaTest (fun-browser) Results'], true)) 15 | 16 | when: 17 | Job job = jobBuilder.build(JOB_PARENT) 18 | 19 | then: 20 | with(job.node) { 21 | 22 | publishers.'htmlpublisher.HtmlPublisher'.reportTargets.'htmlpublisher.HtmlPublisherTarget'.reportName.text() == 'ScalaTest (fun-browser) Results' 23 | publishers.'htmlpublisher.HtmlPublisher'.reportTargets.'htmlpublisher.HtmlPublisherTarget'.reportDir.text() == 'target/fun-browser-test-reports/html-report' 24 | publishers.'htmlpublisher.HtmlPublisher'.reportTargets.'htmlpublisher.HtmlPublisherTarget'.allowMissing.text() == 'true' 25 | publishers.'htmlpublisher.HtmlPublisher'.reportTargets.'htmlpublisher.HtmlPublisherTarget'.keepAll.text() == 'true' 26 | } 27 | } 28 | 29 | void 'Without keepAll (default)'() { 30 | given: 31 | JobBuilder jobBuilder = new JobBuilder('test-job', 'test-job-description'). 32 | withPublishers(htmlReportsPublisher(['target/fun-browser-test-reports/html-report': 'ScalaTest (fun-browser) Results'])) 33 | 34 | when: 35 | Job job = jobBuilder.build(JOB_PARENT) 36 | 37 | then: 38 | with(job.node) { 39 | 40 | publishers.'htmlpublisher.HtmlPublisher'.reportTargets.'htmlpublisher.HtmlPublisherTarget'.reportName.text() == 'ScalaTest (fun-browser) Results' 41 | publishers.'htmlpublisher.HtmlPublisher'.reportTargets.'htmlpublisher.HtmlPublisherTarget'.reportDir.text() == 'target/fun-browser-test-reports/html-report' 42 | publishers.'htmlpublisher.HtmlPublisher'.reportTargets.'htmlpublisher.HtmlPublisherTarget'.allowMissing.text() == 'true' 43 | publishers.'htmlpublisher.HtmlPublisher'.reportTargets.'htmlpublisher.HtmlPublisherTarget'.keepAll.text() == 'false' 44 | } 45 | } 46 | 47 | void 'Without keepAll'() { 48 | given: 49 | JobBuilder jobBuilder = new JobBuilder('test-job', 'test-job-description'). 50 | withPublishers(htmlReportsPublisher(['target/fun-browser-test-reports/html-report': 'ScalaTest (fun-browser) Results'], false)) 51 | 52 | when: 53 | Job job = jobBuilder.build(JOB_PARENT) 54 | 55 | then: 56 | with(job.node) { 57 | 58 | publishers.'htmlpublisher.HtmlPublisher'.reportTargets.'htmlpublisher.HtmlPublisherTarget'.reportName.text() == 'ScalaTest (fun-browser) Results' 59 | publishers.'htmlpublisher.HtmlPublisher'.reportTargets.'htmlpublisher.HtmlPublisherTarget'.reportDir.text() == 'target/fun-browser-test-reports/html-report' 60 | publishers.'htmlpublisher.HtmlPublisher'.reportTargets.'htmlpublisher.HtmlPublisherTarget'.allowMissing.text() == 'true' 61 | publishers.'htmlpublisher.HtmlPublisher'.reportTargets.'htmlpublisher.HtmlPublisherTarget'.keepAll.text() == 'false' 62 | } 63 | } 64 | 65 | void 'With alwaysLinkToLastBuild'() { 66 | given: 67 | JobBuilder jobBuilder = new JobBuilder('test-job', 'test-job-description'). 68 | withPublishers(htmlReportsPublisher(['target/fun-browser-test-reports/html-report': 'ScalaTest (fun-browser) Results'], false, true)) 69 | 70 | when: 71 | Job job = jobBuilder.build(JOB_PARENT) 72 | 73 | then: 74 | with(job.node) { 75 | 76 | publishers.'htmlpublisher.HtmlPublisher'.reportTargets.'htmlpublisher.HtmlPublisherTarget'.reportName.text() == 'ScalaTest (fun-browser) Results' 77 | publishers.'htmlpublisher.HtmlPublisher'.reportTargets.'htmlpublisher.HtmlPublisherTarget'.reportDir.text() == 'target/fun-browser-test-reports/html-report' 78 | publishers.'htmlpublisher.HtmlPublisher'.reportTargets.'htmlpublisher.HtmlPublisherTarget'.allowMissing.text() == 'true' 79 | publishers.'htmlpublisher.HtmlPublisher'.reportTargets.'htmlpublisher.HtmlPublisherTarget'.alwaysLinkToLastBuild.text() == 'true' 80 | } 81 | } 82 | 83 | void 'Without alwaysLinkToLastBuild (default)'() { 84 | given: 85 | JobBuilder jobBuilder = new JobBuilder('test-job', 'test-job-description'). 86 | withPublishers(htmlReportsPublisher(['target/fun-browser-test-reports/html-report': 'ScalaTest (fun-browser) Results'])) 87 | 88 | when: 89 | Job job = jobBuilder.build(JOB_PARENT) 90 | 91 | then: 92 | with(job.node) { 93 | 94 | publishers.'htmlpublisher.HtmlPublisher'.reportTargets.'htmlpublisher.HtmlPublisherTarget'.reportName.text() == 'ScalaTest (fun-browser) Results' 95 | publishers.'htmlpublisher.HtmlPublisher'.reportTargets.'htmlpublisher.HtmlPublisherTarget'.reportDir.text() == 'target/fun-browser-test-reports/html-report' 96 | publishers.'htmlpublisher.HtmlPublisher'.reportTargets.'htmlpublisher.HtmlPublisherTarget'.allowMissing.text() == 'true' 97 | publishers.'htmlpublisher.HtmlPublisher'.reportTargets.'htmlpublisher.HtmlPublisherTarget'.alwaysLinkToLastBuild.text() == 'false' 98 | } 99 | } 100 | 101 | void 'Without alwaysLinkToLastBuild'() { 102 | given: 103 | JobBuilder jobBuilder = new JobBuilder('test-job', 'test-job-description'). 104 | withPublishers(htmlReportsPublisher(['target/fun-browser-test-reports/html-report': 'ScalaTest (fun-browser) Results'], false, false)) 105 | 106 | when: 107 | Job job = jobBuilder.build(JOB_PARENT) 108 | 109 | then: 110 | with(job.node) { 111 | 112 | publishers.'htmlpublisher.HtmlPublisher'.reportTargets.'htmlpublisher.HtmlPublisherTarget'.reportName.text() == 'ScalaTest (fun-browser) Results' 113 | publishers.'htmlpublisher.HtmlPublisher'.reportTargets.'htmlpublisher.HtmlPublisherTarget'.reportDir.text() == 'target/fun-browser-test-reports/html-report' 114 | publishers.'htmlpublisher.HtmlPublisher'.reportTargets.'htmlpublisher.HtmlPublisherTarget'.allowMissing.text() == 'true' 115 | publishers.'htmlpublisher.HtmlPublisher'.reportTargets.'htmlpublisher.HtmlPublisherTarget'.alwaysLinkToLastBuild.text() == 'false' 116 | } 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /src/test/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/configure/ProjectBasedGroupSecurityPropertySpec.groovy: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 HM Revenue & Customs 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.gov.hmrc.jenkinsjobbuilders.domain.configure 18 | 19 | import javaposse.jobdsl.dsl.Job 20 | import uk.gov.hmrc.jenkinsjobbuilders.domain.AbstractJobSpec 21 | import uk.gov.hmrc.jenkinsjobbuilders.domain.authorisation.GroupPermission 22 | import uk.gov.hmrc.jenkinsjobbuilders.domain.authorisation.Permission 23 | import uk.gov.hmrc.jenkinsjobbuilders.domain.builder.JobBuilder 24 | import static uk.gov.hmrc.jenkinsjobbuilders.domain.configure.InheritanceStrategy.NON_INHERITING_STRATEGY 25 | import static uk.gov.hmrc.jenkinsjobbuilders.domain.configure.InheritanceStrategy.INHERIT_PARENT_STRATEGY 26 | import static uk.gov.hmrc.jenkinsjobbuilders.domain.configure.InheritanceStrategy.INHERIT_GLOBAL_STRATEGY 27 | 28 | import static uk.gov.hmrc.jenkinsjobbuilders.domain.configure.ProjectBasedSecurityProperty.enableProjectBasedSecurity as enableProjectNonGroupBasedSecurity 29 | import static uk.gov.hmrc.jenkinsjobbuilders.domain.configure.ProjectBasedGroupSecurityProperty.enableProjectBasedSecurity 30 | 31 | class ProjectBasedGroupSecurityPropertySpec extends AbstractJobSpec { 32 | 33 | void 'test XML output with non inheriting group perms'() { 34 | final Set desiredPermissions = [GroupPermission.permissionSetting("viewer", "hudson.model.Item.Read"), 35 | GroupPermission.permissionSetting("admin", "hudson.model.Item.Build"), 36 | GroupPermission.permissionSetting("admin", "hudson.model.Item.Cancel"), 37 | GroupPermission.permissionSetting("admin", "hudson.model.Item.Configure"), 38 | GroupPermission.permissionSetting("admin", "hudson.model.Item.Delete"), 39 | GroupPermission.permissionSetting("admin", "hudson.model.Item.Discover"), 40 | GroupPermission.permissionSetting("admin", "hudson.model.Item.Read"), 41 | GroupPermission.permissionSetting("admin", "hudson.model.Item.Workspace"), 42 | GroupPermission.permissionSetting("admin", "hudson.model.Run.Delete"), 43 | GroupPermission.permissionSetting("admin", "hudson.model.Run.Update"), 44 | GroupPermission.permissionSetting("admin", "hudson.scm.SCM.Tag")] 45 | given: 46 | JobBuilder jobBuilder = new JobBuilder('test-job', 'test-job-description'). 47 | withConfigures(enableProjectBasedSecurity(NON_INHERITING_STRATEGY, desiredPermissions)) 48 | 49 | when: 50 | Job job = jobBuilder.build(JOB_PARENT) 51 | 52 | then: 53 | with(job.node) { 54 | def authorizationMatrixProperty = properties.'hudson.security.AuthorizationMatrixProperty' 55 | //assert !authorizationMatrixProperty.inheritanceStrategy.nonInheriting.isEmpty() 56 | authorizationMatrixProperty.inheritanceStrategy.'@class'.text() == 'org.jenkinsci.plugins.matrixauth.inheritance.NonInheritingStrategy' 57 | desiredPermissions.size() == authorizationMatrixProperty.permission.size() 58 | desiredPermissions.each { expectedPermission -> 59 | assert authorizationMatrixProperty.permission.find() { actualPermission -> 60 | "GROUP:${expectedPermission.permission}:${expectedPermission.ldapIdentifier}" == actualPermission.text() 61 | } 62 | } 63 | } 64 | } 65 | 66 | void 'test XML output with inheriting group perms'() { 67 | final Set desiredPermissions = [GroupPermission.permissionSetting("viewer", "hudson.model.Item.Read")] 68 | given: 69 | JobBuilder jobBuilder = new JobBuilder('test-job', 'test-job-description'). 70 | withConfigures(enableProjectBasedSecurity(INHERIT_PARENT_STRATEGY, desiredPermissions)) 71 | 72 | when: 73 | Job job = jobBuilder.build(JOB_PARENT) 74 | 75 | then: 76 | with(job.node) { 77 | def authorizationMatrixProperty = properties.'hudson.security.AuthorizationMatrixProperty' 78 | authorizationMatrixProperty.inheritanceStrategy.'@class'.text() == 'org.jenkinsci.plugins.matrixauth.inheritance.InheritParentStrategy' 79 | desiredPermissions.size() == authorizationMatrixProperty.permission.size() 80 | desiredPermissions.each { expectedPermission -> 81 | assert authorizationMatrixProperty.permission.find() { actualPermission -> 82 | "GROUP:${expectedPermission.permission}:${expectedPermission.ldapIdentifier}" == actualPermission.text() 83 | } 84 | } 85 | } 86 | } 87 | 88 | void 'test default XML output with global inheriting no group perms'() { 89 | final Set desiredPermissions = [] 90 | given: 91 | JobBuilder jobBuilder = new JobBuilder('test-job', 'test-job-description'). 92 | withConfigures(enableProjectBasedSecurity(INHERIT_GLOBAL_STRATEGY, desiredPermissions)) 93 | 94 | when: 95 | Job job = jobBuilder.build(JOB_PARENT) 96 | 97 | then: 98 | with(job.node) { 99 | def authorizationMatrixProperty = properties.'hudson.security.AuthorizationMatrixProperty' 100 | authorizationMatrixProperty.inheritanceStrategy.'@class'.text() == 'org.jenkinsci.plugins.matrixauth.inheritance.InheritGlobalStrategy' 101 | 0 == authorizationMatrixProperty.permission.size() 102 | } 103 | } 104 | 105 | void 'test XML output with group global inheriting overriding non group parent inheritance'() { 106 | final Set groupPermissions = [GroupPermission.permissionSetting("viewer", "hudson.model.Item.Cancel")] 107 | final Set permissions = [Permission.permissionSetting("viewer", "hudson.model.Item.Read")] 108 | 109 | given: 110 | JobBuilder jobBuilder = new JobBuilder('test-job', 'test-job-description'). 111 | withConfigures(enableProjectNonGroupBasedSecurity(INHERIT_PARENT_STRATEGY, permissions)). 112 | withConfigures(enableProjectBasedSecurity(INHERIT_GLOBAL_STRATEGY, groupPermissions)) 113 | 114 | when: 115 | Job job = jobBuilder.build(JOB_PARENT) 116 | 117 | then: 118 | with(job.node) { 119 | def authorizationMatrixProperty = properties.'hudson.security.AuthorizationMatrixProperty' 120 | authorizationMatrixProperty.inheritanceStrategy.'@class'.text() == 'org.jenkinsci.plugins.matrixauth.inheritance.InheritGlobalStrategy' 121 | 1 == authorizationMatrixProperty.permission.size() 122 | authorizationMatrixProperty.permission[0].text() == "GROUP:hudson.model.Item.Cancel:viewer" 123 | } 124 | } 125 | } 126 | 127 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | >**We are aware that this codebase contains outdated language. This may be due to a third party dependency which provides essential functionality or because of links to other projects. We are committed to providing an inclusive experience and are actively engaging with our suppliers and the open source community to address these issues.** 2 | 3 | # jenkins-job-builders 4 | [Download here](https://open.artefacts.tax.service.gov.uk/maven2/uk/gov/hmrc/jenkins-job-builders/) 5 | 6 | Automated Jenkins job and view configuration builders, via the [Jenkins Job DSL Plugin](https://github.com/jenkinsci/job-dsl-plugin) ([wiki](https://github.com/jenkinsci/job-dsl-plugin/wiki)). 7 | 8 | The problem with using a user interface for configuring a continuous build system like Jenkins is that it doesn't scale - teams should own their Jenkins configuration, but the normal approach in the user interface is to copy and paste jobs which quickly becomes unmaintainable when applying global fixes e.g. temporary diskspace management. 9 | 10 | This can be solved in Jenkins by using the Jenkins Job DSL plugin, which allows job configuration to be automated and scales up to 10s/100s of jobs. Teams can configure their own microservice, frontend, stubs, and test jobs in their own product-specific configuration file, using a convenience Builder pattern that encapsulates the Jobs DSL (in [Groovy](http://groovy-lang.org/)). If and when the existing Builders are insufficient, the raw Jobs DSL can still be used. 11 | 12 | ## Job Builders 13 | 14 | `JobBuilder` allows a job to be constructed from a series of SCM settings, publishers, and plugins. The raw Jobs DSL can be used if necessary. 15 | 16 | ## View Builders 17 | 18 | `ListViewBuilder` allows a job list to be constructed from a series of jobs. The raw Jobs DSL can be used if necessary. 19 | 20 | `BuildMonitorViewBuilder` allows a job monitor to be constructed from a series of jobs. The raw Jobs DSL can be used if necessary. 21 | 22 | ## Building 23 | 24 | 1. Run `./gradlew clean test` locally to test your changes. The test suite will ensure the Builders are capable of producing the expected config XML for Jenkins. 25 | 26 | ## Installing 27 | 28 | Create a Jenkins jobs repository akin to www.github.com/hmrc/jenkins-jobs, and then create a Jenkins job that weaves together this library with the jobs repository as follows: 29 | 30 | 1. Gradle Step 31 | * Use Gradle Wrapper = true 32 | * From Root Build Script Dir = true 33 | * Tasks = clean build 34 | 2. Process Jobs DSL Step 35 | * Look On Filesystem = true 36 | * DSL Scripts = DIR/*.groovy 37 | * Removed Jobs = Delete 38 | * Removed Views = Delete 39 | * Context To Use For Relative Job Names = Jenkins Root 40 | * Additional Classpath = DIR/*.jar (where jenkins-job-builders is in DIR) 41 | 42 | ## Examples 43 | 44 | The [open HMRC Jenkins jobs](https://github.com/hmrc/jenkins-jobs) are one example of how to use this library. 45 | 46 | ## Testing/Release process 47 | 48 | 1. Complete development: 49 | * Make changes 50 | Ensure tests are passing locally by running `task docker-test` 51 | * Merge changes to main 52 | * You can test by tagging your current local commit with the command in steps 3 and running the pipeline in step 4 to produce the package. 53 | 54 | 2. The `jenkins-job-builders` Jenkins job will be triggered by the merge to main 55 | 56 | 3. If successful, manually create a new git tag in the `jenkins-job-builder` repo: 57 | * Update the 'Release Notes' section below; 58 | * Add a new version number and description 59 | * Commit and merge to main 60 | * create a new GitHub tag 61 | * `git tag -a release/` 62 | * `git push origin tag release/` 63 | 4. Run the `jenkins-job-builders-hmrc-release` Jenkins job specifying the new tag number 64 | This will build the artefact and push it to Live Artifactory 65 | *NB: We do not store the `jenkins-job-builder` artifact in Lab03 Artifactory. The Labs point to Live Artifactory for when using this artefact* 66 | 67 | 5. Test labs: 68 | * In the `build-job` repo set the `jenkinsJobBuildersVersion` (in `build.gradle`) to the new version 69 | * Push branch 70 | * Run `seed-jenkins` from branch in a Lab of your choice 71 | * Tests job(s) as needed 72 | * *NB: For general changes `build-and-deploy-canary-service-pipeline` is always a good choice.* 73 | 74 | 7. If all good, merge branch to main. 75 | This will kick off `seed-jenkins` in Live. 76 | 77 | ## Release Notes 78 | * 18.9.2 (17/11/2025) - Setting package compatibility to java 21. Synced Jenkins plugin versions with build-jenkins-primary repo. Will fail if Jenkins shows plugin warnings. 79 | * 18.9.1 (11/11/2025) - Setting package compatibility to java 11. 80 | * 18.9.0 (10/11/2025) - Allow update of job description for failed jobs 81 | * 18.8.0 (09/09/2024) - Reverts Group Permissions into Configure block 82 | * 18.7.0 (05/09/2024) - Added Group permissions into the Authorization block in job builder 83 | * 18.5.0 (05/09/2024) - Add config class for remote parameterized trigger version 3.1.6.2+ 84 | * 18.4.0 (20/08/2024) - Update remote parameterized plugin 85 | * 18.2.0 (30/07/2024) - Updated Methods for handling timeout value 86 | * 18.1.0 (23/07/2024) - Added in Git Timeout option 87 | * 18.0.0 (22/07/2024) - Moving to new version number 88 | * 17.2.0 (19/07/2024) - Added disablerebuild test 89 | * 17.1.0 (19/07/2024) - Added ability to disable job rebuilds 90 | * 17.0.0 (18/07/2024) - Upgrade Jenkins-job-builder and unblock failing tests 91 | * 16.4.0 (16/07/2024) - Upgrade com.jfrog.artifactory plugin version to 4.33.1 92 | * 16.3.0 (12/07/2024) - Upgrade Jenkins-job-builder and unblock failing tests 93 | * 16.2.0 (06/03/2024) - Add support for a post build workspace cleanup 94 | * 16.0.0 (21/11/2023) - Add support for [timestamper](https://plugins.jenkins.io/timestamper/) 95 | * 15.0.0 (21/07/2023) - Remove ruby-runtime plugin support 96 | * 14.0.0 (29/06/2023) - Upgrade job-dsl to v1.82 97 | * 13.4.0 (18/08/2022) - Provide a `scalac` version agnostic SCoverage publisher 98 | * 13.3.0 (29/06/2022) - Disable "Show Committers" on build monitor views 99 | * 13.1.0 (19/11/2021) - Remove deprecated properties from cucumber and xvfb plugins 100 | * 13.0.0 (28/10/2021) - Only support Post Build Script plugin version 3.0.0 and higher 101 | * 12.0.0 (23/09/2021) - Support multiple build results in postBuildScriptPublisher 102 | * 11.42.0 (03/08/2021) - Don't add envInject properties if they are defined with a null value 103 | * 11.41.0 (02/08/2021) - Updated DSL property name in InjectEnvironmentJobProperty 104 | * 11.40.1 (02/08/2021) - Updated DSL property name in InjectEnvironmentJobProperty 105 | * 11.40.0 (02/08/2021) - Add the InjectEnvironmentJobProperty class to perform pre-scm environment injection 106 | * 11.39.0 (12/05/2021) - Allow ListViewBuilder to recurse into folders 107 | * 11.38.0 (04/05/2021) - add BuildDescriptionStep to allow jobs to set their own descriptions 108 | * 11.37.0 (23/03/2021) - Allow JobBuilder description to be modified after instantiation 109 | * 11.36.0 (12/03/2021) - add an optional parameter to shellStep "unstableReturnCode" that sets build to unstable based of given return code value 110 | * 11.35.0 (08/12/2020) - Release to hmrc-digital bintray account 111 | * 11.34.0 (13/10/2020) - Allow configuration of alwaysLinkToLastBuild in HtmlReportsPublisher 112 | * 11.33.0 (05/10/2020) - Modify cucumber reporter to mark build as failed when cucumber report marked as failed 113 | * 11.32.0 (07/08/2020) - Add support for the PostBuildScript Publisher plugin 114 | * 11.31.0 (17/01/2020) - Allow to specify the Scala version in the Coverage Report Publisher 115 | * 11.30.0 (25/10/2019) - Add support for honoring the refspec in the initial git fetch, and parameterise whether tags are pulled or not 116 | * 11.29.0 (01/10/2019) - Add support for [AWS CodeBuild with Jenkins plugin](https://wiki.jenkins.io/display/JENKINS/AWS+CodeBuild+Plugin) 117 | * 11.27.0 (15/04/2019) - Fix name bug in BuildMonitorViewBuilder. 118 | * 11.26.0 (02/04/2019) - Implement a conjoined username/password credentials binding. 119 | * 11.25.0 (29/03/2019) - Upgrade test harness jenkins version to v2.150.3 120 | * 11.24.0 (29/03/2019) - Add shallow SCM clone options and upgrade job-dsl-core to v1.72 121 | * 11.23.0 (06/03/2019) - Upgrade to job-dsl-core v1.71. 122 | * 11.22.0 (05/12/2018) - Add a method to insert a pre scm build job 123 | * 11.21.0 (05/11/2018) - Add active choice reactive parameter 124 | * 11.20.0 (31/10/2018) - Fix a bug where folders have spaces (for maven steps) 125 | * 11.18.0 (19/10/2018) - Add the UpstreamTrigger class to enable building a job based on the successful completion of upstream jobs 126 | * 11.17.0 (09/10/2018) - Add the GitHubPullRequestTrigger class and extend the GitHubScm class to allow refspec and name to be specified. 127 | * 11.14.0 (24/08/2018) - Merged SecretText and SecretUsernamPassword wrapper together 128 | * 11.13.0 (24/08/2018) - Added ability to recurse through sub folders in views 129 | * 11.12.0 (24/08/2018) - Modify cucumber reporter to use target/cucumber.json file 130 | * 11.11.0 (10/08/2018) - Support regex job filter in view builder. 131 | * 11.10.0 (03/08/2018) - Support inheritance strategies in Folders. 132 | * 11.9.0 (30/07/2018) - Provide the capability of configuring permissions on a per-job basis. 133 | * 11.8.0 (17/07/2018) - Add RemoteTriggerStep & RemoteToken. 134 | * 11.7.0 (11/07/2018) - Add GithubCommitNotifierPublisher. 135 | * 11.1.0 (18/04/2018) - Upgrade to job-dsl-core v1.69. 136 | * 11.0.0 (17/04/2018) - Upgrade to job-dsl-core v1.68. 137 | * 10.13.0 - Update views to align with job-dsl-core 1.38. 138 | * 10.12.0 - Add JobsTriggerStep which adds ability to trigger a downstream job as a build step. 139 | * This required an upgrade to job-dsl-core from 1.29 to 1.38. 140 | 141 | ## License 142 | 143 | This code is open source software licensed under the [Apache 2.0 License]("http://www.apache.org/licenses/LICENSE-2.0.html"). 144 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # 4 | # Copyright © 2015-2021 the original authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | # 21 | # Gradle start up script for POSIX generated by Gradle. 22 | # 23 | # Important for running: 24 | # 25 | # (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is 26 | # noncompliant, but you have some other compliant shell such as ksh or 27 | # bash, then to run this script, type that shell name before the whole 28 | # command line, like: 29 | # 30 | # ksh Gradle 31 | # 32 | # Busybox and similar reduced shells will NOT work, because this script 33 | # requires all of these POSIX shell features: 34 | # * functions; 35 | # * expansions «$var», «${var}», «${var:-default}», «${var+SET}», 36 | # «${var#prefix}», «${var%suffix}», and «$( cmd )»; 37 | # * compound commands having a testable exit status, especially «case»; 38 | # * various built-in commands including «command», «set», and «ulimit». 39 | # 40 | # Important for patching: 41 | # 42 | # (2) This script targets any POSIX shell, so it avoids extensions provided 43 | # by Bash, Ksh, etc; in particular arrays are avoided. 44 | # 45 | # The "traditional" practice of packing multiple parameters into a 46 | # space-separated string is a well documented source of bugs and security 47 | # problems, so this is (mostly) avoided, by progressively accumulating 48 | # options in "$@", and eventually passing that to Java. 49 | # 50 | # Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, 51 | # and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; 52 | # see the in-line comments for details. 53 | # 54 | # There are tweaks for specific operating systems such as AIX, CygWin, 55 | # Darwin, MinGW, and NonStop. 56 | # 57 | # (3) This script is generated from the Groovy template 58 | # https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt 59 | # within the Gradle project. 60 | # 61 | # You can find Gradle at https://github.com/gradle/gradle/. 62 | # 63 | ############################################################################## 64 | 65 | # Attempt to set APP_HOME 66 | 67 | # Resolve links: $0 may be a link 68 | app_path=$0 69 | 70 | # Need this for daisy-chained symlinks. 71 | while 72 | APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path 73 | [ -h "$app_path" ] 74 | do 75 | ls=$( ls -ld "$app_path" ) 76 | link=${ls#*' -> '} 77 | case $link in #( 78 | /*) app_path=$link ;; #( 79 | *) app_path=$APP_HOME$link ;; 80 | esac 81 | done 82 | 83 | # This is normally unused 84 | # shellcheck disable=SC2034 85 | APP_BASE_NAME=${0##*/} 86 | # Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) 87 | APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit 88 | 89 | # Use the maximum available, or set MAX_FD != -1 to use that value. 90 | MAX_FD=maximum 91 | 92 | warn () { 93 | echo "$*" 94 | } >&2 95 | 96 | die () { 97 | echo 98 | echo "$*" 99 | echo 100 | exit 1 101 | } >&2 102 | 103 | # OS specific support (must be 'true' or 'false'). 104 | cygwin=false 105 | msys=false 106 | darwin=false 107 | nonstop=false 108 | case "$( uname )" in #( 109 | CYGWIN* ) cygwin=true ;; #( 110 | Darwin* ) darwin=true ;; #( 111 | MSYS* | MINGW* ) msys=true ;; #( 112 | NONSTOP* ) nonstop=true ;; 113 | esac 114 | 115 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 116 | 117 | 118 | # Determine the Java command to use to start the JVM. 119 | if [ -n "$JAVA_HOME" ] ; then 120 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 121 | # IBM's JDK on AIX uses strange locations for the executables 122 | JAVACMD=$JAVA_HOME/jre/sh/java 123 | else 124 | JAVACMD=$JAVA_HOME/bin/java 125 | fi 126 | if [ ! -x "$JAVACMD" ] ; then 127 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 128 | 129 | Please set the JAVA_HOME variable in your environment to match the 130 | location of your Java installation." 131 | fi 132 | else 133 | JAVACMD=java 134 | if ! command -v java >/dev/null 2>&1 135 | then 136 | die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 137 | 138 | Please set the JAVA_HOME variable in your environment to match the 139 | location of your Java installation." 140 | fi 141 | fi 142 | 143 | # Increase the maximum file descriptors if we can. 144 | if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then 145 | case $MAX_FD in #( 146 | max*) 147 | # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. 148 | # shellcheck disable=SC2039,SC3045 149 | MAX_FD=$( ulimit -H -n ) || 150 | warn "Could not query maximum file descriptor limit" 151 | esac 152 | case $MAX_FD in #( 153 | '' | soft) :;; #( 154 | *) 155 | # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. 156 | # shellcheck disable=SC2039,SC3045 157 | ulimit -n "$MAX_FD" || 158 | warn "Could not set maximum file descriptor limit to $MAX_FD" 159 | esac 160 | fi 161 | 162 | # Collect all arguments for the java command, stacking in reverse order: 163 | # * args from the command line 164 | # * the main class name 165 | # * -classpath 166 | # * -D...appname settings 167 | # * --module-path (only if needed) 168 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. 169 | 170 | # For Cygwin or MSYS, switch paths to Windows format before running java 171 | if "$cygwin" || "$msys" ; then 172 | APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) 173 | CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) 174 | 175 | JAVACMD=$( cygpath --unix "$JAVACMD" ) 176 | 177 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 178 | for arg do 179 | if 180 | case $arg in #( 181 | -*) false ;; # don't mess with options #( 182 | /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath 183 | [ -e "$t" ] ;; #( 184 | *) false ;; 185 | esac 186 | then 187 | arg=$( cygpath --path --ignore --mixed "$arg" ) 188 | fi 189 | # Roll the args list around exactly as many times as the number of 190 | # args, so each arg winds up back in the position where it started, but 191 | # possibly modified. 192 | # 193 | # NB: a `for` loop captures its iteration list before it begins, so 194 | # changing the positional parameters here affects neither the number of 195 | # iterations, nor the values presented in `arg`. 196 | shift # remove old arg 197 | set -- "$@" "$arg" # push replacement arg 198 | done 199 | fi 200 | 201 | 202 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 203 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 204 | 205 | # Collect all arguments for the java command: 206 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, 207 | # and any embedded shellness will be escaped. 208 | # * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be 209 | # treated as '${Hostname}' itself on the command line. 210 | 211 | set -- \ 212 | "-Dorg.gradle.appname=$APP_BASE_NAME" \ 213 | -classpath "$CLASSPATH" \ 214 | org.gradle.wrapper.GradleWrapperMain \ 215 | "$@" 216 | 217 | # Stop when "xargs" is not available. 218 | if ! command -v xargs >/dev/null 2>&1 219 | then 220 | die "xargs is not available" 221 | fi 222 | 223 | # Use "xargs" to parse quoted args. 224 | # 225 | # With -n1 it outputs one arg per line, with the quotes and backslashes removed. 226 | # 227 | # In Bash we could simply go: 228 | # 229 | # readarray ARGS < <( xargs -n1 <<<"$var" ) && 230 | # set -- "${ARGS[@]}" "$@" 231 | # 232 | # but POSIX shell has neither arrays nor command substitution, so instead we 233 | # post-process each arg (as a line of input to sed) to backslash-escape any 234 | # character that might be a shell metacharacter, then use eval to reverse 235 | # that process (while maintaining the separation between arguments), and wrap 236 | # the whole thing up as a single "set" statement. 237 | # 238 | # This will of course break if any of these variables contains a newline or 239 | # an unmatched quote. 240 | # 241 | 242 | eval "set -- $( 243 | printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | 244 | xargs -n1 | 245 | sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | 246 | tr '\n' ' ' 247 | )" '"$@"' 248 | 249 | # Run Java and also save its console output to a file 250 | "$JAVACMD" "$@" | tee gradlew.log 251 | e=$? 252 | # Search for and show Jenkins plugin problems 253 | if grep -E "(needs to be installed|Failed to load|Update required)" gradlew.log; then 254 | [ 0 = "$e" ] && e=1 255 | printf '\n%s\n' "FAILED due to Jenkins plugin problems above." 256 | fi 257 | rm -f gradlew.log 258 | exit "$e" 259 | -------------------------------------------------------------------------------- /src/main/groovy/uk/gov/hmrc/jenkinsjobbuilders/domain/builder/JobBuilder.groovy: -------------------------------------------------------------------------------- 1 | package uk.gov.hmrc.jenkinsjobbuilders.domain.builder 2 | 3 | import javaposse.jobdsl.dsl.DslFactory 4 | import javaposse.jobdsl.dsl.Job 5 | import uk.gov.hmrc.jenkinsjobbuilders.domain.authorisation.Permission 6 | import uk.gov.hmrc.jenkinsjobbuilders.domain.authorisation.GroupPermission 7 | import uk.gov.hmrc.jenkinsjobbuilders.domain.configure.Configure 8 | import uk.gov.hmrc.jenkinsjobbuilders.domain.parameters.Parameter 9 | import uk.gov.hmrc.jenkinsjobbuilders.domain.publisher.Publisher 10 | import uk.gov.hmrc.jenkinsjobbuilders.domain.scm.Scm 11 | import uk.gov.hmrc.jenkinsjobbuilders.domain.throttle.ThrottleConfiguration 12 | import uk.gov.hmrc.jenkinsjobbuilders.domain.trigger.Trigger 13 | import uk.gov.hmrc.jenkinsjobbuilders.domain.step.Step 14 | import uk.gov.hmrc.jenkinsjobbuilders.domain.variable.EnvironmentVariable 15 | import uk.gov.hmrc.jenkinsjobbuilders.domain.wrapper.CredentialsBindings 16 | import uk.gov.hmrc.jenkinsjobbuilders.domain.wrapper.Wrapper 17 | 18 | import static java.util.Arrays.asList 19 | import static uk.gov.hmrc.jenkinsjobbuilders.domain.throttle.ThrottleConfiguration.throttleConfiguration 20 | import static uk.gov.hmrc.jenkinsjobbuilders.domain.wrapper.CredentialsBindings.combineCredentialsBindings 21 | import static uk.gov.hmrc.jenkinsjobbuilders.domain.wrapper.EnvironmentVariablesWrapper.environmentVariablesWrapper 22 | import static uk.gov.hmrc.jenkinsjobbuilders.domain.wrapper.PreScmStepsWrapper.preScmStepsWrapper 23 | import static uk.gov.hmrc.jenkinsjobbuilders.domain.publisher.PostBuildCleanWsPublisher.postBuildCleanWsPublisher 24 | 25 | final class JobBuilder implements Builder { 26 | private final String name 27 | private String description 28 | private final List parameters = [] 29 | private final List environmentVariables = [] 30 | private final List triggers = [] 31 | private final List steps = [] 32 | private final List preScmSteps = [] 33 | private final List publishers = [] 34 | private final List configures = [] 35 | private final List wrappers = [] 36 | private CredentialsBindings credentialsBindings 37 | private Scm scm 38 | private int daysToKeep = -1 39 | private int numToKeep = -1 40 | private String labelExpression 41 | private String environmentVariablesFile 42 | private String environmentVariablesScriptContent = '' 43 | private String environmentVariablesGroovyScript = '' 44 | private boolean concurrentBuilds = false 45 | private boolean rebuildDisabled = false 46 | private boolean disabled = false 47 | private final List permissions = [] 48 | private final List groupPermissions = [] 49 | private ThrottleConfiguration throttle 50 | private Publisher postBuildWorkspaceCleanup 51 | 52 | JobBuilder(String name, String description) { 53 | this.name = name 54 | this.description = description 55 | } 56 | 57 | JobBuilder withLogRotator(int daysToKeep, int numToKeep) { 58 | this.daysToKeep = daysToKeep 59 | this.numToKeep = numToKeep 60 | this 61 | } 62 | 63 | JobBuilder withScm(Scm scm) { 64 | this.scm = scm 65 | this 66 | } 67 | 68 | JobBuilder withTriggers(Trigger ... triggers) { 69 | this.triggers.addAll(triggers) 70 | this 71 | } 72 | 73 | JobBuilder withConcurrentBuilds() { 74 | this.concurrentBuilds = true 75 | this 76 | } 77 | 78 | JobBuilder withRebuildDisabled() { 79 | this.rebuildDisabled = true 80 | this 81 | } 82 | 83 | JobBuilder withDisabled() { 84 | this.disabled = true 85 | this 86 | } 87 | 88 | JobBuilder withSteps(Step ... steps) { 89 | withSteps(asList(steps)) 90 | } 91 | 92 | JobBuilder withSteps(List steps) { 93 | this.steps.addAll(steps) 94 | this 95 | } 96 | 97 | JobBuilder withPreScmSteps(Step ... steps) { 98 | withPreScmSteps(asList(steps)) 99 | } 100 | 101 | JobBuilder withPreScmSteps(List steps) { 102 | this.preScmSteps.addAll(steps) 103 | this 104 | } 105 | 106 | JobBuilder withParameters(Parameter ... parameters) { 107 | withParameters(asList(parameters)) 108 | } 109 | 110 | JobBuilder withParameters(List parameters) { 111 | this.parameters.addAll(parameters) 112 | this 113 | } 114 | 115 | JobBuilder withEnvironmentVariablesFile(String environmentVariablesFile) { 116 | this.environmentVariablesFile = environmentVariablesFile 117 | this 118 | } 119 | 120 | JobBuilder withEnvironmentVariablesScriptContent(String environmentVariablesScriptContent) { 121 | this.environmentVariablesScriptContent = environmentVariablesScriptContent 122 | this 123 | } 124 | 125 | JobBuilder withEnvironmentVariablesGroovyScript(String environmentVariablesGroovyScript) { 126 | this.environmentVariablesGroovyScript = environmentVariablesGroovyScript 127 | this 128 | } 129 | 130 | JobBuilder withEnvironmentVariables(EnvironmentVariable ... environmentsVariables) { 131 | withEnvironmentVariables(asList(environmentsVariables)) 132 | } 133 | 134 | JobBuilder withEnvironmentVariables(List environmentVariables) { 135 | this.environmentVariables.addAll(environmentVariables) 136 | this 137 | } 138 | 139 | JobBuilder withLabel(String labelExpression) { 140 | this.labelExpression = labelExpression 141 | this 142 | } 143 | 144 | JobBuilder withPublishers(Publisher ... publishers) { 145 | withPublishers(asList(publishers)) 146 | } 147 | 148 | JobBuilder withPublishers(List publishers) { 149 | this.publishers.addAll(publishers) 150 | this 151 | } 152 | 153 | JobBuilder withPostBuildWorkspaceCleanup(boolean cleanAbortedBuild = true, boolean cleanFailBuild = true, boolean cleanNotBuilt = true, boolean cleanSuccessfulBuild = true, boolean cleanUnstableBuild = true, boolean notFailBuild = true) { 154 | this.postBuildWorkspaceCleanup = postBuildCleanWsPublisher(cleanAbortedBuild, cleanFailBuild, cleanNotBuilt, cleanSuccessfulBuild, cleanUnstableBuild, notFailBuild) 155 | this 156 | } 157 | 158 | JobBuilder withConfigures(Configure ... configures) { 159 | withConfigures(asList(configures)) 160 | } 161 | 162 | JobBuilder withConfigures(List configures) { 163 | this.configures.addAll(configures) 164 | this 165 | } 166 | 167 | JobBuilder withWrappers(Wrapper ... wrappers) { 168 | withWrappers(asList(wrappers)) 169 | } 170 | 171 | JobBuilder withWrappers(List wrappers) { 172 | this.wrappers.addAll(wrappers) 173 | this 174 | } 175 | 176 | JobBuilder withCredentialsBindings(CredentialsBindings credentialsBindings) { 177 | this.credentialsBindings = combineCredentialsBindings(this.credentialsBindings, credentialsBindings) 178 | this 179 | } 180 | 181 | JobBuilder withPermissions(Permission ... permissions) { 182 | this.permissions.addAll(permissions) 183 | this 184 | } 185 | 186 | JobBuilder withPermissions(List permissions) { 187 | this.permissions.addAll(permissions) 188 | this 189 | } 190 | 191 | JobBuilder withGroupPermissions(GroupPermission ... permissions) { 192 | withGroupPermissions(asList(permissions)) 193 | this 194 | } 195 | 196 | JobBuilder withGroupPermissions(List permissions) { 197 | this.groupPermissions.addAll(permissions) 198 | this 199 | } 200 | 201 | JobBuilder withThrottle(List categories, int maxConcurrentPerNode, int maxConcurrentTotal, boolean throttleDisabled) { 202 | this.throttle = throttleConfiguration(categories, maxConcurrentPerNode, maxConcurrentTotal, throttleDisabled) 203 | this 204 | } 205 | 206 | JobBuilder withExtendedDescription(String description) { 207 | this.description += description 208 | this 209 | } 210 | 211 | @Override 212 | Job build(DslFactory dslFactory) { 213 | if (!this.environmentVariables.isEmpty()) { 214 | this.wrappers.add(0, environmentVariablesWrapper(environmentVariablesFile, environmentVariables, 215 | environmentVariablesScriptContent, environmentVariablesGroovyScript)) 216 | } 217 | 218 | if (!this.preScmSteps.isEmpty()) { 219 | this.wrappers.add(preScmStepsWrapper(preScmSteps)) 220 | } 221 | 222 | if (this.postBuildWorkspaceCleanup != null) { 223 | this.publishers.add(this.postBuildWorkspaceCleanup) 224 | } 225 | 226 | dslFactory.freeStyleJob(this.name) { 227 | it.description this.description 228 | logRotator(daysToKeep, numToKeep) 229 | concurrentBuild(concurrentBuilds) 230 | properties{ 231 | rebuild{ 232 | rebuildDisabled(this.rebuildDisabled) 233 | } 234 | } 235 | disabled(this.disabled) 236 | 237 | this.parameters.each { 238 | parameters(it.toDsl()) 239 | } 240 | 241 | if (labelExpression != null) { 242 | label labelExpression 243 | } 244 | 245 | if (scm != null) { 246 | scm(scm.toDsl()) 247 | } 248 | 249 | this.triggers.each { 250 | triggers(it.toDsl()) 251 | } 252 | 253 | this.steps.each { 254 | steps(it.toDsl()) 255 | } 256 | 257 | this.publishers.each { 258 | publishers(it.toDsl()) 259 | } 260 | 261 | this.configures.each { 262 | configure(it.toDsl()) 263 | } 264 | this.permissions.each { 265 | authorization(it.toDsl()) 266 | } 267 | this.groupPermissions.each { 268 | authorization(it.toDsl()) 269 | } 270 | if (this.throttle != null) { 271 | throttleConcurrentBuilds(this.throttle.toDsl()) 272 | } 273 | this.wrappers.each { 274 | wrappers(it.toDsl()) 275 | } 276 | if (this.credentialsBindings != null) { 277 | wrappers(credentialsBindings.toDsl()) 278 | } 279 | } 280 | } 281 | } 282 | --------------------------------------------------------------------------------