Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a default template path for when we can't find a pipeline_template #195

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified images/config-driven-pipeline-project-recognizer.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,20 @@
public class ConfigDrivenWorkflowBranchProjectFactory extends AbstractWorkflowBranchProjectFactory {
// TODO: Make this a parameter that users can adjust to their liking
public static final String USER_DEFINITION_PATH = ".yourconfig.yml";
public static final String USER_DEFINITION_PIPELINE_PATH = "Jenkinsfile";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Depending on whether we go with the name change for the field, it might be good to name this closer to what it is. 😄

Suggested change
public static final String USER_DEFINITION_PIPELINE_PATH = "Jenkinsfile";
public static final String DEFAULT_PIPELINE_TEMPLATE_PATH = "Jenkinsfile";

public static final String PIPELINE_TEMPLATE = "pipeline_template";

private String scriptPath = USER_DEFINITION_PATH;
private String pipelinePath = USER_DEFINITION_PIPELINE_PATH;
private SCM jenkinsFileScm = null;

public Object readResolve() {
if (this.scriptPath == null) {
this.scriptPath = USER_DEFINITION_PATH;
}
if (this.pipelinePath == null) {
this.pipelinePath = USER_DEFINITION_PIPELINE_PATH;
}
return this;
}

Expand All @@ -42,8 +47,18 @@ public void setScriptPath(String scriptPath) {
}
}

@DataBoundSetter
public void setPipelinePath(String pipelinePath) {
if (StringUtils.isEmpty(pipelinePath)) {
this.pipelinePath = USER_DEFINITION_PIPELINE_PATH;
} else {
this.pipelinePath = pipelinePath;
}
}

public String getScriptPath() { return scriptPath; }

public String getPipelinePath() { return pipelinePath; }

