├── .github ├── CODEOWNERS ├── dependabot.yml └── release-drafter.yml ├── .gitignore ├── Jenkinsfile ├── LICENSE ├── README.md ├── examples ├── dynamic │ └── Jenkinsfile └── script │ └── Jenkinsfile ├── images └── config-driven-pipeline-project-recognizer.png ├── pom.xml └── src ├── main ├── java │ └── org │ │ └── jenkinsci │ │ └── plugins │ │ └── workflow │ │ └── multibranch │ │ └── template │ │ ├── ConfigDrivenWorkflowBranchProjectFactory.java │ │ ├── ConfigDrivenWorkflowMultiBranchProjectFactory.java │ │ ├── ConfigFileEnvironmentContributingAction.java │ │ ├── ConfigFileSCMBinder.java │ │ └── finder │ │ ├── ConfigurationValueFinder.java │ │ └── SupportedSCMFinder.java └── resources │ ├── index.jelly │ └── org │ └── jenkinsci │ └── plugins │ └── workflow │ └── multibranch │ └── template │ ├── ConfigDrivenWorkflowBranchProjectFactory │ ├── config.jelly │ ├── getting-started.jelly │ └── help-scriptPath.html │ ├── ConfigDrivenWorkflowMultiBranchProjectFactory │ ├── config.jelly │ ├── getting-started.jelly │ └── help-scriptPath.html │ ├── ConfigFileSCMBinder │ └── config.jelly │ └── Messages.properties └── test └── java └── org └── jenkinsci └── plugins └── workflow └── multibranch └── template └── finder └── ConfigurationValueFinderTest.java /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | * @justinharringa @afalko 2 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: maven 4 | directory: "/" 5 | schedule: 6 | interval: daily 7 | open-pull-requests-limit: 10 8 | -------------------------------------------------------------------------------- /.github/release-drafter.yml: -------------------------------------------------------------------------------- 1 | _extends: .github 2 | tag-template: config-driven-pipeline-$NEXT_MINOR_VERSION -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | target 2 | work 3 | .idea 4 | *.iml 5 | -------------------------------------------------------------------------------- /Jenkinsfile: -------------------------------------------------------------------------------- 1 | buildPlugin() 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Justin Harringa 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Config-Driven Pipeline Plugin 2 | [![License](https://img.shields.io/github/license/jenkinsci/config-driven-pipeline-plugin.svg)](LICENSE) 3 | [![Jenkins Plugin](https://img.shields.io/jenkins/plugin/v/config-driven-pipeline.svg)](https://plugins.jenkins.io/config-driven-pipeline) 4 | [![GitHub release](https://img.shields.io/github/release/jenkinsci/config-driven-pipeline-plugin.svg?label=changelog)](https://github.com/jenkinsci/config-driven-pipeline-plugin/releases/latest) 5 | [![Jenkins Plugin Installs](https://img.shields.io/jenkins/plugin/i/config-driven-pipeline.svg?color=blue)](https://plugins.jenkins.io/config-driven-pipeline) 6 | 7 | ## Purpose 8 | Would you like to share `Jenkinsfile` without copy-pasting in git (or other SCMs) but 9 | would also like to be able to have some variance in your `Jenkinsfile` (e.g. configuration 10 | values such as email address, different unit test scripts, etc...)? 11 | 12 | If so, this is the main driver of this plugin. We desired a central git-driven repository 13 | of trusted `Jenkinsfile` templates which are inherently visible, can be contributed to, but 14 | also allow us the ability to centrally roll out updates and improvements to hundreds of 15 | pipelines at a time. 16 | 17 | This plugin will select a Jenkinsfile based on config in the repository. What this means is 18 | that you can configure a whole GitHub Organization to use a Jenkinsfile repository and 19 | different repos can run different Jenkinsfiles in the central Jenkinsfile repo based on 20 | versioned configuration in the repo (no messing around with job configuration and it's all 21 | under version control). You simply point your 22 | [pipeline_template](https://github.com/jenkinsci/config-driven-pipeline-plugin#pipeline_template) 23 | to the path in the repo. This means you can switch between templates for different branches 24 | and test out new templates in PRs without having to muck around with job config. 25 | 26 | ## Setup 27 | This plugin provides you with a new Project Recognizer that you can use with any 28 | [Multibranch Pipeline type](https://jenkins.io/doc/book/pipeline/multibranch/#creating-a-multibranch-pipeline) 29 | such as a GitHub Organization or Multibranch Pipeline. 30 | 31 | ![Config-Driven Pipeline Project Recognizer](/images/config-driven-pipeline-project-recognizer.png) 32 | 33 | You'll simply set the `Config File Path` to a location where you expect the config file to reside in 34 | the repositories (traditionally at the root of the repo). 35 | 36 | ### pipeline_template 37 | The `pipeline_template` configuration key is reserved for finding the `Jenkinsfile` template you'd 38 | like to use out of the centralized `Jenkinsfile` repo. 39 | 40 | ### Config File Format - Most Any! 41 | The plugin itself is only going to search for a `pipeline_template` key/value in your Yaml, JSON, 42 | Java property file (and likely some others). This logic is in the `ConfigurationValueFinder` and 43 | we'd be happy to entertain additions to expand compatibility. We recommend using the 44 | [Pipeline Utility Steps Plugin](https://plugins.jenkins.io/pipeline-utility-steps) to parse your 45 | config but you're free to implement and validate this however you'd like in your `Jenkinsfile` 46 | templates. 47 | 48 | ## Config File Contents Available for Parsing! 49 | The plugin places the contents of the config file in the `PIPELINE_CONFIG` environment variable so 50 | that you don't have to read the file again. 51 | 52 | ## Why would I use this when I can use ${OTHER_SOLUTION}? 53 | ### Shared Libraries 54 | [Shared libraries](https://jenkins.io/doc/book/pipeline/shared-libraries/) are fantastic and 55 | are a great way to be able to make your pipeline code testable. However, it was nice to compose 56 | overall stages declaratively in Jenkinsfile and simply let each repo pass in configurable values 57 | such as the unit test command, Docker container to run under, etc... 58 | 59 | ### Buildpacks 60 | [Buildpacks](https://buildpacks.io/) are also awesome! However, sometimes there aren't quite the 61 | right buildpacks for your needs (and you could also use them within this :smile:). 62 | 63 | ## Upcoming Additions To This Repo 64 | * Example project configured via [Job DSL Plugin](https://plugins.jenkins.io/job-dsl) 65 | * More configuration information 66 | * Other shinies 67 | * FAQs? 68 | -------------------------------------------------------------------------------- /examples/dynamic/Jenkinsfile: -------------------------------------------------------------------------------- 1 | def config = readYaml text: "${env.PIPELINE_CONFIG}" 2 | pipeline { 3 | agent { 4 | label "${config.agent_label}" 5 | } 6 | stages { 7 | stage("run run ANYTHING") { 8 | steps { 9 | echo "What to run: ${config.run_me}" 10 | sh "${config.run_me}" 11 | } 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /examples/script/Jenkinsfile: -------------------------------------------------------------------------------- 1 | def config = readYaml text: "${env.PIPELINE_CONFIG}" 2 | pipeline { 3 | agent any 4 | stages { 5 | stage("run run.sh") { 6 | steps { 7 | echo "Script to run: ${config.script_to_run}" 8 | sh "./${config.script_to_run}" 9 | } 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /images/config-driven-pipeline-project-recognizer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jenkinsci/config-driven-pipeline-plugin/62d19d6eddd45e6fc234eaba36ef8da17da190bb/images/config-driven-pipeline-project-recognizer.png -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | 5 | 6 | plugin 7 | org.jenkins-ci.plugins 8 | 9 | 4.31 10 | 11 | 12 | config-driven-pipeline 13 | 1.4-SNAPSHOT 14 | hpi 15 | 16 | Config-Driven Pipeline Plugin 17 | Uses a config file in the source repo to route to a Jenkinsfile in a centralized repo of Jenkinsfiles 18 | and provides the config content as a PIPELINE_CONFIG environment variable to hydrate the pipeline with data. 19 | https://github.com/jenkinsci/config-driven-pipeline-plugin 20 | 21 | 22 | MIT License 23 | http://opensource.org/licenses/MIT 24 | 25 | 26 | 27 | 28 | justinharringa 29 | Justin Harringa 30 | jharringa@salesforce.com 31 | 32 | 33 | ma3oxuct 34 | Andrey Falko 35 | afalko@gmail.com 36 | 37 | 38 | 39 | scm:git:git://github.com/jenkinsci/${project.artifactId}-plugin.git 40 | scm:git:git@github.com:jenkinsci/${project.artifactId}-plugin.git 41 | http://github.com/jenkinsci/${project.artifactId}-plugin 42 | HEAD 43 | 44 | 45 | 1.16 46 | 2.4.1 47 | 0.8.8 48 | 8 49 | 1.8 50 | 2.277.4 51 | 4.13.2 52 | 3.0.0-M5 53 | 2.11.0 54 | UTF-8 55 | 2.17 56 | 2.6 57 | 5.8.2 58 | 59 | 60 | 61 | org.jenkins-ci 62 | annotation-indexer 63 | ${annotation-indexer.version} 64 | 65 | 66 | org.jenkins-ci.plugins 67 | branch-api 68 | 69 | 70 | org.jenkins-ci.plugins 71 | cloudbees-folder 72 | 73 | 74 | org.jenkins-ci.plugins 75 | git 76 | 77 | 78 | org.jenkins-ci.plugins 79 | junit 80 | test 81 | 82 | 83 | org.jenkins-ci.plugins 84 | scm-api 85 | 86 | 87 | org.jenkins-ci.plugins 88 | structs 89 | 90 | 91 | org.jenkins-ci.plugins.workflow 92 | workflow-cps 93 | 94 | 95 | org.jenkins-ci.plugins 96 | github-branch-source 97 | ${github-branch-source.version} 98 | 99 | 100 | org.jenkins-ci.plugins 101 | credentials 102 | 103 | 104 | org.jenkins-ci.plugins 105 | token-macro 106 | 107 | 108 | org.jenkins-ci.plugins.workflow 109 | workflow-multibranch 110 | ${workflow-multibranch.version} 111 | 112 | 113 | org.jenkins-ci.plugins.workflow 114 | workflow-api 115 | 116 | 117 | org.jenkins-ci.plugins.workflow 118 | workflow-job 119 | 120 | 121 | org.jenkins-ci.plugins.workflow 122 | workflow-aggregator 123 | 2.5 124 | test 125 | 126 | 127 | org.jenkins-ci.plugins.pipeline-stage-view 128 | pipeline-rest-api 129 | 2.19 130 | test 131 | 132 | 133 | org.jenkins-ci.plugins 134 | plain-credentials 135 | test 136 | 137 | 138 | org.jenkins-ci.plugins 139 | jackson2-api 140 | test 141 | 142 | 143 | org.jenkins-ci.plugins 144 | pipeline-utility-steps 145 | ${pipeline-utility-steps-test.version} 146 | test 147 | 148 | 149 | org.jenkins-ci.plugins.workflow 150 | workflow-scm-step 151 | 152 | 153 | org.jenkins-ci.plugins.workflow 154 | workflow-step-api 155 | 156 | 157 | org.jenkins-ci.plugins.workflow 158 | workflow-support 159 | 160 | 161 | org.jenkins-ci.plugins 162 | git 163 | tests 164 | test 165 | 166 | 167 | httpclient 168 | org.apache.httpcomponents 169 | 170 | 171 | 172 | 173 | org.apache.commons 174 | commons-lang3 175 | 3.12.0 176 | 177 | 178 | junit 179 | junit 180 | ${junit.version} 181 | test 182 | 183 | 184 | org.jenkins-ci.plugins.workflow 185 | workflow-multibranch 186 | ${workflow-multibranch.version} 187 | tests 188 | test 189 | 190 | 191 | org.jenkins-ci.plugins.workflow 192 | workflow-scm-step 193 | ${workflow-scm-step.version} 194 | tests 195 | test 196 | 197 | 198 | org.junit.jupiter 199 | junit-jupiter 200 | ${junit-jupiter.version} 201 | test 202 | 203 | 204 | org.junit.jupiter 205 | junit-jupiter-api 206 | ${junit-jupiter.version} 207 | test 208 | 209 | 210 | 211 | 212 | repo.jenkins-ci.org 213 | https://repo.jenkins-ci.org/public/ 214 | 215 | 216 | 217 | 218 | repo.jenkins-ci.org 219 | https://repo.jenkins-ci.org/public/ 220 | 221 | 222 | 223 | 224 | 225 | maven-compiler-plugin 226 | 3.9.0 227 | 228 | ${java.version} 229 | ${java.version} 230 | 231 | 232 | 233 | org.apache.maven.plugins 234 | maven-surefire-plugin 235 | ${maven-surefire-plugin.version} 236 | 237 | 238 | org.junit.vintage 239 | junit-vintage-engine 240 | ${junit-jupiter.version} 241 | 242 | 243 | 244 | 245 | org.jacoco 246 | jacoco-maven-plugin 247 | ${jacoco-maven-plugin.version} 248 | 249 | 250 | pre-unit-test 251 | 252 | prepare-agent 253 | 254 | 255 | 256 | post-unit-test 257 | test 258 | 259 | report 260 | 261 | 262 | 263 | 264 | 265 | org.codehaus.mojo 266 | versions-maven-plugin 267 | 2.8.1 268 | 269 | false 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | io.jenkins.tools.bom 278 | bom-2.222.x 279 | 887.vae9c8ac09ff7 280 | import 281 | pom 282 | 283 | 284 | 285 | 286 | -------------------------------------------------------------------------------- /src/main/java/org/jenkinsci/plugins/workflow/multibranch/template/ConfigDrivenWorkflowBranchProjectFactory.java: -------------------------------------------------------------------------------- 1 | package org.jenkinsci.plugins.workflow.multibranch.template; 2 | 3 | import edu.umd.cs.findbugs.annotations.NonNull; 4 | import hudson.Extension; 5 | import hudson.model.TaskListener; 6 | import hudson.scm.SCM; 7 | import hudson.scm.SCMDescriptor; 8 | import jenkins.scm.api.SCMProbeStat; 9 | import jenkins.scm.api.SCMSource; 10 | import jenkins.scm.api.SCMSourceCriteria; 11 | import org.apache.commons.lang.StringUtils; 12 | import org.jenkinsci.plugins.workflow.flow.FlowDefinition; 13 | import org.jenkinsci.plugins.workflow.multibranch.AbstractWorkflowBranchProjectFactory; 14 | import org.jenkinsci.plugins.workflow.multibranch.template.finder.SupportedSCMFinder; 15 | import org.kohsuke.stapler.DataBoundConstructor; 16 | import org.kohsuke.stapler.DataBoundSetter; 17 | 18 | import java.io.IOException; 19 | import java.util.Collection; 20 | 21 | public class ConfigDrivenWorkflowBranchProjectFactory extends AbstractWorkflowBranchProjectFactory { 22 | // TODO: Make this a parameter that users can adjust to their liking 23 | public static final String USER_DEFINITION_PATH = ".yourconfig.yml"; 24 | public static final String PIPELINE_TEMPLATE = "pipeline_template"; 25 | 26 | private String scriptPath = USER_DEFINITION_PATH; 27 | private SCM jenkinsFileScm = null; 28 | 29 | public Object readResolve() { 30 | if (this.scriptPath == null) { 31 | this.scriptPath = USER_DEFINITION_PATH; 32 | } 33 | return this; 34 | } 35 | 36 | @DataBoundSetter 37 | public void setScriptPath(String scriptPath) { 38 | if (StringUtils.isEmpty(scriptPath)) { 39 | this.scriptPath = USER_DEFINITION_PATH; 40 | } else { 41 | this.scriptPath = scriptPath; 42 | } 43 | } 44 | 45 | public String getScriptPath() { return scriptPath; } 46 | 47 | 48 | public SCM getJenkinsFileScm() { 49 | return jenkinsFileScm; 50 | } 51 | 52 | @DataBoundSetter 53 | public void setJenkinsFileScm(SCM jenkinsFileScm) { 54 | this.jenkinsFileScm = jenkinsFileScm; 55 | } 56 | 57 | @DataBoundConstructor 58 | public ConfigDrivenWorkflowBranchProjectFactory() {} 59 | 60 | @Override protected FlowDefinition createDefinition() { 61 | // This creates the CpsScmFlowDefinition... create a new type of "binder"??? 62 | // We need a non-hardcoded version of this class... it does almost everything we want already... 63 | return new ConfigFileSCMBinder(scriptPath, jenkinsFileScm); 64 | } 65 | 66 | @Override protected SCMSourceCriteria getSCMSourceCriteria(SCMSource source) { 67 | return new SCMSourceCriteria() { 68 | @Override public boolean isHead(@NonNull SCMSourceCriteria.Probe probe, @NonNull TaskListener listener) throws IOException { 69 | SCMProbeStat stat = probe.stat(scriptPath); 70 | switch (stat.getType()) { 71 | case NONEXISTENT: 72 | if (stat.getAlternativePath() != null) { 73 | listener.getLogger().format(" ‘%s’ not found (but found ‘%s’, search is case sensitive)%n", scriptPath, stat.getAlternativePath()); 74 | } else { 75 | listener.getLogger().format(" ‘%s’ not found%n", scriptPath); 76 | } 77 | return false; 78 | case DIRECTORY: 79 | listener.getLogger().format(" ‘%s’ found but is a directory not a file%n", scriptPath); 80 | return false; 81 | default: 82 | listener.getLogger().format(" ‘%s’ found%n", scriptPath); 83 | return true; 84 | 85 | } 86 | } 87 | 88 | @Override 89 | public int hashCode() { 90 | return getClass().hashCode(); 91 | } 92 | 93 | @Override 94 | public boolean equals(Object obj) { 95 | return getClass().isInstance(obj); 96 | } 97 | }; 98 | } 99 | 100 | @Extension 101 | public static class DescriptorImpl extends AbstractWorkflowBranchProjectFactory.AbstractWorkflowBranchProjectFactoryDescriptor { 102 | // This should update the UI 103 | @Override public String getDisplayName() { 104 | return "by " + Messages.ProjectRecognizer_DisplayName(); 105 | } 106 | 107 | public Collection> getApplicableDescriptors() { 108 | return SupportedSCMFinder.getSupportedSCMs(); 109 | } 110 | } 111 | 112 | } -------------------------------------------------------------------------------- /src/main/java/org/jenkinsci/plugins/workflow/multibranch/template/ConfigDrivenWorkflowMultiBranchProjectFactory.java: -------------------------------------------------------------------------------- 1 | package org.jenkinsci.plugins.workflow.multibranch.template; 2 | 3 | import hudson.Extension; 4 | import hudson.scm.SCM; 5 | import hudson.scm.SCMDescriptor; 6 | import jenkins.branch.MultiBranchProjectFactoryDescriptor; 7 | import jenkins.scm.api.SCMSource; 8 | import jenkins.scm.api.SCMSourceCriteria; 9 | import org.apache.commons.lang.StringUtils; 10 | import org.jenkinsci.plugins.workflow.multibranch.AbstractWorkflowMultiBranchProjectFactory; 11 | import org.jenkinsci.plugins.workflow.multibranch.WorkflowBranchProjectFactory; 12 | import org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject; 13 | import org.jenkinsci.plugins.workflow.multibranch.template.finder.SupportedSCMFinder; 14 | import org.kohsuke.stapler.DataBoundConstructor; 15 | import org.kohsuke.stapler.DataBoundSetter; 16 | 17 | import java.io.IOException; 18 | import java.util.Collection; 19 | 20 | import static org.jenkinsci.plugins.workflow.multibranch.template.ConfigDrivenWorkflowBranchProjectFactory.USER_DEFINITION_PATH; 21 | 22 | /** 23 | * Defines organization folders by {@link WorkflowBranchProjectFactory}. 24 | */ 25 | public class ConfigDrivenWorkflowMultiBranchProjectFactory extends AbstractWorkflowMultiBranchProjectFactory { 26 | 27 | private String scriptPath = USER_DEFINITION_PATH; 28 | private SCM jenkinsFileScm = null; 29 | 30 | public Object readResolve() { 31 | if (this.scriptPath == null) { 32 | this.scriptPath = USER_DEFINITION_PATH; 33 | } 34 | return this; 35 | } 36 | 37 | @DataBoundSetter 38 | public void setScriptPath(String scriptPath) { 39 | if (StringUtils.isEmpty(scriptPath)) { 40 | this.scriptPath = USER_DEFINITION_PATH; 41 | } else { 42 | this.scriptPath = scriptPath; 43 | } 44 | } 45 | 46 | public String getScriptPath() { return scriptPath; } 47 | 48 | 49 | public SCM getJenkinsFileScm() { 50 | return jenkinsFileScm; 51 | } 52 | 53 | @DataBoundSetter 54 | public void setJenkinsFileScm(SCM jenkinsFileScm) { 55 | this.jenkinsFileScm = jenkinsFileScm; 56 | } 57 | 58 | @DataBoundConstructor 59 | public ConfigDrivenWorkflowMultiBranchProjectFactory() {} 60 | 61 | @Override protected SCMSourceCriteria getSCMSourceCriteria(SCMSource source) { 62 | return newProjectFactory().getSCMSourceCriteria(source); 63 | } 64 | 65 | private ConfigDrivenWorkflowBranchProjectFactory newProjectFactory() { 66 | ConfigDrivenWorkflowBranchProjectFactory workflowBranchProjectFactory = new ConfigDrivenWorkflowBranchProjectFactory(); 67 | workflowBranchProjectFactory.setScriptPath(scriptPath); 68 | workflowBranchProjectFactory.setJenkinsFileScm(jenkinsFileScm); 69 | return workflowBranchProjectFactory; 70 | } 71 | 72 | @Extension 73 | public static class DescriptorImpl extends MultiBranchProjectFactoryDescriptor { 74 | 75 | @Override public ConfigDrivenWorkflowMultiBranchProjectFactory newInstance() { 76 | return new ConfigDrivenWorkflowMultiBranchProjectFactory(); 77 | } 78 | 79 | @Override public String getDisplayName() { 80 | return Messages.ProjectRecognizer_DisplayName(); 81 | } 82 | 83 | public Collection> getApplicableDescriptors() { 84 | return SupportedSCMFinder.getSupportedSCMs(); 85 | } 86 | } 87 | 88 | protected void customize(WorkflowMultiBranchProject project) throws IOException, InterruptedException { 89 | project.setProjectFactory(newProjectFactory()); 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /src/main/java/org/jenkinsci/plugins/workflow/multibranch/template/ConfigFileEnvironmentContributingAction.java: -------------------------------------------------------------------------------- 1 | package org.jenkinsci.plugins.workflow.multibranch.template; 2 | 3 | import hudson.EnvVars; 4 | import hudson.Extension; 5 | import hudson.model.*; 6 | 7 | import javax.annotation.Nonnull; 8 | 9 | /** 10 | * Add the specified configContents to an environment variable called PIPELINE_CONFIG for a run with this 11 | * Action 12 | */ 13 | @Extension 14 | public class ConfigFileEnvironmentContributingAction extends InvisibleAction implements EnvironmentContributingAction { 15 | 16 | public static final String PIPELINE_CONFIG = "PIPELINE_CONFIG"; 17 | private String configContents; 18 | 19 | public ConfigFileEnvironmentContributingAction() { 20 | // @Extension annotated classes must have a public no-argument constructor 21 | super(); 22 | } 23 | 24 | /** 25 | * Init with the configContents which should be added to PIPELINE_CONFIG 26 | * @param configContents 27 | */ 28 | public ConfigFileEnvironmentContributingAction(String configContents) { 29 | this.configContents = configContents; 30 | } 31 | 32 | @Override 33 | public void buildEnvironment(@Nonnull Run run, @Nonnull EnvVars env) { 34 | env.put(PIPELINE_CONFIG, configContents); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/org/jenkinsci/plugins/workflow/multibranch/template/ConfigFileSCMBinder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License 3 | * 4 | * Copyright 2015 CloudBees, Inc. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | */ 24 | 25 | package org.jenkinsci.plugins.workflow.multibranch.template; 26 | 27 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; 28 | import hudson.AbortException; 29 | import hudson.Extension; 30 | import hudson.FilePath; 31 | import hudson.Functions; 32 | import hudson.model.*; 33 | import hudson.scm.SCM; 34 | import hudson.slaves.WorkspaceList; 35 | import jenkins.branch.Branch; 36 | import jenkins.model.Jenkins; 37 | import jenkins.scm.api.*; 38 | import jenkins.scm.api.mixin.ChangeRequestSCMHead; 39 | import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; 40 | import org.jenkinsci.plugins.workflow.flow.FlowDefinition; 41 | import org.jenkinsci.plugins.workflow.flow.FlowDefinitionDescriptor; 42 | import org.jenkinsci.plugins.workflow.flow.FlowExecution; 43 | import org.jenkinsci.plugins.workflow.flow.FlowExecutionOwner; 44 | import org.jenkinsci.plugins.workflow.job.WorkflowJob; 45 | import org.jenkinsci.plugins.workflow.job.WorkflowRun; 46 | import org.jenkinsci.plugins.workflow.multibranch.BranchJobProperty; 47 | import org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject; 48 | import org.jenkinsci.plugins.workflow.multibranch.template.finder.ConfigurationValueFinder; 49 | import org.jenkinsci.plugins.workflow.steps.scm.GenericSCMStep; 50 | import org.jenkinsci.plugins.workflow.steps.scm.SCMStep; 51 | import org.kohsuke.stapler.DataBoundConstructor; 52 | 53 | import java.io.FileNotFoundException; 54 | import java.io.IOException; 55 | import java.util.List; 56 | 57 | import static org.jenkinsci.plugins.workflow.multibranch.template.ConfigDrivenWorkflowBranchProjectFactory.PIPELINE_TEMPLATE; 58 | import static org.jenkinsci.plugins.workflow.multibranch.template.ConfigDrivenWorkflowBranchProjectFactory.USER_DEFINITION_PATH; 59 | 60 | /** 61 | * Checks out the desired version of {@link ConfigDrivenWorkflowBranchProjectFactory#USER_DEFINITION_PATH}. 62 | */ 63 | class ConfigFileSCMBinder extends FlowDefinition { 64 | 65 | private String scriptPath; 66 | private SCM jenkinsFileScm; 67 | 68 | public Object readResolve() { 69 | if (this.scriptPath == null) { 70 | this.scriptPath = USER_DEFINITION_PATH; 71 | } 72 | return this; 73 | } 74 | 75 | @DataBoundConstructor public ConfigFileSCMBinder(String scriptPath, SCM jenkinsFileScm) { 76 | this.scriptPath = scriptPath; 77 | this.jenkinsFileScm = jenkinsFileScm; 78 | } 79 | 80 | public static final String INAPPROPRIATE_CONTEXT = "inappropriate context"; 81 | 82 | @SuppressFBWarnings(value = "RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE", 83 | justification = "SCMFileSystem.of can return a null but spotbugs does not understand") 84 | @Override public FlowExecution create(FlowExecutionOwner handle, TaskListener listener, List actions) throws Exception { 85 | Queue.Executable exec = handle.getExecutable(); 86 | if (!(exec instanceof WorkflowRun)) { 87 | throw new IllegalStateException(INAPPROPRIATE_CONTEXT); 88 | } 89 | WorkflowRun build = (WorkflowRun) exec; 90 | WorkflowJob job = build.getParent(); 91 | BranchJobProperty property = job.getProperty(BranchJobProperty.class); 92 | if (property == null) { 93 | throw new IllegalStateException(INAPPROPRIATE_CONTEXT); 94 | } 95 | Branch branch = property.getBranch(); 96 | ItemGroup parent = job.getParent(); 97 | if (!(parent instanceof WorkflowMultiBranchProject)) { 98 | throw new IllegalStateException(INAPPROPRIATE_CONTEXT); 99 | } 100 | SCMSource scmSource = ((WorkflowMultiBranchProject) parent).getSCMSource(branch.getSourceId()); 101 | if (scmSource == null) { 102 | throw new IllegalStateException(branch.getSourceId() + " not found"); 103 | } 104 | SCMHead head = branch.getHead(); 105 | SCMRevision tip = scmSource.fetch(head, listener); 106 | String script = null; 107 | String configContents; 108 | if (tip != null) { 109 | // TODO are we getting an extra "Could not update commit status." from here? 110 | build.addAction(new SCMRevisionAction(scmSource, tip)); 111 | if (head instanceof ChangeRequestSCMHead) { 112 | // TODO evaluate if there's a better way to snag pull requests 113 | // Keep in mind that the pull request could be: 114 | // a) coming from the same repo (easy) 115 | // b) coming from a fork (why we defaulted to this...) 116 | SCM scm = scmSource.build(head, tip); 117 | listener.getLogger().println("Checking out " + scm.getKey() + " to read " + scriptPath); 118 | FilePath dir; 119 | Node node = Jenkins.getInstanceOrNull(); 120 | if (node == null) { 121 | throw new IOException("Unable to communicate with Jenkins node"); 122 | } 123 | FilePath baseWorkspace = node.getWorkspaceFor(build.getParent()); 124 | if (baseWorkspace == null) { 125 | throw new IOException(node.getDisplayName() + " may be offline"); 126 | } 127 | dir = baseWorkspace.withSuffix( 128 | System.getProperty(WorkspaceList.class.getName(), "@") + "script"); 129 | Computer computer = node.toComputer(); 130 | if (computer == null) { 131 | throw new IOException(node.getDisplayName() + " may be offline"); 132 | } 133 | SCMStep delegate = new GenericSCMStep(scm); 134 | delegate.setPoll(true); 135 | delegate.setChangelog(true); 136 | try (WorkspaceList.Lease lease = computer.getWorkspaceList().acquire(dir)) { 137 | delegate.checkout(build, dir, listener, node.createLauncher(listener)); 138 | FilePath scriptFile = dir.child(scriptPath); 139 | if (!scriptFile.absolutize().getRemote().replace('\\', '/').startsWith(dir.absolutize().getRemote().replace('\\', '/') + '/')) { // TODO JENKINS-26838 140 | throw new IOException(scriptFile + " is not inside " + dir); 141 | } 142 | if (!scriptFile.exists()) { 143 | throw new AbortException(scriptFile + " not found"); 144 | } 145 | configContents = scriptFile.readToString(); 146 | } 147 | } else { 148 | try (SCMFileSystem fs = SCMFileSystem.of(scmSource, head, tip)) { 149 | if (fs != null) { // JENKINS-33273 150 | try { 151 | configContents = fs.child(scriptPath).contentAsString(); 152 | listener.getLogger().println("Obtained " + scriptPath); 153 | } catch (IOException | InterruptedException x) { 154 | throw new AbortException(String.format("Could not do lightweight checkout, %n%s", 155 | Functions.printThrowable(x).trim())); 156 | } 157 | 158 | } else { 159 | // TODO: Evaluate if this is necessary... 160 | // PRs were getting here because they're not trusted revisions but we're checking 161 | // for PRs above... Are there other possible scenarios or should we just bail? 162 | throw new AbortException("Could not do a lightweight checkout and retrieve an SCMFileSystem"); 163 | } 164 | } 165 | } 166 | if (configContents == null) { 167 | String pipelineTemplateNotFound = 168 | String.format("Could not find a value for %s in %s", PIPELINE_TEMPLATE, scriptPath); 169 | throw new AbortException(pipelineTemplateNotFound); 170 | } else { 171 | String jenkinsfilePathString = 172 | ConfigurationValueFinder.findFirstConfigurationValue(configContents, 173 | ConfigDrivenWorkflowBranchProjectFactory.PIPELINE_TEMPLATE); 174 | 175 | build.addAction(new ConfigFileEnvironmentContributingAction(configContents)); 176 | 177 | try (SCMFileSystem scriptFileSystem = SCMFileSystem.of(job, jenkinsFileScm)) { 178 | if (scriptFileSystem != null) { 179 | script = scriptFileSystem.child(jenkinsfilePathString).contentAsString(); 180 | listener.getLogger().println("Obtained " + jenkinsfilePathString); 181 | 182 | } 183 | 184 | } catch (FileNotFoundException exception) { 185 | throw new AbortException(String.format("Could not find file %s", jenkinsfilePathString)); 186 | } 187 | } 188 | 189 | if (script != null) { 190 | return new CpsFlowDefinition(script, true).create(handle, listener, actions); 191 | } 192 | } 193 | // TODO evaluate if there's something else we should be looking at for no `tip` or `script` 194 | throw new AbortException("Unable to properly load a script file."); 195 | } 196 | 197 | @Extension 198 | public static class DescriptorImpl extends FlowDefinitionDescriptor { 199 | 200 | @Override public String getDisplayName() { 201 | return "Pipeline script from " + Messages.ProjectRecognizer_DisplayName(); 202 | } 203 | 204 | } 205 | 206 | /** Want to display this in the r/o configuration for a branch project, but not offer it on standalone jobs or in any other context. */ 207 | @Extension 208 | public static class HideMeElsewhere extends DescriptorVisibilityFilter { 209 | 210 | @Override public boolean filter(Object context, Descriptor descriptor) { 211 | if (descriptor instanceof DescriptorImpl) { 212 | return context instanceof WorkflowJob && ((WorkflowJob) context).getParent() instanceof WorkflowMultiBranchProject; 213 | } 214 | return true; 215 | } 216 | 217 | } 218 | 219 | } 220 | -------------------------------------------------------------------------------- /src/main/java/org/jenkinsci/plugins/workflow/multibranch/template/finder/ConfigurationValueFinder.java: -------------------------------------------------------------------------------- 1 | package org.jenkinsci.plugins.workflow.multibranch.template.finder; 2 | 3 | /** 4 | * Finds a key's value from a string in many config formats (e.g. properties, yaml, json) 5 | */ 6 | public class ConfigurationValueFinder { 7 | 8 | private ConfigurationValueFinder() {} 9 | 10 | /** 11 | * This method will find the first given {@code keyToFind} in {@code configurationContents} and then 12 | * return the value of it assuming that the key/value is represented in the following formats: 13 | * 20 | * Whitespace and " and ' characters are removed. 21 | * @param configurationContents the configuration text to search 22 | * @param keyToFind the key to find in the {@code configurationContents} 23 | * @return value of the associated {@code keyToFind}; returns null if not found 24 | */ 25 | public static String findFirstConfigurationValue(String configurationContents, String keyToFind) { 26 | if (keyToFind == null || configurationContents == null) { 27 | return null; 28 | } 29 | int indexOfKey = configurationContents.indexOf(keyToFind); 30 | if (indexOfKey == -1) { 31 | return null; 32 | } 33 | String delimiterAndValue = getDelimiterAndValue(configurationContents, keyToFind, indexOfKey); 34 | String valueWithoutDelimitersQuotesAndTicks = removeDelimitersQuotesAndTicks(delimiterAndValue); 35 | if (valueWithoutDelimitersQuotesAndTicks == null) { 36 | return null; 37 | } 38 | return valueWithoutDelimitersQuotesAndTicks.trim(); 39 | } 40 | 41 | private static String getDelimiterAndValue(String configurationContents, String keyToFind, int indexOfKey) { 42 | int indexAfterKey = keyToFind.length() + indexOfKey; 43 | int firstNewLine = configurationContents.indexOf("\n", indexAfterKey); 44 | if (firstNewLine == -1) { 45 | return configurationContents.substring(indexAfterKey); 46 | } 47 | return configurationContents.substring(indexAfterKey, firstNewLine); 48 | } 49 | 50 | /** 51 | * Remove colon or equal delimiters as well as "s and 's from the {@code inputString} 52 | * @param inputString small string which potentially contains a colon or equals sign 53 | * @return string without delimiters 54 | */ 55 | private static String removeDelimitersQuotesAndTicks(String inputString) { 56 | String charsToRemove = ":='\""; 57 | if (inputString == null) { 58 | return null; 59 | } 60 | StringBuilder outputString = new StringBuilder(); 61 | for (char character : inputString.toCharArray()) { 62 | // If character isn't in charsToRemove 63 | if (charsToRemove.indexOf(character) == -1) { 64 | outputString.append(character); 65 | } 66 | } 67 | return outputString.toString(); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/org/jenkinsci/plugins/workflow/multibranch/template/finder/SupportedSCMFinder.java: -------------------------------------------------------------------------------- 1 | package org.jenkinsci.plugins.workflow.multibranch.template.finder; 2 | 3 | import hudson.scm.SCM; 4 | import hudson.scm.SCMDescriptor; 5 | 6 | import java.util.ArrayList; 7 | import java.util.Collection; 8 | import java.util.List; 9 | 10 | /** 11 | * Finder to return only supported SCMs 12 | */ 13 | public class SupportedSCMFinder { 14 | 15 | public static Collection> getSupportedSCMs() { 16 | List> list = new ArrayList<>(); 17 | for (SCMDescriptor scmDescriptor : SCM.all()) { 18 | // It doesn't really make sense to have the None SCM per the spirit of this plugin. 19 | if (!scmDescriptor.getDisplayName().equals("None")) { 20 | list.add(scmDescriptor); 21 | } 22 | } 23 | return list; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/resources/index.jelly: -------------------------------------------------------------------------------- 1 | 2 |
3 | Share your Jenkinsfiles in a configurable way, driven by SCM. 4 |
5 | -------------------------------------------------------------------------------- /src/main/resources/org/jenkinsci/plugins/workflow/multibranch/template/ConfigDrivenWorkflowBranchProjectFactory/config.jelly: -------------------------------------------------------------------------------- 1 | 2 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /src/main/resources/org/jenkinsci/plugins/workflow/multibranch/template/ConfigDrivenWorkflowBranchProjectFactory/getting-started.jelly: -------------------------------------------------------------------------------- 1 | 24 | 25 | 26 | Pipeline Multibranch projects recognize and build repositories with a file named based on your convention (e.g. .yourconfig.yml) in branches of the repository. 27 | This file should contain a valid 28 | Jenkins Pipeline script. See also: 29 | Creating Multibranch Projects. 30 | 31 | -------------------------------------------------------------------------------- /src/main/resources/org/jenkinsci/plugins/workflow/multibranch/template/ConfigDrivenWorkflowBranchProjectFactory/help-scriptPath.html: -------------------------------------------------------------------------------- 1 |
2 | Relative location within the checkout of your Pipeline script. 3 | Note that it will always be run inside a Groovy sandbox. 4 | Default is Jenkinsfile if left empty. 5 | (just use checkout scm to retrieve sources from the same location as is configured here) 6 |
7 | -------------------------------------------------------------------------------- /src/main/resources/org/jenkinsci/plugins/workflow/multibranch/template/ConfigDrivenWorkflowMultiBranchProjectFactory/config.jelly: -------------------------------------------------------------------------------- 1 | 2 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /src/main/resources/org/jenkinsci/plugins/workflow/multibranch/template/ConfigDrivenWorkflowMultiBranchProjectFactory/getting-started.jelly: -------------------------------------------------------------------------------- 1 | 24 | 25 | 26 | Pipeline Multibranch projects recognize and build repositories with a file named based on your convention (e.g. .yourconfig.yml) in branches of the repository. 27 | This file should contain a valid 28 | Jenkins Pipeline script. See also: 29 | Creating Multibranch Projects. 30 | 31 | -------------------------------------------------------------------------------- /src/main/resources/org/jenkinsci/plugins/workflow/multibranch/template/ConfigDrivenWorkflowMultiBranchProjectFactory/help-scriptPath.html: -------------------------------------------------------------------------------- 1 |
2 | Relative location within the checkout of your Pipeline script. 3 | Note that it will always be run inside a Groovy sandbox. 4 | Default is Jenkinsfile if left empty. 5 | (just use checkout scm to retrieve sources from the same location as is configured here) 6 |
7 | -------------------------------------------------------------------------------- /src/main/resources/org/jenkinsci/plugins/workflow/multibranch/template/ConfigFileSCMBinder/config.jelly: -------------------------------------------------------------------------------- 1 | 2 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /src/main/resources/org/jenkinsci/plugins/workflow/multibranch/template/Messages.properties: -------------------------------------------------------------------------------- 1 | ProjectRecognizer.DisplayName=Config-Driven Pipeline -------------------------------------------------------------------------------- /src/test/java/org/jenkinsci/plugins/workflow/multibranch/template/finder/ConfigurationValueFinderTest.java: -------------------------------------------------------------------------------- 1 | package org.jenkinsci.plugins.workflow.multibranch.template.finder; 2 | 3 | import org.junit.jupiter.params.ParameterizedTest; 4 | import org.junit.jupiter.params.provider.Arguments; 5 | import org.junit.jupiter.params.provider.MethodSource; 6 | 7 | import java.util.stream.Stream; 8 | 9 | import static org.junit.jupiter.api.Assertions.*; 10 | 11 | class ConfigurationValueFinderTest { 12 | 13 | public static final String EXPECTED_RESULT = "expectedResult"; 14 | 15 | /** 16 | * Simple helper method to call ConfigurationValueFinder.findFirstConfigurationValue 17 | * to improve test readability. 18 | * 19 | * @param configurationContents test config 20 | * @param keyToFind test key 21 | * @return evaluated result 22 | */ 23 | private String find(String configurationContents, String keyToFind) { 24 | return ConfigurationValueFinder.findFirstConfigurationValue(configurationContents, keyToFind); 25 | } 26 | 27 | private static Stream keyNotFoundProvider() { 28 | return Stream.of( 29 | Arguments.of("null key", null, "test"), 30 | Arguments.of("null config", "someKey", null), 31 | Arguments.of("key not found", "someKey", "keynotfound") 32 | ); 33 | } 34 | 35 | @ParameterizedTest 36 | @MethodSource("keyNotFoundProvider") 37 | void cannotFindKey(String testCase, String keyToFind, String configurationContents) { 38 | assertNull(find(configurationContents, keyToFind), "Should not have found value for " + testCase); 39 | } 40 | 41 | 42 | private static Stream keysFoundProvider() { 43 | return Stream.of( 44 | Arguments.of("noNewLine", "noNewLine:" + EXPECTED_RESULT), 45 | Arguments.of("firstKey", "firstKey:" + EXPECTED_RESULT + "\n test1:second\n test1:third"), 46 | Arguments.of("lastKey", "bare:hi\n test1:second\n lastKey:" + EXPECTED_RESULT), 47 | Arguments.of("keyWithQuotesAround", "\"keyWithQuotesAround\":\"" + EXPECTED_RESULT + "\"\n test1:second\n test1:third"), 48 | Arguments.of("keyWithTicksAround", "'keyWithTicksAround':'" + EXPECTED_RESULT + "'\n test1:second\n test1:third"), 49 | Arguments.of("keyWithTicksSpaces", "'keyWithTicksSpaces' : '" + EXPECTED_RESULT + "' \n test1:second\n test1:third"), 50 | Arguments.of("keyWithEqualsDelim", "keyWithEqualsDelim=\"" + EXPECTED_RESULT + "\"\ntest1=second\ntest2=third"), 51 | Arguments.of("lastKeyWithEqualsDelim", "equals=\"hi\"\ntest1=second\nlastKeyWithEqualsDelim=" + EXPECTED_RESULT), 52 | Arguments.of("keyWithSpaceAroundEquals", "keyWithSpaceAroundEquals = \"" + EXPECTED_RESULT + "\"\ntest1 = second\ntest2 = third"), 53 | Arguments.of("lastKeyWithSpaceyEquals", "equals_space = \"hi\"\ntest1 = second\nlastKeyWithSpaceyEquals = " + EXPECTED_RESULT) 54 | ); 55 | } 56 | 57 | @ParameterizedTest 58 | @MethodSource("keysFoundProvider") 59 | void findKey(String keyToFind, String configurationContents) { 60 | assertEquals(EXPECTED_RESULT, find(configurationContents, keyToFind), "Did not find expected value for " + keyToFind); 61 | } 62 | } --------------------------------------------------------------------------------