Skip to content
Snippets Groups Projects
Commit 3f0461ca authored by Aurélien Thieriot's avatar Aurélien Thieriot Committed by Aurélien Thieriot
Browse files

Discover the registry from the image name

parent 0fecb82f
No related branches found
No related tags found
No related merge requests found
......@@ -24,7 +24,6 @@ type DockerConfig struct {
DisableCache *bool `toml:"disable_cache" json:"disable_cache"`
Volumes []string `toml:"volumes" json:"volumes"`
CacheDir *string `toml:"cache_dir" json:"cache_dir"`
Registry *string `toml:"registry" json:"registry"`
ExtraHosts []string `toml:"extra_hosts" json:"extra_hosts"`
Links []string `toml:"links" json:"links"`
Services []string `toml:"services" json:"services"`
......
......@@ -22,7 +22,6 @@ concurrent = 4
privileged = false
disable_cache = false
cache_dir = ""
registry = ""
[[runners]]
name = "ruby-2.1-docker-ssh"
......@@ -37,7 +36,6 @@ concurrent = 4
privileged = false
disable_cache = false
cache_dir = ""
registry = ""
[runners.ssh]
port = "22"
user = "root"
......
......@@ -83,7 +83,6 @@ This defines the Docker Container parameters.
| `disable_cache` | disable automatic |
| `wait_for_services_timeout` | specify how long to wait for docker services, set to 0 to disable, default: 30 |
| `cache_dir` | specify where Docker caches should be stored (this can be absolute or relative to current working directory) |
| `registry` | specify custom Docker registry to be used |
| `volumes` | specify additional volumes that should be mounted (same syntax as Docker -v option) |
| `extra_hosts` | specify hosts that should be defined in container environment |
| `links` | specify containers which should be linked with building container |
......@@ -103,7 +102,6 @@ Example:
disable_cache = false
wait_for_services_timeout = 30
cache_dir = ""
registry = ""
volumes = ["/data", "/home/project/cache"]
extra_hosts = ["other-host:127.0.0.1"]
links = ["mysql_container:mysql"]
......@@ -196,4 +194,4 @@ Example:
### Note
If you'd like to deploy to multiple servers using GitLab CI, you can create a single script that deploys to multiple servers or you can create many scripts. It depends on what you'd like to do.
\ No newline at end of file
If you'd like to deploy to multiple servers using GitLab CI, you can create a single script that deploys to multiple servers or you can create many scripts. It depends on what you'd like to do.
......@@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"os"
u "os/user"
"path/filepath"
"strconv"
"strings"
......@@ -43,8 +44,16 @@ func (s *DockerExecutor) getServiceVariables() []string {
return variables
}
func NewAuthConfigurationsFromConfigJson() (*docker.AuthConfigurations, error) {
p := path.Join(os.Getenv("HOME"), ".docker", "config.json")
func (s *DockerExecutor) newAuthConfigurationsFromConfigJson() (*docker.AuthConfigurations, error) {
user, err := u.Current()
if s.Shell.User != nil {
user, err = u.Lookup(*s.Shell.User)
}
if err != nil {
return nil, err
}
p := path.Join(user.HomeDir, ".docker", "config.json")
r, err := os.Open(p)
if err != nil {
return nil, err
......@@ -54,24 +63,24 @@ func NewAuthConfigurationsFromConfigJson() (*docker.AuthConfigurations, error) {
return docker.NewAuthConfigurations(r)
}
func (s *DockerExecutor) getAuthConfig(registry string) (docker.AuthConfiguration, error) {
if registry == "" {
return docker.AuthConfiguration{}, nil
}
func (s *DockerExecutor) getAuthConfig(registry *string) (docker.AuthConfiguration, error) {
authConfigs, err := docker.NewAuthConfigurationsFromDockerCfg()
if err != nil {
authConfigs, err = NewAuthConfigurationsFromConfigJson()
authConfigs, err = s.newAuthConfigurationsFromConfigJson()
if err != nil {
return docker.AuthConfiguration{}, err
}
}
if authConfig, ok := authConfigs.Configs[registry]; ok == true {
return authConfig, nil
authRegistry := helpers.StringOrDefault(registry, "docker.io")
for authKey, authConfig := range authConfigs.Configs {
if strings.Contains(authKey, authRegistry) {
return authConfig, nil
}
}
return docker.AuthConfiguration{}, fmt.Errorf("failed to find credentials for %v in docker config files", registry)
s.Warningln("No credentials found for", authRegistry, "in the docker config files")
return docker.AuthConfiguration{}, nil
}
func (s *DockerExecutor) getDockerImage(imageName string) (*docker.Image, error) {
......@@ -84,10 +93,9 @@ func (s *DockerExecutor) getDockerImage(imageName string) (*docker.Image, error)
s.Println("Pulling docker image", imageName, "...")
pullImageOptions := docker.PullImageOptions{
Repository: imageName,
Registry: helpers.StringOrDefault(s.Config.Docker.Registry, ""),
}
authConfig, err := s.getAuthConfig(pullImageOptions.Registry)
authConfig, err := s.getAuthConfig(helpers.ExtractRegistry(imageName))
if err != nil {
s.Warningln(err)
}
......
package helpers
import (
"strings"
)
func ExtractRegistry(imageName string) *string {
nameParts := strings.Split(imageName, "/")
if len(nameParts) == 3 {
return &nameParts[0]
}
return nil
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment