Skip to content

Commit e2613c9

Browse files
goodsparkammariomatifali
authored
feat(cli): support non-active template version pushes (#7488)
So that we can push template updates for testing without impacting normal users of the template. --------- Co-authored-by: Ammar Bandukwala <ammar@ammar.io> Co-authored-by: Muhammad Atif Ali <matifali@live.com> Co-authored-by: Atif Ali <atif@coder.com>
1 parent 1f8e817 commit e2613c9

File tree

4 files changed

+68
-5
lines changed

4 files changed

+68
-5
lines changed

cli/templatepush.go

+14-5
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ func (r *RootCmd) templatePush() *clibase.Cmd {
115115
alwaysPrompt bool
116116
provisionerTags []string
117117
uploadFlags templateUploadFlags
118+
activate bool
118119
)
119120
client := new(codersdk.Client)
120121
cmd := &clibase.Cmd{
@@ -172,11 +173,13 @@ func (r *RootCmd) templatePush() *clibase.Cmd {
172173
return xerrors.Errorf("job failed: %s", job.Job.Status)
173174
}
174175

175-
err = client.UpdateActiveTemplateVersion(inv.Context(), template.ID, codersdk.UpdateActiveTemplateVersion{
176-
ID: job.ID,
177-
})
178-
if err != nil {
179-
return err
176+
if activate {
177+
err = client.UpdateActiveTemplateVersion(inv.Context(), template.ID, codersdk.UpdateActiveTemplateVersion{
178+
ID: job.ID,
179+
})
180+
if err != nil {
181+
return err
182+
}
180183
}
181184

182185
_, _ = fmt.Fprintf(inv.Stdout, "Updated version at %s!\n", cliui.Styles.DateTimeStamp.Render(time.Now().Format(time.Stamp)))
@@ -226,6 +229,12 @@ func (r *RootCmd) templatePush() *clibase.Cmd {
226229
Description: "Always prompt all parameters. Does not pull parameter values from active template version.",
227230
Value: clibase.BoolOf(&alwaysPrompt),
228231
},
232+
{
233+
Flag: "activate",
234+
Description: "Whether the new template will be marked active.",
235+
Default: "true",
236+
Value: clibase.BoolOf(&activate),
237+
},
229238
cliui.SkipPromptOption(),
230239
uploadFlags.option(),
231240
}

cli/templatepush_test.go

+42
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,48 @@ func TestTemplatePush(t *testing.T) {
6969
require.Equal(t, "example", templateVersions[1].Name)
7070
})
7171

72+
t.Run("PushInactiveTemplateVersion", func(t *testing.T) {
73+
t.Parallel()
74+
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
75+
user := coderdtest.CreateFirstUser(t, client)
76+
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
77+
_ = coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
78+
79+
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
80+
81+
// Test the cli command.
82+
source := clitest.CreateTemplateVersionSource(t, &echo.Responses{
83+
Parse: echo.ParseComplete,
84+
ProvisionApply: echo.ProvisionComplete,
85+
})
86+
inv, root := clitest.New(t, "templates", "push", template.Name, "--activate=false", "--directory", source, "--test.provisioner", string(database.ProvisionerTypeEcho), "--name", "example")
87+
clitest.SetupConfig(t, client, root)
88+
pty := ptytest.New(t).Attach(inv)
89+
w := clitest.StartWithWaiter(t, inv)
90+
91+
matches := []struct {
92+
match string
93+
write string
94+
}{
95+
{match: "Upload", write: "yes"},
96+
}
97+
for _, m := range matches {
98+
pty.ExpectMatch(m.match)
99+
pty.WriteLine(m.write)
100+
}
101+
102+
w.RequireSuccess()
103+
104+
// Assert that the template version didn't change.
105+
templateVersions, err := client.TemplateVersionsByTemplate(context.Background(), codersdk.TemplateVersionsByTemplateRequest{
106+
TemplateID: template.ID,
107+
})
108+
require.NoError(t, err)
109+
assert.Len(t, templateVersions, 2)
110+
assert.Equal(t, template.ActiveVersionID, templateVersions[0].ID)
111+
require.NotEqual(t, "example", templateVersions[0].Name)
112+
})
113+
72114
t.Run("UseWorkingDir", func(t *testing.T) {
73115
t.Parallel()
74116

cli/testdata/coder_templates_push_--help.golden

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ Usage: coder templates push [flags] [template]
33
Push a new template version from the current directory or as specified by flag
44

55
Options
6+
--activate bool (default: true)
7+
Whether the new template will be marked active.
8+
69
--always-prompt bool
710
Always prompt all parameters. Does not pull parameter values from
811
active template version.

docs/cli/templates_push.md

+9
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ coder templates push [flags] [template]
1212

1313
## Options
1414

15+
### --activate
16+
17+
| | |
18+
| ------- | ----------------- |
19+
| Type | <code>bool</code> |
20+
| Default | <code>true</code> |
21+
22+
Whether the new template will be marked active.
23+
1524
### --always-prompt
1625

1726
| | |

0 commit comments

Comments
 (0)