Merge pull request #6467 from dokku/registry-repo-template
Add ability to customize the registry repo with a template
This commit is contained in:
@@ -102,6 +102,16 @@ Setting the property value to an empty string will reset the value to the system
|
|||||||
dokku registry:set node-js-app image-repo
|
dokku registry:set node-js-app image-repo
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Templating the image repository name
|
||||||
|
|
||||||
|
Instead of setting the image repository name on a per-app basis, it can be set via a template globally with the `image-repo-template` property:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
dokku registry:set --global image-repo-template "my-awesome-prefix/{{ .AppName }}"
|
||||||
|
```
|
||||||
|
|
||||||
|
Dokku uses a Golang template and has access to the `AppName` variable as shown above.
|
||||||
|
|
||||||
### Pushing images on build
|
### Pushing images on build
|
||||||
|
|
||||||
To push the image on release, set the `push-on-release` property to `true` via the `registry:set` command. The default value for this property is `false`. Setting the property to `true` will result in the image being tagged with an ID that is incremented with every release. This tag will be what is used for running app code.
|
To push the image on release, set the `push-on-release` property to `true` via the `registry:set` command. The default value for this property is `false`. Setting the property to `true` will result in the image being tagged with an ID that is incremented with every release. This tag will be what is used for running app code.
|
||||||
|
|||||||
@@ -1,16 +1,42 @@
|
|||||||
package registry
|
package registry
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"text/template"
|
||||||
|
|
||||||
"github.com/codeskyblue/go-sh"
|
"github.com/codeskyblue/go-sh"
|
||||||
"github.com/dokku/dokku/plugins/common"
|
"github.com/dokku/dokku/plugins/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func getImageRepoFromTemplate(appName string) (string, error) {
|
||||||
|
imageRepoTemplate := common.PropertyGet("registry", "--global", "image-repo-template")
|
||||||
|
if imageRepoTemplate == "" {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
tmpl, err := template.New("template").Parse(imageRepoTemplate)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("Unable to parse image-repo-template: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
type templateData struct {
|
||||||
|
AppName string
|
||||||
|
}
|
||||||
|
data := templateData{AppName: appName}
|
||||||
|
|
||||||
|
var doc bytes.Buffer
|
||||||
|
if err := tmpl.Execute(&doc, data); err != nil {
|
||||||
|
return "", fmt.Errorf("Unable to execute image-repo-template: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.TrimSpace(doc.String()), nil
|
||||||
|
}
|
||||||
|
|
||||||
func getRegistryServerForApp(appName string) string {
|
func getRegistryServerForApp(appName string) string {
|
||||||
value := common.PropertyGet("registry", appName, "server")
|
value := common.PropertyGet("registry", appName, "server")
|
||||||
if value == "" {
|
if value == "" {
|
||||||
|
|||||||
@@ -10,7 +10,8 @@ var (
|
|||||||
|
|
||||||
// GlobalProperties is a map of all valid global registry properties
|
// GlobalProperties is a map of all valid global registry properties
|
||||||
GlobalProperties = map[string]bool{
|
GlobalProperties = map[string]bool{
|
||||||
"push-on-release": true,
|
"image-repo-template": true,
|
||||||
"server": true,
|
"push-on-release": true,
|
||||||
|
"server": true,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -13,15 +13,16 @@ func ReportSingleApp(appName string, format string, infoFlag string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
flags := map[string]common.ReportFunc{
|
flags := map[string]common.ReportFunc{
|
||||||
"--registry-computed-image-repo": reportComputedImageRepo,
|
"--registry-computed-image-repo": reportComputedImageRepo,
|
||||||
"--registry-image-repo": reportImageRepo,
|
"--registry-image-repo": reportImageRepo,
|
||||||
"--registry-computed-push-on-release": reportComputedPushOnRelease,
|
"--registry-computed-push-on-release": reportComputedPushOnRelease,
|
||||||
"--registry-global-push-on-release": reportGlobalPushOnRelease,
|
"--registry-global-push-on-release": reportGlobalPushOnRelease,
|
||||||
"--registry-push-on-release": reportPushOnRelease,
|
"--registry-push-on-release": reportPushOnRelease,
|
||||||
"--registry-computed-server": reportComputedServer,
|
"--registry-computed-server": reportComputedServer,
|
||||||
"--registry-global-server": reportGlobalServer,
|
"--registry-global-server": reportGlobalServer,
|
||||||
"--registry-server": reportServer,
|
"--registry-global-image-repo-template": reportGlobalImageRepoTemplate,
|
||||||
"--registry-tag-version": reportTagVersion,
|
"--registry-server": reportServer,
|
||||||
|
"--registry-tag-version": reportTagVersion,
|
||||||
}
|
}
|
||||||
|
|
||||||
flagKeys := []string{}
|
flagKeys := []string{}
|
||||||
@@ -36,8 +37,11 @@ func ReportSingleApp(appName string, format string, infoFlag string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func reportComputedImageRepo(appName string) string {
|
func reportComputedImageRepo(appName string) string {
|
||||||
imageRepo := reportImageRepo(appName)
|
imageRepo := strings.TrimSpace(reportImageRepo(appName))
|
||||||
imageRepo = strings.TrimSpace(imageRepo)
|
if imageRepo == "" {
|
||||||
|
imageRepo, _ = getImageRepoFromTemplate(appName)
|
||||||
|
}
|
||||||
|
|
||||||
if imageRepo == "" {
|
if imageRepo == "" {
|
||||||
imageRepo = common.GetAppImageRepo(appName)
|
imageRepo = common.GetAppImageRepo(appName)
|
||||||
}
|
}
|
||||||
@@ -76,6 +80,10 @@ func reportComputedServer(appName string) string {
|
|||||||
return strings.TrimSpace(server)
|
return strings.TrimSpace(server)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func reportGlobalImageRepoTemplate(appName string) string {
|
||||||
|
return common.PropertyGet("registry", "--global", "image-repo-template")
|
||||||
|
}
|
||||||
|
|
||||||
func reportGlobalServer(appName string) string {
|
func reportGlobalServer(appName string) string {
|
||||||
return common.PropertyGet("registry", "--global", "server")
|
return common.PropertyGet("registry", "--global", "server")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,8 +10,15 @@ import (
|
|||||||
|
|
||||||
// TriggerDeployedAppImageRepo outputs the associated image repo to stdout
|
// TriggerDeployedAppImageRepo outputs the associated image repo to stdout
|
||||||
func TriggerDeployedAppImageRepo(appName string) error {
|
func TriggerDeployedAppImageRepo(appName string) error {
|
||||||
imageRepo := common.PropertyGet("registry", appName, "image-repo")
|
imageRepo := strings.TrimSpace(reportImageRepo(appName))
|
||||||
imageRepo = strings.TrimSpace(imageRepo)
|
if imageRepo == "" {
|
||||||
|
var err error
|
||||||
|
imageRepo, err = getImageRepoFromTemplate(appName)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Unable to determine image repo from template: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if imageRepo == "" {
|
if imageRepo == "" {
|
||||||
imageRepo = common.GetAppImageRepo(appName)
|
imageRepo = common.GetAppImageRepo(appName)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user