Skip to content

feat(cli): support non-active template version pushes #7488

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jun 3, 2023
19 changes: 14 additions & 5 deletions cli/templatepush.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ func (r *RootCmd) templatePush() *clibase.Cmd {
alwaysPrompt bool
provisionerTags []string
uploadFlags templateUploadFlags
activate bool
)
client := new(codersdk.Client)
cmd := &clibase.Cmd{
Expand Down Expand Up @@ -174,11 +175,13 @@ func (r *RootCmd) templatePush() *clibase.Cmd {
return xerrors.Errorf("job failed: %s", job.Job.Status)
}

err = client.UpdateActiveTemplateVersion(inv.Context(), template.ID, codersdk.UpdateActiveTemplateVersion{
ID: job.ID,
})
if err != nil {
return err
if activate {
err = client.UpdateActiveTemplateVersion(inv.Context(), template.ID, codersdk.UpdateActiveTemplateVersion{
ID: job.ID,
})
if err != nil {
return err
}
}

_, _ = fmt.Fprintf(inv.Stdout, "Updated version at %s!\n", cliui.Styles.DateTimeStamp.Render(time.Now().Format(time.Stamp)))
Expand Down Expand Up @@ -233,6 +236,12 @@ func (r *RootCmd) templatePush() *clibase.Cmd {
Description: "Always prompt all parameters. Does not pull parameter values from active template version.",
Value: clibase.BoolOf(&alwaysPrompt),
},
{
Flag: "activate",
Description: "Whether the new template will be marked active.",
Default: "true",
Value: clibase.BoolOf(&activate),
},
cliui.SkipPromptOption(),
uploadFlags.option(),
}
Expand Down
46 changes: 46 additions & 0 deletions cli/templatepush_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,52 @@ func TestTemplatePush(t *testing.T) {
require.Equal(t, "example", templateVersions[1].Name)
})

t.Run("PushInactiveTemplateVersion", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
user := coderdtest.CreateFirstUser(t, client)
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
_ = coderdtest.AwaitTemplateVersionJob(t, client, version.ID)

template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)

// Test the cli command.
source := clitest.CreateTemplateVersionSource(t, &echo.Responses{
Parse: echo.ParseComplete,
ProvisionApply: echo.ProvisionComplete,
})
inv, root := clitest.New(t, "templates", "push", template.Name, "--make-active", "false", "--directory", source, "--test.provisioner", string(database.ProvisionerTypeEcho), "--name", "example")
clitest.SetupConfig(t, client, root)
pty := ptytest.New(t).Attach(inv)

execDone := make(chan error)
go func() {
execDone <- inv.Run()
}()

matches := []struct {
match string
write string
}{
{match: "Upload", write: "yes"},
}
for _, m := range matches {
pty.ExpectMatch(m.match)
pty.WriteLine(m.write)
}

require.NoError(t, <-execDone)

// Assert that the template version didn't change.
templateVersions, err := client.TemplateVersionsByTemplate(context.Background(), codersdk.TemplateVersionsByTemplateRequest{
TemplateID: template.ID,
})
require.NoError(t, err)
assert.Len(t, templateVersions, 2)
assert.Equal(t, template.ActiveVersionID, templateVersions[0].ID)
require.NotEqual(t, "example", templateVersions[0].Name)
})

t.Run("UseWorkingDir", func(t *testing.T) {
t.Parallel()

Expand Down
3 changes: 3 additions & 0 deletions cli/testdata/coder_templates_push_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ Usage: coder templates push [flags] [template]
Push a new template version from the current directory or as specified by flag

Options
--activate bool (default: true)
Whether the new template will be marked active.

--always-prompt bool
Always prompt all parameters. Does not pull parameter values from
active template version.
Expand Down
9 changes: 9 additions & 0 deletions docs/cli/templates_push.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ coder templates push [flags] [template]

## Options

### --activate

| | |
| ------- | ----------------- |
| Type | <code>bool</code> |
| Default | <code>true</code> |

Whether the new template will be marked active.

### --always-prompt

| | |
Expand Down