GitLabgitlab-org

cli

main
ply8 git calls229 ms101 KiB5 cachedprotocol v2
321 lines9.9 KiBRaw
1package run
3import (
4 "context"
5 "encoding/json"
6 "fmt"
7 "os"
8 "strings"
10 "github.com/MakeNowJust/heredoc/v2"
11 "github.com/spf13/cobra"
13 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
15 "gitlab.com/gitlab-org/cli/internal/cmdutils"
16 "gitlab.com/gitlab-org/cli/internal/commands/ci/ciutils"
17 "gitlab.com/gitlab-org/cli/internal/commands/mr/mrutils"
18 "gitlab.com/gitlab-org/cli/internal/glrepo"
19 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
20 "gitlab.com/gitlab-org/cli/internal/utils"
23func parseVarArg(s string) (*gitlab.PipelineVariableOptions, error) {
24 // From https://pkg.go.dev/strings#Split:
25 //
26 // > If s does not contain sep and sep is not empty,
27 // > Split returns a slice of length 1 whose only element is s.
28 //
29 // Therefore, the function will always return a slice of min length 1.
30 v := strings.SplitN(s, ":", 2)
31 if len(v) == 1 {
32 return nil, fmt.Errorf("invalid argument structure")
33 }
34 key := strings.TrimSpace(v[0])
35 if key == "" {
36 return nil, fmt.Errorf("variable key cannot be empty")
37 }
38 return &gitlab.PipelineVariableOptions{
39 Key: &key,
40 Value: &v[1],
41 }, nil
44func extractEnvVar(s string) (*gitlab.PipelineVariableOptions, error) {
45 pvar, err := parseVarArg(s)
46 if err != nil {
47 return nil, err
48 }
49 pvar.VariableType = new(gitlab.EnvVariableType)
50 return pvar, nil
53func extractFileVar(s string) (*gitlab.PipelineVariableOptions, error) {
54 pvar, err := parseVarArg(s)
55 if err != nil {
56 return nil, err
57 }
58 b, err := os.ReadFile(*pvar.Value)
59 if err != nil {
60 return nil, err
61 }
62 pvar.VariableType = new(gitlab.FileVariableType)
63 pvar.Value = new(string(b))
64 return pvar, nil
67type PipelineData struct {
68 ID int64 `json:"id"`
69 Status string `json:"status"`
70 Ref string `json:"ref"`
71 WebURL string `json:"web_url"`
74func createPipeline(cmd *cobra.Command, c *gitlab.CreatePipelineOptions, f cmdutils.Factory, apiClient *gitlab.Client, repo glrepo.Interface, mr bool) (*PipelineData, error) {
75 branch, err := resolveBranch(cmd, f)
76 if err != nil {
77 return nil, err
78 }
79 if mr {
80 pipe, err := createMrPipeline(cmd.Context(), branch, f, apiClient, repo)
81 if err != nil {
82 return nil, fmt.Errorf("could not create mr pipeline for branch %s: %w", branch, err)
83 }
84 return &PipelineData{
85 ID: pipe.ID,
86 Status: pipe.Status,
87 Ref: pipe.Ref,
88 WebURL: pipe.WebURL,
89 }, nil
90 }
91 pipe, err := createBranchPipeline(branch, c, apiClient, repo)
92 if err != nil {
93 return nil, fmt.Errorf("could not create branch pipeline for branch %s: %w", branch, err)
94 }
95 return &PipelineData{
96 ID: pipe.ID,
97 Status: pipe.Status,
98 Ref: pipe.Ref,
99 WebURL: pipe.WebURL,
100 }, nil
103func createBranchPipeline(branch string, c *gitlab.CreatePipelineOptions, apiClient *gitlab.Client, repo glrepo.Interface) (*gitlab.Pipeline, error) {
104 c.Ref = new(branch)
105 pipe, _, err := apiClient.Pipelines.CreatePipeline(repo.FullName(), c)
106 return pipe, err
109func resolveBranch(cmd *cobra.Command, f cmdutils.Factory) (string, error) {
110 br, err := cmd.Flags().GetString("branch")
111 if err != nil {
112 return "", err
113 }
114 var branch string
115 if br != "" {
116 branch = br
117 } else if currentBranch, err := f.Branch(); err == nil {
118 branch = currentBranch
119 } else {
120 // `ci run` is running out of a git repo
121 f.IO().LogInfo("not in a Git repository. Using repository argument.")
122 client, err := f.GitLabClient()
123 if err != nil {
124 return "", err
125 }
126 repo, err := f.BaseRepo()
127 if err != nil {
128 return "", err
129 }
130 branch = ciutils.GetDefaultBranch(repo, client)
131 }
132 return branch, nil
135func createMrPipeline(ctx context.Context, branch string, f cmdutils.Factory, apiClient *gitlab.Client, repo glrepo.Interface) (*gitlab.PipelineInfo, error) {
136 mr, err := mrutils.GetMRForBranch(
137 ctx,
138 f.IO(),
139 apiClient,
140 mrutils.MrOptions{
141 BaseRepo: repo, Branch: branch, State: "opened", PromptEnabled: f.IO().PromptEnabled(),
142 },
143 )
144 if err != nil {
145 return nil, err
146 }
148 pipe, _, err := apiClient.MergeRequests.CreateMergeRequestPipeline(repo.FullName(), mr.IID)
149 if err != nil {
150 return nil, err
151 }
152 return pipe, nil
155func resolvePipelineVars(cmd *cobra.Command) ([]*gitlab.PipelineVariableOptions, error) {
156 pipelineVars := []*gitlab.PipelineVariableOptions{}
157 for _, flag := range []string{"variables-env", "variables"} {
158 if customPipelineVars, _ := cmd.Flags().GetStringSlice(flag); len(customPipelineVars) > 0 {
159 for _, v := range customPipelineVars {
160 pvar, err := extractEnvVar(v)
161 if err != nil {
162 return nil, fmt.Errorf("parsing pipeline variable. Expected format KEY:VALUE: %w", err)
163 }
164 pipelineVars = append(pipelineVars, pvar)
165 }
166 }
167 }
169 if customPipelineFileVars, _ := cmd.Flags().GetStringSlice("variables-file"); len(customPipelineFileVars) > 0 {
170 for _, v := range customPipelineFileVars {
171 pvar, err := extractFileVar(v)
172 if err != nil {
173 return nil, fmt.Errorf("parsing pipeline variable. Expected format KEY:FILENAME: %w", err)
174 }
175 pipelineVars = append(pipelineVars, pvar)
176 }
177 }
179 vf, err := cmd.Flags().GetString("variables-from")
180 if err != nil {
181 return nil, err
182 }
184 if vf != "" {
185 b, err := os.ReadFile(vf)
186 if err != nil {
187 return nil, fmt.Errorf("opening variable file: %s", vf)
188 }
189 var result []*gitlab.PipelineVariableOptions
190 err = json.Unmarshal(b, &result)
191 if err != nil {
192 return nil, fmt.Errorf("loading pipeline values: %w. Expected JSON array format: [{\"key\": \"VAR_NAME\", \"value\": \"VAR_VALUE\", \"variable_type\": \"env_var\"}]", err)
193 }
194 pipelineVars = append(pipelineVars, result...)
195 }
197 return pipelineVars, nil
200func NewCmdRun(f cmdutils.Factory) *cobra.Command {
201 openInBrowser := false
202 mr := false
204 pipelineRunCmd := &cobra.Command{
205 Use: "run [flags]",
206 Short: `Create a new CI/CD pipeline.`,
207 Aliases: []string{"create"},
208 Long: heredoc.Docf(`
209 Use %[1]s--branch%[1]s to specify a branch or reference. Defaults to the current branch.
211 The variable flags (%[1]s--variables%[1]s, %[1]s--variables-env%[1]s,
212 %[1]s--variables-file%[1]s, %[1]s--variables-from%[1]s) cannot be used with
213 %[1]s--mr%[1]s. If both are used, the command fails.
214 `, "`") + "\n" + cmdutils.PipelineInputsDescription,
215 Example: heredoc.Doc(`
216 glab ci run
217 glab ci run --variables \"key1:value,with,comma\"
218 glab ci run -b main
219 glab ci run --web
220 glab ci run --mr
222 # Specify CI variables
223 glab ci run -b main --variables-env key1:val1
224 glab ci run -b main --variables-env key1:val1,key2:val2
225 glab ci run -b main --variables-env key1:val1 --variables-env key2:val2
226 glab ci run -b main --variables-file MYKEY:file1 --variables KEY2:some_value
228 # Specify CI inputs
229 glab ci run -b main --input key1:val1 --input key2:val2
230 glab ci run -b main --input "replicas:int(3)" --input "debug:bool(false)" --input "regions:array(us-east,eu-west)"
232 # Load variables from JSON file
233 # Create variables.json with this format:
234 # [
235 # {
236 # "key": "CI_PIPELINE_SOURCE",
237 # "value": "web",
238 # "variable_type": "env_var"
239 # },
240 # {
241 # "key": "DEPLOY_ENV",
242 # "value": "production"
243 # }
244 # ]
245 glab ci run -b main --variables-from variables.json`),
246 Args: cobra.NoArgs,
247 ValidArgsFunction: cobra.NoFileCompletions,
248 Annotations: map[string]string{
249 mcpannotations.Destructive: "true",
250 },
251 RunE: func(cmd *cobra.Command, args []string) error {
252 var err error
254 client, err := f.GitLabClient()
255 if err != nil {
256 return err
257 }
259 repo, err := f.BaseRepo()
260 if err != nil {
261 return err
262 }
264 pipelineVars, err := resolvePipelineVars(cmd)
265 if err != nil {
266 return err
267 }
269 pipelineInputs, err := cmdutils.PipelineInputsFromFlags(cmd)
270 if err != nil {
271 return err
272 }
274 c := &gitlab.CreatePipelineOptions{
275 Inputs: pipelineInputs,
276 }
278 if len(pipelineVars) != 0 {
279 c.Variables = new(pipelineVars)
280 }
282 pipe, err := createPipeline(cmd, c, f, client, repo, mr)
283 if err != nil {
284 return err
285 }
286 if openInBrowser { // open in browser if --web flag is specified
287 webURL := pipe.WebURL
289 if f.IO().IsOutputTTY() {
290 f.IO().LogErrorf("Opening %s in your browser.\n", utils.DisplayURL(webURL))
291 }
293 cfg := f.Config()
295 browser, _ := cfg.Get(repo.RepoHost(), "browser")
296 return utils.OpenInBrowser(webURL, browser)
297 }
299 output := fmt.Sprintf("Created pipeline (id: %d), status: %s, ref: %s, weburl: %s", pipe.ID, pipe.Status, pipe.Ref, pipe.WebURL)
300 f.IO().LogInfo(output)
301 return nil
302 },
303 }
304 pipelineRunCmd.Flags().StringP("branch", "b", "", "Create pipeline on branch or reference <string>.")
305 pipelineRunCmd.Flags().StringSliceP("variables", "", []string{}, "Pass variables to the pipeline in the format <key>:<value>. Cannot be used for MR pipelines.")
306 pipelineRunCmd.Flags().StringSliceP("variables-env", "", []string{}, "Pass variables to the pipeline in the format <key>:<value>. Cannot be used for MR pipelines.")
307 pipelineRunCmd.Flags().StringSliceP("variables-file", "", []string{}, "Pass file contents as a file variable to the pipeline in the format <key>:<filename>. Cannot be used for MR pipelines.")
308 pipelineRunCmd.Flags().StringP("variables-from", "f", "", "JSON file with variables for pipeline execution. Expects array of hashes, each with at least 'key' and 'value'. Cannot be used for MR pipelines.")
309 pipelineRunCmd.Flags().BoolVarP(&openInBrowser, "web", "w", false, "Open pipeline in a browser. Uses default browser, or browser specified in BROWSER environment variable.")
310 pipelineRunCmd.Flags().BoolVar(&mr, "mr", false, "Run merge request pipeline instead of branch pipeline.")
311 cmdutils.AddPipelineInputsFlag(pipelineRunCmd)
313 for _, flag := range []string{"variables", "variables-env", "variables-file", "variables-from", "input"} {
314 // https://docs.gitlab.com/api/merge_requests/#create-merge-request-pipeline
315 // MR pipeline creation API does not accept variables unlike "normal" pipelines
316 // https://docs.gitlab.com/api/pipelines/#create-a-new-pipeline
317 pipelineRunCmd.MarkFlagsMutuallyExclusive("mr", flag)
318 }
320 return pipelineRunCmd
Diagnostics8 git calls · 229 ms
CallStatusSizeTime0229 ms
discover200185 B2 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=185
git.server.agent=git/2.56.g1891de4-Linux
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch server-option object-format
git.server.fetch=shallow wait-for-done filter
git.protocol.version=2
git.pkt.frames=9
git.pkt.data=7
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=185
git.pkt.payload.max=35
ls-refs20058 KiB2 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=59849
git.response.cache=UPDATING
git.response.duration_ms=2
git.protocol.version=2
git.pkt.frames=731
git.pkt.data=730
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=59849
git.pkt.payload.max=143
git.ls_refs.count=730
git.ls_refs.head=refs/heads/main
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=4273
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=combine:blob:none+tree:2
git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9
git.fetch.haves.count=0
git.fetch.deepen=1
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=7
git.pkt.data=5
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=4273
git.pkt.payload.max=4173
git.sideband.band1.bytes=4173
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=11
git.pack.bytes=4173
git.fetch.wide.objects=11
git.fetch.wide.extra_bytes=3930
git.fetch.wide.blobs=0
fetchtree:0 · deepen 1020032 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=32926
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=22
git.pkt.data=20
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=32926
git.pkt.payload.max=32046
git.sideband.band1.bytes=32046
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=80
git.pack.bytes=32046
git.pack.ofs_delta=36
git.pack.ref_delta=0
git.pack.chain.max=2
git.pack.trailer=cb0bad5d7ffc514895d8b21c4dd470327f90bc91
git.pack.full=44
Open pack map
fetchtree:02001.8 KiB · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=1857
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=dce174ff8c632a19d17eea99c8a9b7c37fb2b592
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=1857
git.pkt.payload.max=1830
git.sideband.band1.bytes=1830
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=1830
git.pack.trailer=fb03183b7909876778f740d196d7516811003517
Open pack map
fetchtree:0200635 B · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=635
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=517b073a0d5c32bc4e93f86de74b31d60ff8cf03
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=635
git.pkt.payload.max=608
git.sideband.band1.bytes=608
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=608
git.pack.trailer=178abb9456d874b7fd38fa21ad5cf9ca3ebcccaa
Open pack map
fetchtree:0200135 B · hit4 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=4
git.response.bytes=135
git.response.cache=HIT
git.response.duration_ms=4
git.fetch.filter=tree:0
git.fetch.wants=01689565d0aef6a9088d9b739904c4d5fe1b7fbb
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=135
git.pkt.payload.max=108
git.sideband.band1.bytes=108
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=108
git.pack.trailer=7033f661a84564110bda7edbcf34cb3243bcf14e
Open pack map
fetch2003.1 KiB216 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=216
git.response.bytes=3210
git.response.cache=MISS
git.response.duration_ms=216
git.fetch.wants=83ac4376265207a887d4d7ca0953e4af9ad5f083
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=3210
git.pkt.payload.max=3183
git.sideband.band1.bytes=3183
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://gitlab.com/gitlab-org/cli
Ref
refs/heads/main
Commit
0dd7574dce9e385c5c30540e6c15657adc6bdce9
cf-ray
a5e2cb6a7a267929
Request
569f72f3…
Protocol
v2 · sha1
Agent
git/2.56.g1891de4-Linux
Filter
advertised
Fetch
shallow wait-for-done filter
Capabilities
agent ls-refs fetch server-option object-format