Skip to content
Snippets Groups Projects
Commit 3932ca2f authored by Kamil Trzcinski's avatar Kamil Trzcinski
Browse files

By default all volumes are shared between projects

parent f118acc5
Branches caching
No related tags found
No related merge requests found
...@@ -198,32 +198,44 @@ func (s *DockerExecutor) createCacheVolume(containerName, containerPath string) ...@@ -198,32 +198,44 @@ func (s *DockerExecutor) createCacheVolume(containerName, containerPath string)
return container, nil return container, nil
} }
func (s *DockerExecutor) addCacheVolume(binds, volumesFrom *[]string, containerPath string) error { func (s *DockerExecutor) addCacheVolume(binds, volumesFrom *[]string, containerPath string, shared bool) error {
var err error var err error
containerPath = s.getAbsoluteContainerPath(containerPath) containerPath = s.getAbsoluteContainerPath(containerPath)
// disable cache for automatic container cache, but leave it for host volumes (they are shared on purpose) // disable cache for automatic container cache, but leave it for host volumes (they are shared on purpose)
if helpers.BoolOrDefault(s.Config.Docker.DisableCache, false) { if helpers.BoolOrDefault(s.Config.Docker.DisableCache, false) {
s.Debugln("Container cache for", containerPath, " is disabled.") s.Debugln("Container cache for", containerPath, "is disabled.")
return nil return nil
} }
hash := md5.Sum([]byte(containerPath)) hash := fmt.Sprintf("%x",md5.Sum([]byte(containerPath)))
// use host-based cache // use host-based cache
if cacheDir := helpers.StringOrDefault(s.Config.Docker.CacheDir, ""); cacheDir != "" { if cacheDir := helpers.StringOrDefault(s.Config.Docker.CacheDir, ""); cacheDir != "" {
hostPath := fmt.Sprintf("%s/%s/%x", cacheDir, s.Build.ProjectUniqueName(), hash) hostPath, err := filepath.Abs(cacheDir)
hostPath, err := filepath.Abs(hostPath)
if err != nil { if err != nil {
return err return err
} }
// For shared directory
if shared {
hostPath = filepath.Join(hostPath, "runner-" + s.Build.Runner.ShortDescription())
hostPath = filepath.Join(hostPath, "project-" + strconv.Itoa(s.Build.ProjectID))
} else {
hostPath = filepath.Join(hostPath, s.Build.Runner.ShortDescription())
}
hostPath = filepath.Join(hostPath, hash)
s.Debugln("Using path", hostPath, "as cache for", containerPath, "...") s.Debugln("Using path", hostPath, "as cache for", containerPath, "...")
*binds = append(*binds, fmt.Sprintf("%v:%v", hostPath, containerPath)) *binds = append(*binds, fmt.Sprintf("%v:%v", hostPath, containerPath))
return nil return nil
} }
// get existing cache container // get existing cache container
containerName := fmt.Sprintf("%s-cache-%x", s.Build.ProjectUniqueName(), hash) containerName := fmt.Sprintf("%s-cache-%s", s.Build.ProjectUniqueName(), hash)
if shared {
containerName = fmt.Sprintf("runner-%s-project-%d-cache-%s", s.Build.Runner.ShortDescription(), s.Build.ProjectID, hash)
}
container, _ := s.client.InspectContainer(containerName) container, _ := s.client.InspectContainer(containerName)
// check if we have valid cache, if not remove the broken container // check if we have valid cache, if not remove the broken container
...@@ -245,7 +257,7 @@ func (s *DockerExecutor) addCacheVolume(binds, volumesFrom *[]string, containerP ...@@ -245,7 +257,7 @@ func (s *DockerExecutor) addCacheVolume(binds, volumesFrom *[]string, containerP
return nil return nil
} }
func (s *DockerExecutor) addVolume(binds, volumesFrom *[]string, volume string) error { func (s *DockerExecutor) addVolume(binds, volumesFrom *[]string, volume string, shared bool) error {
var err error var err error
hostVolume := strings.SplitN(volume, ":", 2) hostVolume := strings.SplitN(volume, ":", 2)
switch len(hostVolume) { switch len(hostVolume) {
...@@ -254,7 +266,7 @@ func (s *DockerExecutor) addVolume(binds, volumesFrom *[]string, volume string) ...@@ -254,7 +266,7 @@ func (s *DockerExecutor) addVolume(binds, volumesFrom *[]string, volume string)
case 1: case 1:
// disable cache disables // disable cache disables
err = s.addCacheVolume(binds, volumesFrom, hostVolume[0]) err = s.addCacheVolume(binds, volumesFrom, hostVolume[0], shared)
} }
if err != nil { if err != nil {
...@@ -265,9 +277,8 @@ func (s *DockerExecutor) addVolume(binds, volumesFrom *[]string, volume string) ...@@ -265,9 +277,8 @@ func (s *DockerExecutor) addVolume(binds, volumesFrom *[]string, volume string)
func (s *DockerExecutor) createVolumes() ([]string, []string, error) { func (s *DockerExecutor) createVolumes() ([]string, []string, error) {
var binds, volumesFrom []string var binds, volumesFrom []string
for _, volume := range s.Config.Docker.Volumes { for _, volume := range s.Config.Docker.Volumes {
s.addVolume(&binds, &volumesFrom, volume) s.addVolume(&binds, &volumesFrom, volume, true)
} }
// Cache Git sources: // Cache Git sources:
...@@ -279,7 +290,7 @@ func (s *DockerExecutor) createVolumes() ([]string, []string, error) { ...@@ -279,7 +290,7 @@ func (s *DockerExecutor) createVolumes() ([]string, []string, error) {
if filepath.IsAbs(parentDir) && parentDir != "/" { if filepath.IsAbs(parentDir) && parentDir != "/" {
if s.Build.AllowGitFetch && !helpers.BoolOrDefault(s.Config.Docker.DisableCache, false) { if s.Build.AllowGitFetch && !helpers.BoolOrDefault(s.Config.Docker.DisableCache, false) {
// create persistent cache container // create persistent cache container
s.addVolume(&binds, &volumesFrom, parentDir) s.addVolume(&binds, &volumesFrom, parentDir, false)
} else { } else {
// create temporary cache container // create temporary cache container
container, _ := s.createCacheVolume("", parentDir) container, _ := s.createCacheVolume("", parentDir)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment