/
pipeline.go
251 lines (227 loc) · 8.56 KB
/
pipeline.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package cmd
import (
"context"
"fmt"
"github.com/MakeNowJust/heredoc/v2"
"github.com/azure/azure-dev/cli/azd/cmd/actions"
"github.com/azure/azure-dev/cli/azd/internal"
"github.com/azure/azure-dev/cli/azd/pkg/auth"
"github.com/azure/azure-dev/cli/azd/pkg/environment"
"github.com/azure/azure-dev/cli/azd/pkg/infra/provisioning"
"github.com/azure/azure-dev/cli/azd/pkg/input"
"github.com/azure/azure-dev/cli/azd/pkg/output"
"github.com/azure/azure-dev/cli/azd/pkg/output/ux"
"github.com/azure/azure-dev/cli/azd/pkg/pipeline"
"github.com/azure/azure-dev/cli/azd/pkg/project"
"github.com/azure/azure-dev/cli/azd/pkg/prompt"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
type pipelineConfigFlags struct {
pipeline.PipelineManagerArgs
global *internal.GlobalCommandOptions
internal.EnvFlag
}
func (pc *pipelineConfigFlags) Bind(local *pflag.FlagSet, global *internal.GlobalCommandOptions) {
local.StringVar(
&pc.PipelineServicePrincipalId,
"principal-id",
"",
"The client id of the service principal to use to grant access to Azure resources as part of the pipeline.",
)
local.StringVar(
&pc.PipelineServicePrincipalName,
"principal-name",
"",
"The name of the service principal to use to grant access to Azure resources as part of the pipeline.",
)
local.StringVar(
&pc.PipelineRemoteName,
"remote-name",
"origin",
"The name of the git remote to configure the pipeline to run on.",
)
//nolint:lll
local.StringVar(
&pc.PipelineAuthTypeName,
"auth-type",
"",
"The authentication type used between the pipeline provider and Azure for deployment (Only valid for GitHub provider). Valid values: federated, client-credentials.",
)
//nolint:lll
local.StringArrayVar(
&pc.PipelineRoleNames,
"principal-role",
pipeline.DefaultRoleNames,
"The roles to assign to the service principal. By default the service principal will be granted the Contributor and User Access Administrator roles.",
)
// default provider is empty because it can be set from azure.yaml. By letting default here be empty, we know that
// there no customer input using --provider
local.StringVar(&pc.PipelineProvider, "provider", "",
"The pipeline provider to use (github for Github Actions and azdo for Azure Pipelines).")
pc.EnvFlag.Bind(local, global)
pc.global = global
}
func pipelineActions(root *actions.ActionDescriptor) *actions.ActionDescriptor {
group := root.Add("pipeline", &actions.ActionDescriptorOptions{
Command: &cobra.Command{
Use: "pipeline",
Short: fmt.Sprintf("Manage and configure your deployment pipelines. %s", output.WithWarningFormat("(Beta)")),
},
HelpOptions: actions.ActionHelpOptions{
Description: getCmdPipelineHelpDescription,
Footer: getCmdPipelineHelpFooter,
},
GroupingOptions: actions.CommandGroupOptions{
RootLevelHelp: actions.CmdGroupMonitor,
},
})
group.Add("config", &actions.ActionDescriptorOptions{
Command: newPipelineConfigCmd(),
FlagsResolver: newPipelineConfigFlags,
ActionResolver: newPipelineConfigAction,
HelpOptions: actions.ActionHelpOptions{
Description: getCmdPipelineConfigHelpDescription,
Footer: getCmdPipelineConfigHelpFooter,
},
})
return group
}
func newPipelineConfigFlags(cmd *cobra.Command, global *internal.GlobalCommandOptions) *pipelineConfigFlags {
flags := &pipelineConfigFlags{}
flags.Bind(cmd.Flags(), global)
return flags
}
func newPipelineConfigCmd() *cobra.Command {
return &cobra.Command{
Use: "config",
Short: fmt.Sprintf(
"Configure your deployment pipeline to connect securely to Azure. %s",
output.WithWarningFormat("(Beta)")),
}
}
// pipelineConfigAction defines the action for pipeline config command
type pipelineConfigAction struct {
flags *pipelineConfigFlags
manager *pipeline.PipelineManager
provisioningManager *provisioning.Manager
env *environment.Environment
console input.Console
prompters prompt.Prompter
projectConfig *project.ProjectConfig
importManager *project.ImportManager
}
func newPipelineConfigAction(
env *environment.Environment,
_ auth.LoggedInGuard,
console input.Console,
flags *pipelineConfigFlags,
prompters prompt.Prompter,
manager *pipeline.PipelineManager,
provisioningManager *provisioning.Manager,
importManager *project.ImportManager,
projectConfig *project.ProjectConfig,
) actions.Action {
pca := &pipelineConfigAction{
flags: flags,
manager: manager,
env: env,
console: console,
prompters: prompters,
provisioningManager: provisioningManager,
importManager: importManager,
projectConfig: projectConfig,
}
return pca
}
// Run implements action interface
func (p *pipelineConfigAction) Run(ctx context.Context) (*actions.ActionResult, error) {
infra, err := p.importManager.ProjectInfrastructure(ctx, p.projectConfig)
if err != nil {
return nil, err
}
defer func() { _ = infra.Cleanup() }()
err = p.provisioningManager.Initialize(ctx, p.projectConfig.Path, infra.Options)
if err != nil {
return nil, err
}
pipelineProviderName := p.manager.CiProviderName()
// Command title
p.console.MessageUxItem(ctx, &ux.MessageTitle{
Title: fmt.Sprintf("Configure your %s pipeline", pipelineProviderName),
})
pipelineResult, err := p.manager.Configure(ctx)
if err != nil {
return nil, err
}
return &actions.ActionResult{
Message: &actions.ResultMessage{
Header: fmt.Sprintf("Your %s pipeline has been configured!", pipelineProviderName),
FollowUp: heredoc.Docf(`
Link to view your new repo: %s
Link to view your pipeline status: %s`,
output.WithLinkFormat("%s", pipelineResult.RepositoryLink),
output.WithLinkFormat("%s", pipelineResult.PipelineLink)),
},
}, nil
}
func getCmdPipelineHelpDescription(*cobra.Command) string {
return generateCmdHelpDescription(
fmt.Sprintf("Manage integrating your application with deployment pipelines. %s", output.WithWarningFormat("(Beta)")),
[]string{
formatHelpNote(
"azd commands (e.g. " +
output.WithHighLightFormat("provision") + ", " +
output.WithHighLightFormat("deploy") + ") " +
"can be used within GitHub Actions and Azure Pipelines to test your code against real Azure resources " +
"and facilitate deployments."),
formatHelpNote(
"After creating a pipeline definition file, running " +
output.WithHighLightFormat("pipeline config") +
" will help configure your deployment pipeline to connect securely to Azure."),
formatHelpNote(fmt.Sprintf("For more information on how to use azd in your pipeline, go to: %s.",
output.WithLinkFormat("https://aka.ms/azure-dev/pipeline"))),
})
}
func getCmdPipelineHelpFooter(c *cobra.Command) string {
return generateCmdHelpSamplesBlock(map[string]string{
"Walk through the steps required " +
"to set up your deployment pipeline.": output.WithHighLightFormat("azd pipeline config"),
})
}
func getCmdPipelineConfigHelpDescription(*cobra.Command) string {
return generateCmdHelpDescription(
"Configure your deployment pipeline to connect securely to Azure",
[]string{
formatHelpNote(
"Supports GitHub Actions and Azure Pipelines. To configure using a specific pipeline provider, " +
"provide a value for the '--provider' flag."),
formatHelpNote(
output.WithHighLightFormat("pipeline config") +
" creates or uses a service principal on the Azure subscription to create a secure connection between" +
" your deployment pipeline and Azure."),
formatHelpNote("By default, " +
output.WithHighLightFormat("pipeline config") +
" will set deployment pipeline variables and secrets using the current environment. " +
"To configure for a new or an existing environment, provide a value for the '-e' flag."),
})
}
func getCmdPipelineConfigHelpFooter(c *cobra.Command) string {
return generateCmdHelpSamplesBlock(map[string]string{
"Configure a deployment pipeline using an existing service principal": fmt.Sprintf("%s %s",
output.WithHighLightFormat("azd pipeline config --principal-name"),
output.WithWarningFormat("[Principal name]"),
),
"Configure a deployment pipeline for 'app-test' environment": fmt.Sprintf("%s %s",
output.WithHighLightFormat("azd pipeline config -e"),
output.WithWarningFormat("app-test"),
),
"Configure a deployment pipeline for 'app-test' environment on Azure Pipelines.": fmt.Sprintf("%s %s %s",
output.WithHighLightFormat("azd pipeline config -e"),
output.WithWarningFormat("app-test"),
output.WithHighLightFormat("--provider azdo"),
),
})
}