public SCM getJenkinsFileScm() {
return jenkinsFileScm;
Expand All @@ -60,7 +75,7 @@ public ConfigDrivenWorkflowBranchProjectFactory() {}
@Override protected FlowDefinition createDefinition() {
// This creates the CpsScmFlowDefinition... create a new type of "binder"???
// We need a non-hardcoded version of this class... it does almost everything we want already...
return new ConfigFileSCMBinder(scriptPath, jenkinsFileScm);
return new ConfigFileSCMBinder(scriptPath, pipelinePath, jenkinsFileScm);
}

@Override protected SCMSourceCriteria getSCMSourceCriteria(SCMSource source) {
Expand Down Expand Up @@ -109,4 +124,4 @@ public Collection<? extends SCMDescriptor<?>> getApplicableDescriptors() {
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,26 @@
import java.util.Collection;

import static org.jenkinsci.plugins.workflow.multibranch.template.ConfigDrivenWorkflowBranchProjectFactory.USER_DEFINITION_PATH;
import static org.jenkinsci.plugins.workflow.multibranch.template.ConfigDrivenWorkflowBranchProjectFactory.USER_DEFINITION_PIPELINE_PATH;


/**
* Defines organization folders by {@link WorkflowBranchProjectFactory}.
*/
public class ConfigDrivenWorkflowMultiBranchProjectFactory extends AbstractWorkflowMultiBranchProjectFactory {

private String scriptPath = USER_DEFINITION_PATH;
private String pipelinePath = USER_DEFINITION_PIPELINE_PATH;

private SCM jenkinsFileScm = null;

public Object readResolve() {
if (this.scriptPath == null) {
this.scriptPath = USER_DEFINITION_PATH;
}
if (this.pipelinePath == null) {
this.pipelinePath = USER_DEFINITION_PIPELINE_PATH;
}
return this;
}

Expand All @@ -43,8 +50,18 @@ public void setScriptPath(String scriptPath) {
}
}

@DataBoundSetter
public void setpipelinePath(String pipelinePath) {
if (StringUtils.isEmpty(pipelinePath)) {
this.pipelinePath = USER_DEFINITION_PIPELINE_PATH;
} else {
this.pipelinePath = pipelinePath;
}
}

public String getScriptPath() { return scriptPath; }

public String getPipelinePath() { return pipelinePath; }

public SCM getJenkinsFileScm() {
return jenkinsFileScm;
Expand All @@ -65,6 +82,7 @@ public ConfigDrivenWorkflowMultiBranchProjectFactory() {}
private ConfigDrivenWorkflowBranchProjectFactory newProjectFactory() {
ConfigDrivenWorkflowBranchProjectFactory workflowBranchProjectFactory = new ConfigDrivenWorkflowBranchProjectFactory();
workflowBranchProjectFactory.setScriptPath(scriptPath);
workflowBranchProjectFactory.setPipelinePath(pipelinePath);
workflowBranchProjectFactory.setJenkinsFileScm(jenkinsFileScm);
return workflowBranchProjectFactory;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,24 +56,30 @@

import static org.jenkinsci.plugins.workflow.multibranch.template.ConfigDrivenWorkflowBranchProjectFactory.PIPELINE_TEMPLATE;
import static org.jenkinsci.plugins.workflow.multibranch.template.ConfigDrivenWorkflowBranchProjectFactory.USER_DEFINITION_PATH;
import static org.jenkinsci.plugins.workflow.multibranch.template.ConfigDrivenWorkflowBranchProjectFactory.USER_DEFINITION_PIPELINE_PATH;

/**
* Checks out the desired version of {@link ConfigDrivenWorkflowBranchProjectFactory#USER_DEFINITION_PATH}.
*/
class ConfigFileSCMBinder extends FlowDefinition {

private String scriptPath;
private String pipelinePath;
private SCM jenkinsFileScm;

public Object readResolve() {
if (this.scriptPath == null) {
this.scriptPath = USER_DEFINITION_PATH;
}
if (this.pipelinePath == null) {
this.pipelinePath = USER_DEFINITION_PIPELINE_PATH;
}
return this;
}

@DataBoundConstructor public ConfigFileSCMBinder(String scriptPath, SCM jenkinsFileScm) {
@DataBoundConstructor public ConfigFileSCMBinder(String scriptPath, String pipelinePath, SCM jenkinsFileScm) {
this.scriptPath = scriptPath;
this.pipelinePath = pipelinePath;
this.jenkinsFileScm = jenkinsFileScm;
}

Expand Down Expand Up @@ -163,27 +169,30 @@ public Object readResolve() {
}
}
}

String jenkinsfilePathString;

if (configContents == null) {
String pipelineTemplateNotFound =
String.format("Could not find a value for %s in %s", PIPELINE_TEMPLATE, scriptPath);
throw new AbortException(pipelineTemplateNotFound);
jenkinsfilePathString = pipelinePath;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if adding a log entry here might be useful and perhaps providing a different AbortException at https://github.com/jenkinsci/config-driven-pipeline-plugin/pull/195/files#diff-cb4b4fb0cb84e1621bd3517151cb5d3308b9555208d5aed521263075d12603cbR195

What I'm thinking is that it might be hard to tell which of these code branches the plugin has taken and whether I have a misconfiguration in the plugin or if my repo simply isn't correct. Basically, seems like it'd be nice to help the user and admin understand.

Perhaps we could log whether the Config File was found, whether pipeline_template was found, and/or if we're taking the Default Pipeline Template Path

} else {
String jenkinsfilePathString =
jenkinsfilePathString =
ConfigurationValueFinder.findFirstConfigurationValue(configContents,
ConfigDrivenWorkflowBranchProjectFactory.PIPELINE_TEMPLATE);

build.addAction(new ConfigFileEnvironmentContributingAction(configContents));

try (SCMFileSystem scriptFileSystem = SCMFileSystem.of(job, jenkinsFileScm)) {
if (scriptFileSystem != null) {
script = scriptFileSystem.child(jenkinsfilePathString).contentAsString();
listener.getLogger().println("Obtained " + jenkinsfilePathString);
ConfigDrivenWorkflowBranchProjectFactory.PIPELINE_TEMPLATE);
if(jenkinsfilePathString == null) {
jenkinsfilePathString = pipelinePath;
}
}
build.addAction(new ConfigFileEnvironmentContributingAction(configContents));

}
try (SCMFileSystem scriptFileSystem = SCMFileSystem.of(job, jenkinsFileScm)) {
if (scriptFileSystem != null) {
script = scriptFileSystem.child(jenkinsfilePathString).contentAsString();
listener.getLogger().println("Obtained " + jenkinsfilePathString);

} catch (FileNotFoundException exception) {
throw new AbortException(String.format("Could not find file %s", jenkinsfilePathString));
}

} catch (FileNotFoundException exception) {
throw new AbortException(String.format("Could not find file %s", jenkinsfilePathString));
}

if (script != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,9 @@ THE SOFTWARE.
<f:entry title="${%Config File Path}" field="scriptPath">
<f:textbox default=".yourconfig.yml"/>
</f:entry>
<f:entry title="${%Pipeline Template Path}" field="pipelinePath">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if Default Pipeline Template Path might be a better descriptor title? Perhaps renaming the field to defaultPipelinePath if that makes sense?

<f:textbox default="Jenkinsfile"/>
</f:entry>

<f:dropdownDescriptorSelector field="jenkinsFileScm" title="Centralized Jenkinsfile SCM" descriptors="${descriptor.applicableDescriptors}"/>
</j:jelly>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
Relative default location of pipeline_template.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be good to describe the example. It doesn't have to be this suggestion but maybe something like this:

Suggested change
Relative default location of pipeline_template.
Relative default location of <code>pipeline_template</code>. This is useful when you have some repositories in an organization where you do not want to customize any values for the template or you cannot add a Config File to the repository.

</div>
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,9 @@ THE SOFTWARE.
<f:entry title="${%Config File Path}" field="scriptPath">
<f:textbox default=".yourconfig.yml"/>
</f:entry>
<f:entry title="${%Pipeline Template Path}" field="pipelinePath">
<f:textbox default="Jenkinsfile"/>
</f:entry>

<f:dropdownDescriptorSelector field="jenkinsFileScm" title="Centralized Jenkinsfile SCM" descriptors="${descriptor.applicableDescriptors}"/>
</j:jelly>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
Relative default location of pipeline_template.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same suggestion as above 😄

Suggested change
Relative default location of pipeline_template.
Relative default location of <code>pipeline_template</code>. This is useful when you have some repositories in an organization where you do not want to customize any values for the template or you cannot add a Config File to the repository.

</div>
Loading