Skip to content

Commit

Permalink
pipeline additional vars secrs (#3155)
Browse files Browse the repository at this point in the history
  • Loading branch information
vhvb1989 authored Feb 8, 2024
1 parent b7dc243 commit ca4231f
Show file tree
Hide file tree
Showing 7 changed files with 243 additions and 93 deletions.
38 changes: 27 additions & 11 deletions cli/azd/pkg/infra/provisioning/bicep/bicep_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ type BicepProvider struct {
alphaFeatureManager *alpha.FeatureManager
clock clock.Clock
ignoreDeploymentState bool
// compileBicepResult is cached to avoid recompiling the same bicep file multiple times in the same azd run.
compileBicepMemoryCache *compileBicepResult
// prevent resolving parameters multiple times in the same azd run.
ensureParamsInMemoryCache azure.ArmParameters
}

var ErrResourceGroupScopeNotSupported = fmt.Errorf(
Expand Down Expand Up @@ -154,6 +158,10 @@ func (p *BicepProvider) EnsureEnv(ctx context.Context) error {
return nil
}

// prompt parameters during initialization and ignore any errors.
// This strategy takes advantage of the bicep compilation from initialization and allows prompting for required inputs
_, _ = p.ensureParameters(ctx, compileResult.Template)

scope, err := compileResult.Template.TargetScope()
if err != nil {
return err
Expand Down Expand Up @@ -367,12 +375,7 @@ func (p *BicepProvider) plan(ctx context.Context) (*deploymentDetails, error) {

// for .bicep, azd must load a parameters.json file and create the ArmParameters
if isBicepFile(modulePath) {
parameters, err := p.loadParameters(ctx)
if err != nil {
return nil, fmt.Errorf("resolving bicep parameters file: %w", err)
}

configuredParameters, err := p.ensureParameters(ctx, compileResult.Template, parameters)
configuredParameters, err := p.ensureParameters(ctx, compileResult.Template)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1674,6 +1677,10 @@ type compileBicepResult struct {
func (p *BicepProvider) compileBicep(
ctx context.Context, modulePath string,
) (*compileBicepResult, error) {
if p.compileBicepMemoryCache != nil {
return p.compileBicepMemoryCache, nil
}

var compiled string
var parameters azure.ArmParameters

Expand Down Expand Up @@ -1772,12 +1779,13 @@ func (p *BicepProvider) compileBicep(
}
}
}

return &compileBicepResult{
p.compileBicepMemoryCache = &compileBicepResult{
RawArmTemplate: rawTemplate,
Template: template,
Parameters: parameters,
}, nil
}

return p.compileBicepMemoryCache, nil
}

func combineMetadata(base map[string]json.RawMessage, override map[string]json.RawMessage) map[string]json.RawMessage {
Expand Down Expand Up @@ -1876,8 +1884,16 @@ func (p *BicepProvider) modulePath() string {
func (p *BicepProvider) ensureParameters(
ctx context.Context,
template azure.ArmTemplate,
parameters azure.ArmParameters,
) (azure.ArmParameters, error) {
if p.ensureParamsInMemoryCache != nil {
return maps.Clone(p.ensureParamsInMemoryCache), nil
}

parameters, err := p.loadParameters(ctx)
if err != nil {
return nil, fmt.Errorf("resolving bicep parameters file: %w", err)
}

if len(template.Parameters) == 0 {
return azure.ArmParameters{}, nil
}
Expand Down Expand Up @@ -1979,7 +1995,7 @@ func (p *BicepProvider) ensureParameters(
p.console.Message(ctx, fmt.Sprintf("warning: failed to save configured values: %v", err))
}
}

p.ensureParamsInMemoryCache = maps.Clone(configuredParameters)
return configuredParameters, nil
}

Expand Down
26 changes: 26 additions & 0 deletions cli/azd/pkg/pipeline/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ package pipeline

import (
"context"
"maps"
"os"
"path/filepath"
"slices"

"github.com/azure/azure-dev/cli/azd/pkg/graphsdk"
"github.com/azure/azure-dev/cli/azd/pkg/infra/provisioning"
Expand Down Expand Up @@ -118,6 +120,30 @@ type CiProvider interface {
) *CredentialOptions
}

type ConfigOptions struct {
Variables []string
Secrets []string
}

// SecretsAndVar returns the list of variables and secrets to be used in the pipeline
// The initial values reference azd known values, which are merged with the ones defined on azure.yaml by the user.
func (c *ConfigOptions) SecretsAndVars(
initialVariables, initialSecrets, env map[string]string) (variables map[string]string, secrets map[string]string) {
variables = maps.Clone(initialVariables)
secrets = maps.Clone(initialSecrets)

for key, value := range env {
if slices.Contains(c.Variables, key) {
variables[key] = value
}
if slices.Contains(c.Secrets, key) {
secrets[key] = value
}
}

return variables, secrets
}

func folderExists(folderPath string) bool {
if _, err := os.Stat(folderPath); err == nil {
return true
Expand Down
Loading

0 comments on commit ca4231f

Please sign in to comment.