-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #273 from kmanning/issue_272
Issue #272: refactor parameters into a plugin
- Loading branch information
Showing
16 changed files
with
774 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
## [BuildWithParametersPlugin](../src/BuildWithParametersPlugin.groovy) | ||
|
||
This plugin is enabled by default. | ||
|
||
Pipelines can prompt the user for parameters at build-time, with the Jenkinsfile "Build With Parameters" feature. This plugin lets you enable this feature, and prompt the user for parameters. If no parameters are configured, this plugin does nothing. | ||
|
||
Eg: | ||
|
||
``` | ||
@Library(['terraform-pipeline@v5.1']) _ | ||
Jenkinsfile.init(this) | ||
BuildWithParametersPlugin.withBooleanParameter([ | ||
name: 'PIPELINE_PREFERENCE', | ||
description: 'Do you like pipelines?', | ||
defaultValue: true | ||
]) | ||
BuildWithParametersPlugin.withStringParameter([ | ||
name: 'PIPELINE_THOUGHTS', | ||
description: 'What do you think about pipelines?', | ||
defaultValue: 'They make deployments so easy' | ||
]) | ||
def validate = new TerraformValidateStage() | ||
def deployQa = new TerraformEnvironmentStage('qa') | ||
def deployUat = new TerraformEnvironmentStage('uat') | ||
def deployProd = new TerraformEnvironmentStage('prod') | ||
// At the start of the pipeline, the user with a checkbox and a string input | ||
// The user's responses are available in the environment variables | ||
// PIPELINE_PREFERENCE and PIPELINE_THOUGHTS | ||
validate.then(deployQa) | ||
.then(deployUat) | ||
.then(deployProd) | ||
.build() | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
public class BuildWithParametersPlugin implements BuildStagePlugin, | ||
TerraformValidateStagePlugin, | ||
TerraformEnvironmentStagePlugin, | ||
RegressionStagePlugin { | ||
|
||
private static globalBuildParameters = [] | ||
private static appliedOnce = false | ||
|
||
public static void init() { | ||
def plugin = new BuildWithParametersPlugin() | ||
|
||
BuildStage.addPlugin(plugin) | ||
TerraformValidateStage.addPlugin(plugin) | ||
TerraformEnvironmentStage.addPlugin(plugin) | ||
RegressionStage.addPlugin(plugin) | ||
} | ||
|
||
@Override | ||
public void apply(BuildStage stage) { | ||
applyToAllStages(stage) | ||
} | ||
|
||
@Override | ||
public void apply(TerraformValidateStage stage) { | ||
applyToAllStages(stage) | ||
} | ||
|
||
@Override | ||
public void apply(TerraformEnvironmentStage stage) { | ||
applyToAllStages(stage) | ||
} | ||
|
||
@Override | ||
public void apply(RegressionStage stage) { | ||
applyToAllStages(stage) | ||
} | ||
|
||
private void applyToAllStages(DecoratableStage stage) { | ||
stage.decorate(addParameterToFirstStageOnly()) | ||
} | ||
|
||
public Closure addParameterToFirstStageOnly() { | ||
return { innerClosure -> | ||
if (hasParameters() && !appliedOnce) { | ||
properties([ | ||
parameters(getBuildParameters()) | ||
]) | ||
appliedOnce = true | ||
} | ||
|
||
innerClosure() | ||
} | ||
} | ||
|
||
public static withBooleanParameter(Map options) { | ||
def optionDefaults = [ | ||
defaultValue: false, | ||
$class: 'hudson.model.BooleanParameterDefinition' | ||
] | ||
|
||
globalBuildParameters << (optionDefaults + options) | ||
} | ||
|
||
public static withStringParameter(Map options) { | ||
def optionDefaults = [ | ||
defaultValue: '', | ||
$class: 'hudson.model.StringParameterDefinition' | ||
] | ||
|
||
globalBuildParameters << (optionDefaults + options) | ||
} | ||
|
||
public static withParameter(Map options) { | ||
globalBuildParameters << options | ||
} | ||
|
||
public boolean hasParameters() { | ||
return !globalBuildParameters.isEmpty() | ||
} | ||
|
||
public List getBuildParameters() { | ||
return globalBuildParameters | ||
} | ||
|
||
public static reset() { | ||
globalBuildParameters = [] | ||
appliedOnce = false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// This is temporary, and should be pushed into Stage interface. See Issue #152 | ||
interface DecoratableStage { | ||
public void decorate(Closure closure) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.