Created
July 17, 2022 14:08
-
-
Save dot-mike/d38427fd3776ca4a31de59e3e2fbf4e1 to your computer and use it in GitHub Desktop.
Support for running Git hooks in Portainer 2.14
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/api/exec/compose_stack.go b/api/exec/compose_stack.go | |
index e99f265b5..2dbfc6b1f 100644 | |
--- a/api/exec/compose_stack.go | |
+++ b/api/exec/compose_stack.go | |
@@ -194,7 +194,7 @@ func createNetworkEnvFile(stack *portainer.Stack) error { | |
} | |
envfile, err := os.OpenFile(path.Join(stack.ProjectPath, ".env"), | |
- os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) | |
+ os.O_WRONLY|os.O_CREATE, 0600) | |
if err != nil { | |
return errors.Wrap(err, "failed to open env file") | |
} | |
diff --git a/api/git/git.go b/api/git/git.go | |
index 142c1c67a..2d1658f71 100644 | |
--- a/api/git/git.go | |
+++ b/api/git/git.go | |
@@ -136,7 +136,7 @@ func NewService() *Service { | |
return &Service{ | |
httpsCli: httpsCli, | |
azure: NewAzureDownloader(httpsCli), | |
- git: gitClient{}, | |
+ git: gitClient{preserveGitDirectory: true}, | |
} | |
} | |
diff --git a/api/http/handler/stacks/create_compose_stack.go b/api/http/handler/stacks/create_compose_stack.go | |
index 134a08784..9dceae740 100644 | |
--- a/api/http/handler/stacks/create_compose_stack.go | |
+++ b/api/http/handler/stacks/create_compose_stack.go | |
@@ -4,6 +4,9 @@ import ( | |
"fmt" | |
"log" | |
"net/http" | |
+ "os" | |
+ "os/exec" | |
+ "path/filepath" | |
"strconv" | |
"time" | |
@@ -275,6 +278,15 @@ func (handler *Handler) createComposeStackFromGitRepository(w http.ResponseWrite | |
} | |
stack.GitConfig.ConfigHash = commitID | |
+ // trigger git checkout | |
+ gitout, err := exec.Command("git", "-C", projectPath, "checkout", ".").CombinedOutput() | |
+ if err != nil { | |
+ return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: "create_compose_stack.go: Failed git checkout stack", Err: errors.WithMessagef(err, "failed git checkout %s", err)} | |
+ } | |
+ log.Print(string(gitout)) | |
+ // clean up after ourself | |
+ defer os.RemoveAll(filepath.Join(stack.ProjectPath, ".git")) | |
+ | |
config, configErr := handler.createComposeDeployConfig(r, stack, endpoint) | |
if configErr != nil { | |
return configErr | |
diff --git a/api/http/handler/stacks/stack_update_git_redeploy.go b/api/http/handler/stacks/stack_update_git_redeploy.go | |
index 74c7ae283..63f291e6e 100644 | |
--- a/api/http/handler/stacks/stack_update_git_redeploy.go | |
+++ b/api/http/handler/stacks/stack_update_git_redeploy.go | |
@@ -4,6 +4,9 @@ import ( | |
"fmt" | |
"log" | |
"net/http" | |
+ "os" | |
+ "os/exec" | |
+ "path/filepath" | |
"time" | |
"github.com/pkg/errors" | |
@@ -145,6 +148,15 @@ func (handler *Handler) stackGitRedeploy(w http.ResponseWriter, r *http.Request) | |
return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: "Unable to clone git repository", Err: err} | |
} | |
+ // trigger git checkout | |
+ gitout, err := exec.Command("git", "-C", stack.ProjectPath, "checkout", ".").CombinedOutput() | |
+ if err != nil { | |
+ return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: "create_compose_stack_redeploy.go: Failed git checkout stack", Err: errors.WithMessagef(err, "failed git checkout %s", err)} | |
+ } | |
+ log.Print(string(gitout)) | |
+ // clean up after ourself | |
+ defer os.RemoveAll(filepath.Join(stack.ProjectPath, ".git")) | |
+ | |
defer func() { | |
err = handler.FileService.RemoveDirectory(backupProjectPath) | |
if err != nil { | |
diff --git a/api/stacks/deploy.go b/api/stacks/deploy.go | |
index 3e7180687..6338096ec 100644 | |
--- a/api/stacks/deploy.go | |
+++ b/api/stacks/deploy.go | |
@@ -2,6 +2,9 @@ package stacks | |
import ( | |
"fmt" | |
+ "os" | |
+ "os/exec" | |
+ "path/filepath" | |
"strings" | |
"time" | |
@@ -77,6 +80,15 @@ func RedeployWhenChanged(stackID portainer.StackID, deployer StackDeployer, data | |
return errors.WithMessagef(err, "failed to do a fresh clone of the stack %v", stack.ID) | |
} | |
+ // trigger git checkout | |
+ gitout, err := exec.Command("git", "-C", stack.ProjectPath, "checkout", ".").CombinedOutput() | |
+ if err != nil { | |
+ return errors.WithMessagef(err, "deploy.go failed git checkout stack %v", stack.ID) | |
+ } | |
+ logger.Debug(string(gitout)) | |
+ // clean up after ourself | |
+ defer os.RemoveAll(filepath.Join(stack.ProjectPath, ".git")) | |
+ | |
endpoint, err := datastore.Endpoint().Endpoint(stack.EndpointID) | |
if err != nil { | |
return errors.WithMessagef(err, "failed to find the environment %v associated to the stack %v", stack.EndpointID, stack.ID) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment