Skip to content

Commit

Permalink
fix(cmd): add forceRecreate cmd arg
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarzhou-portainer committed Jul 24, 2024
1 parent e9d81ee commit f063fd5
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 10 deletions.
13 changes: 6 additions & 7 deletions deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@ func (cmd *DeployCommand) Run(cmdCtx *CommandExecutionContext) error {
Bool("skipTLSVerify", cmd.SkipTLSVerify).
Msg("Deploying Compose stack from Git repository")

defer dockerLogout(cmd.Registry)
err := dockerLogin(cmd.Registry)
if err != nil {
return err
if err := dockerLogin(cmd.Registry); err != nil {
return fmt.Errorf("an error occured in docker login. Error: %w", err)
}
defer dockerLogout(cmd.Registry)

if cmd.User != "" && cmd.Password != "" {
log.Info().
Expand Down Expand Up @@ -131,7 +130,7 @@ func (cmd *DeployCommand) Run(cmdCtx *CommandExecutionContext) error {
ProjectName: cmd.ProjectName,
Env: cmd.Env,
},
ForceRecreate: true,
ForceRecreate: cmd.ForceRecreateStack,
})

if err != nil {
Expand All @@ -152,11 +151,11 @@ func (cmd *SwarmDeployCommand) Run(cmdCtx *CommandExecutionContext) error {
Str("destination", cmd.Destination).
Msg("Deploying Swarm stack from a Git repository")

defer dockerLogout(cmd.Registry)
err := dockerLogin(cmd.Registry)
if err != nil {
return err
return fmt.Errorf("an error occured in swarm docker login. Error: %w", err)
}
defer dockerLogout(cmd.Registry)

if cmd.User != "" && cmd.Password != "" {
log.Info().
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ toolchain go1.22.5
require (
github.com/alecthomas/kong v0.6.1
github.com/go-git/go-git/v5 v5.11.0
github.com/portainer/portainer/pkg/libstack v0.0.0-20230626042119-89c1d0e33707
github.com/portainer/portainer/pkg/libstack v0.0.0-20230831224222-4560a53317b0
github.com/rs/zerolog v1.28.0
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/portainer/portainer/pkg/libstack v0.0.0-20230626042119-89c1d0e33707 h1:SmwWcOqHpbHj+4tZNR9AV4VfLWmbelkaAcWYuMkQ9G8=
github.com/portainer/portainer/pkg/libstack v0.0.0-20230626042119-89c1d0e33707/go.mod h1:+zCK2UbsH6A3yEGi0yZ45ec5VFRP7svob5Q2lW6LFgk=
github.com/portainer/portainer/pkg/libstack v0.0.0-20230831224222-4560a53317b0 h1:BsSUEHzLdwxQ32QS5qdFYkWT3e7SqCLfrdeW69s1Ku4=
github.com/portainer/portainer/pkg/libstack v0.0.0-20230831224222-4560a53317b0/go.mod h1:+zCK2UbsH6A3yEGi0yZ45ec5VFRP7svob5Q2lW6LFgk=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
Expand Down
11 changes: 9 additions & 2 deletions swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,16 @@ func deploySwarmStack(cmd SwarmDeployCommand, clonePath string) error {
} else {
args = append(args, "stack", "deploy", "--with-registry-auth")
}
if !cmd.Pull {
args = append(args, "--resolve-image=never")

resolveImageArgs := "--resolve-image=never"
if cmd.Pull {
resolveImageArgs = "--resolve-image=changed"
}

if cmd.ForceRecreateStack {
resolveImageArgs = "--resolve-image=always"
}
args = append(args, resolveImageArgs)

for _, cfile := range cmd.ComposeRelativeFilePaths {
args = append(args, "--compose-file", path.Join(clonePath, cfile))
Expand Down
2 changes: 2 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type DeployCommand struct {
Password string `help:"Password or PAT for Git authentication" short:"p"`
Keep bool `help:"Keep stack folder" short:"k"`
SkipTLSVerify bool `help:"Skip TLS verification for git" name:"skip-tls-verify"`
ForceRecreateStack bool `help:"Force to recreate the target stack regardless whether the image hash changes" name:"force-recreate"`
Env []string `help:"OS ENV for stack" example:"key=value"`
Registry []string `help:"Registry credentials" name:"registry"`
GitRepository string `arg:"" help:"Git repository to deploy from." name:"git-repo"`
Expand All @@ -39,6 +40,7 @@ type SwarmDeployCommand struct {
Prune bool `help:"Prune services during deployment" short:"r"`
Keep bool `help:"Keep stack folder" short:"k"`
SkipTLSVerify bool `help:"Skip TLS verification for git" name:"skip-tls-verify"`
ForceRecreateStack bool `help:"Force to recreate the target stack regardless whether the image hash changes" name:"force-recreate"`
Env []string `help:"OS ENV for stack."`
Registry []string `help:"Registry credentials" name:"registry"`
GitRepository string `arg:"" help:"Git repository to deploy from." name:"git-repo"`
Expand Down

0 comments on commit f063fd5

Please sign in to comment.