From cd3fe4940aae0c2e1b34115baebbc36689fd8b14 Mon Sep 17 00:00:00 2001 From: Menghua1 Date: Thu, 7 Aug 2025 11:11:13 +0800 Subject: [PATCH] Add JSON escape to variables before syncing to remote Add JSON escape to variables before syncing to remote Add JSON escape to variables before syncing to remote Add JSON escape to variables before syncing to remote Add JSON escape to variables before syncing to remote --- cli/azd/pkg/azdo/pipeline.go | 18 ++++++++++++++++++ cli/azd/pkg/pipeline/github_provider.go | 15 +++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/cli/azd/pkg/azdo/pipeline.go b/cli/azd/pkg/azdo/pipeline.go index 65ff6cff449..687fd487155 100644 --- a/cli/azd/pkg/azdo/pipeline.go +++ b/cli/azd/pkg/azdo/pipeline.go @@ -5,6 +5,7 @@ package azdo import ( "context" + "encoding/json" "fmt" "strings" @@ -116,6 +117,7 @@ func CreatePipeline( if err != nil { return nil, err } + EscapeBuildDefinitionVariables(buildDefinitionVariables) definition.Variables = buildDefinitionVariables definition, err := client.UpdateDefinition(ctx, build.UpdateDefinitionArgs{ Definition: definition, @@ -140,6 +142,7 @@ func CreatePipeline( return nil, err } + EscapeBuildDefinitionVariables(createDefinitionArgs.Definition.Variables) newBuildDefinition, err := client.CreateDefinition(ctx, *createDefinitionArgs) if err != nil { return nil, err @@ -148,6 +151,21 @@ func CreatePipeline( return newBuildDefinition, nil } +func EscapeBuildDefinitionVariables(vars *map[string]build.BuildDefinitionVariable) { + for key, variable := range *vars { + if variable.Value != nil { + original := *variable.Value + b, _ := json.Marshal(original) + s := string(b) + if len(s) >= 2 && s[0] == '"' && s[len(s)-1] == '"' { + s = s[1 : len(s)-1] + } + variable.Value = &s + (*vars)[key] = variable + } + } +} + func getDefinitionVariables( env *environment.Environment, credentials *entraid.AzureCredentials, diff --git a/cli/azd/pkg/pipeline/github_provider.go b/cli/azd/pkg/pipeline/github_provider.go index b441a39cee9..b02580620d3 100644 --- a/cli/azd/pkg/pipeline/github_provider.go +++ b/cli/azd/pkg/pipeline/github_provider.go @@ -979,6 +979,8 @@ func (p *GitHubCiProvider) configurePipeline( } } + JsonEscapeVariables(toBeSetSecrets, toBeSetVariables) + // set the new variables and secrets for key, value := range toBeSetSecrets { if err := p.ghCli.SetSecret(ctx, repoSlug, key, value); err != nil { @@ -999,6 +1001,19 @@ func (p *GitHubCiProvider) configurePipeline( }, nil } +func JsonEscapeVariables(vars ...map[string]string) { + for _, m := range vars { + for key, value := range m { + b, _ := json.Marshal(value) + s := string(b) + if len(s) >= 2 && s[0] == '"' && s[len(s)-1] == '"' { + s = s[1 : len(s)-1] + } + m[key] = s + } + } +} + // workflow is the implementation for a CiPipeline for GitHub type workflow struct { repoDetails *gitRepositoryDetails