1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00

feat(terraformExecute): workspace selection (#3233)

Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com>
This commit is contained in:
Christian Volk 2021-11-04 13:03:45 +01:00 committed by GitHub
parent cd6e93acba
commit 874a314bf7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 0 deletions

View File

@ -46,6 +46,10 @@ func runTerraformExecute(config *terraformExecuteOptions, telemetryData *telemet
utils.AppendEnv([]string{fmt.Sprintf("TF_CLI_CONFIG_FILE=%s", config.CliConfigFile)})
}
if len(config.Workspace) > 0 {
utils.AppendEnv([]string{fmt.Sprintf("TF_WORKSPACE=%s", config.Workspace)})
}
args := []string{}
if config.Command == "apply" {

View File

@ -24,6 +24,7 @@ type terraformExecuteOptions struct {
AdditionalArgs []string `json:"additionalArgs,omitempty"`
Init bool `json:"init,omitempty"`
CliConfigFile string `json:"cliConfigFile,omitempty"`
Workspace string `json:"workspace,omitempty"`
}
type terraformExecuteCommonPipelineEnvironment struct {
@ -147,6 +148,7 @@ func addTerraformExecuteFlags(cmd *cobra.Command, stepConfig *terraformExecuteOp
cmd.Flags().StringSliceVar(&stepConfig.AdditionalArgs, "additionalArgs", []string{}, "")
cmd.Flags().BoolVar(&stepConfig.Init, "init", false, "")
cmd.Flags().StringVar(&stepConfig.CliConfigFile, "cliConfigFile", os.Getenv("PIPER_cliConfigFile"), "Path to the terraform CLI configuration file (https://www.terraform.io/docs/cli/config/config-file.html#credentials).")
cmd.Flags().StringVar(&stepConfig.Workspace, "workspace", os.Getenv("PIPER_workspace"), "")
}
@ -235,6 +237,15 @@ func terraformExecuteMetadata() config.StepData {
Aliases: []config.Alias{},
Default: os.Getenv("PIPER_cliConfigFile"),
},
{
Name: "workspace",
ResourceRef: []config.ResourceReference{},
Scope: []string{"PARAMETERS", "STAGES", "STEPS"},
Type: "string",
Mandatory: false,
Aliases: []config.Alias{},
Default: os.Getenv("PIPER_workspace"),
},
},
},
Containers: []config.Container{

View File

@ -91,6 +91,12 @@ func TestRunTerraformExecute(t *testing.T) {
CliConfigFile: ".pipeline/.terraformrc",
}, []string{"apply", "-auto-approve"}, []string{"TF_CLI_CONFIG_FILE=.pipeline/.terraformrc"},
},
{
terraformExecuteOptions{
Command: "plan",
Workspace: "any-workspace",
}, []string{"plan"}, []string{"TF_WORKSPACE=any-workspace"},
},
}
for i, test := range tt {

View File

@ -61,6 +61,13 @@ spec:
- type: vaultSecretFile
name: cliConfigFileVaultSecretName
default: terraform
- name: workspace
type: string
descriptions: Name of the terraform workspace to operate on.
scope:
- PARAMETERS
- STAGES
- STEPS
containers:
- name: terraform
image: hashicorp/terraform:1.0.10