-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Refactor the mounting options for containerized runs ⚓ (#149)
- Mounting options for containerized runs can now be adjusted better for container-in-container scenarios. Feature is in testing phase and will be documented once ready.
- Loading branch information
Showing
11 changed files
with
239 additions
and
114 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,25 @@ | ||
package container | ||
|
||
// EnvVariableContainerWorkspaceHostPath specifies the source mount path (can be a container volume too) | ||
// on the host machine where the Git repository is located in which Githooks works on. | ||
// Normally Githooks uses a bind mount, but for docker-in-docker that does not work | ||
// then we need these variables. | ||
// Example: `~/work/myproject`. | ||
const EnvVariableContainerWorkspaceHostPath = "GITHOOKS_CONTAINER_WORKSPACE_HOST_PATH" | ||
|
||
// EnvVariableContainerWorkspaceBasePath specifies a relative path to the host path | ||
// above. Normally empty. | ||
// The variable can contain `${repository-dir-name}` which is replaced by | ||
// the current base name of the repository where Githooks runs. | ||
// Example: `repos/${repository-dir-name}` (Git repo relative to `EnvVariableContainerWorkspaceHostPath`). | ||
const EnvVariableContainerWorkspaceBasePath = "GITHOOKS_CONTAINER_WORKSPACE_BASE_PATH" | ||
|
||
// EnvVariableContainerSharedHostPath specifies the source mount path (can be a container volume too) | ||
// on the host machine where the shared hook repositories are located. | ||
// Normally Githooks uses a bind mount, but for docker-in-docker that does not work | ||
// and this variable must be set if shared hooks are needed. | ||
// It makes sense to mount the host `~/.githooks/shared` path directly into | ||
// container at the same place such that they are in sync with what the containerized hooks | ||
// when this variable is set to e.g. `~/.githooks/shared`. | ||
const EnvVariableContainerSharedHostPath = "GITHOOKS_CONTAINER_SHARED_HOST_PATH" | ||
// EnvVariableContainerRunConfigFile is the YAML file which is used | ||
// for all run invocations | ||
// of containerized hooks. Its enables : | ||
// - to set custom additional arguments to the container run invocation, e.g. | ||
// mount special volumes or set special environment variables needed in CI. | ||
// - override workspace path (`/mnt/workspace`) in the container. | ||
// - override shared repository path `/mnt/shared` in the container. | ||
// | ||
// For example a file: | ||
// | ||
// ```yaml | ||
// | ||
// version: 1 | ||
// workspace-dir-dest: /builds/a/b/c | ||
// shared-dir-dest: /builds/.githooks/shared | ||
// auto-mount-workspace: false | ||
// auto-mount-shared: false | ||
// args: [ "--volumes-from", "123455" ] | ||
// | ||
// ``` | ||
// Would mount the volumes from container `123455` (podman) and | ||
// use the workspace dir `builds/a/b/c` and `/builds/.githooks/shared`. | ||
const EnvVariableContainerRunConfigFile = "GITHOOKS_CONTAINER_RUN_CONFIG_FILE" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package container | ||
|
||
import ( | ||
"os" | ||
|
||
cm "github.com/gabyx/githooks/githooks/common" | ||
) | ||
|
||
// containerRunConfig is the format of the container run arguments file. | ||
type containerRunConfig struct { | ||
// The path in the container where the workspace dir is located. | ||
// Defaults to `/mnt/workspace`. | ||
WorkspacePathDest string `yaml:"workspace-path-dest"` | ||
|
||
// If the workspace directory is automatically mounted or | ||
// you do it yourself with `Args`. Defaults to `true`. | ||
// Giving you the chance to mount it differently, | ||
// e.g. `--volumes-from other-container` when you do not | ||
// know the host path because you are already inside a container. | ||
AutoMountWorkspace bool `yaml:"auto-mount-workspace"` | ||
|
||
// The path in the container to the directory where all shared repositories | ||
// are located. | ||
// Defaults to `/mnt/shared`. | ||
SharedPathDest string `yaml:"shared-path-dest"` | ||
|
||
// If the shared directory is automatically mounted or | ||
// you do it yourself `Args`. | ||
// Giving you the chance to mount it differently, | ||
// e.g. `--volumes-from other-container` when you do not | ||
// know the host path because you are already inside a container. | ||
// Defaults to `true`. | ||
AutoMountShared bool `yaml:"auto-mount-shared"` | ||
|
||
// Additional arguments to the container run command. | ||
Args []string `yaml:"args"` | ||
|
||
// The version of this file format. | ||
Version int `yaml:"version"` | ||
} | ||
|
||
// Version for containerRunConfig. | ||
// Version 1: Initial. | ||
const containerRunConfigVersion int = 1 | ||
|
||
func createContainerRunConfig() containerRunConfig { | ||
return containerRunConfig{ | ||
WorkspacePathDest: "/mnt/workspace", | ||
AutoMountWorkspace: true, | ||
|
||
SharedPathDest: "/mnt/shared", | ||
AutoMountShared: true, | ||
|
||
Version: containerRunConfigVersion, | ||
} | ||
} | ||
|
||
func loadContainerRunConfig() (config containerRunConfig, err error) { | ||
config = createContainerRunConfig() | ||
file, exists := os.LookupEnv(EnvVariableContainerRunConfigFile) | ||
|
||
if exists && cm.IsFile(file) { | ||
|
||
err = cm.LoadYAML(file, &config) | ||
if err != nil { | ||
err = cm.CombineErrors(err, cm.ErrorF("Could not load file '%s'", file)) | ||
|
||
return | ||
} | ||
|
||
if config.Version < 0 || config.Version > containerRunConfigVersion { | ||
err = cm.ErrorF( | ||
"File '%s' has version '%v'. "+ | ||
"This version of Githooks only supports version >= 1 and <= '%v'.", | ||
file, | ||
config.Version, | ||
containerRunConfigVersion) | ||
|
||
return | ||
} | ||
} | ||
|
||
return config, nil | ||
} |
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
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
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
Oops, something went wrong.