feat(customtemplates): use Sources for CustomTemplates BE-12919 (#2759)

This commit is contained in:
andres-portainer
2026-06-05 01:51:18 -03:00
committed by GitHub
parent a779c839b7
commit 8daf0bb2a9
22 changed files with 2580 additions and 64 deletions
@@ -7,6 +7,7 @@ import (
"sync"
portainer "github.com/portainer/portainer/api"
gittypes "github.com/portainer/portainer/api/git/types"
"github.com/portainer/portainer/api/stacks/stackutils"
httperror "github.com/portainer/portainer/pkg/libhttp/error"
"github.com/portainer/portainer/pkg/libhttp/request"
@@ -42,8 +43,26 @@ func (handler *Handler) customTemplateGitFetch(w http.ResponseWriter, r *http.Re
return httperror.InternalServerError("Unable to find a custom template with the specified identifier inside the database", err)
}
if customTemplate.GitConfig == nil {
return httperror.BadRequest("Git configuration does not exist in this custom template", err)
if customTemplate.ArtifactSources == nil || len(customTemplate.ArtifactSources.SourceIDs) == 0 {
return httperror.BadRequest("Git configuration does not exist in this custom template", nil)
}
src, err := handler.DataStore.Source().Read(customTemplate.ArtifactSources.SourceIDs[0])
if err != nil {
return httperror.InternalServerError("Unable to retrieve git source for custom template", err)
}
if src.GitConfig == nil {
return httperror.InternalServerError("Source has no git configuration", nil)
}
gitConfig := &gittypes.RepoConfig{
URL: src.GitConfig.URL,
Authentication: src.GitConfig.Authentication,
TLSSkipVerify: src.GitConfig.TLSSkipVerify,
ReferenceName: customTemplate.ArtifactSources.Artifact.ReferenceName,
ConfigFilePath: customTemplate.ArtifactSources.Artifact.ConfigFilePath,
ConfigHash: customTemplate.ArtifactSources.Artifact.ConfigHash,
}
// If multiple users are trying to fetch the same custom template simultaneously, a lock needs to be added
@@ -68,7 +87,7 @@ func (handler *Handler) customTemplateGitFetch(w http.ResponseWriter, r *http.Re
}
}()
commitHash, err := stackutils.DownloadGitRepository(context.TODO(), *customTemplate.GitConfig, handler.GitService, func() string {
commitHash, err := stackutils.DownloadGitRepository(context.TODO(), *gitConfig, handler.GitService, func() string {
return customTemplate.ProjectPath
})
if err != nil {
@@ -81,15 +100,15 @@ func (handler *Handler) customTemplateGitFetch(w http.ResponseWriter, r *http.Re
return httperror.InternalServerError("Failed to download git repository", err)
}
if customTemplate.GitConfig.ConfigHash != commitHash {
customTemplate.GitConfig.ConfigHash = commitHash
if customTemplate.ArtifactSources.Artifact.ConfigHash != commitHash {
customTemplate.ArtifactSources.Artifact.ConfigHash = commitHash
if err := handler.DataStore.CustomTemplate().Update(customTemplate.ID, customTemplate); err != nil {
return httperror.InternalServerError("Unable to persist custom template changes inside the database", err)
}
}
fileContent, err := handler.FileService.GetFileContent(customTemplate.ProjectPath, customTemplate.GitConfig.ConfigFilePath)
fileContent, err := handler.FileService.GetFileContent(customTemplate.ProjectPath, gitConfig.ConfigFilePath)
if err != nil {
return httperror.InternalServerError("Unable to retrieve custom template file from disk", err)
}