Skip to content

Commit

Permalink
supporting Docker build contexts (support for monorepos) (#877)
Browse files Browse the repository at this point in the history
  • Loading branch information
laszlocph authored Oct 6, 2024
1 parent bfff2fd commit 50634e8
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 11 deletions.
2 changes: 1 addition & 1 deletion cmd/agent/imagebuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ func generateJob(trigger dx.ImageBuildRequest, name, sourceUrl string) *batchv1.
Image: "gcr.io/kaniko-project/executor:latest",
Args: []string{
fmt.Sprintf("--dockerfile=/workspace/%s", trigger.Dockerfile),
"--context=dir:///workspace",
"--context=dir:///workspace/" + trigger.Context,
fmt.Sprintf("--destination=%s:%s", trigger.Image, trigger.Tag),
},
VolumeMounts: []corev1.VolumeMount{
Expand Down
8 changes: 6 additions & 2 deletions pkg/dashboard/gitops/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,11 +391,12 @@ func ExtractImageStrategy(envConfig *dx.Manifest) string {
return "dynamic"
}

func ExtractImageRepoTagDockerfileAndRegistry(envConfig *dx.Manifest, vars map[string]string) (string, string, string, string) {
func ExtractImageRepoTagDockerfileAndRegistry(envConfig *dx.Manifest, vars map[string]string) (string, string, string, string, string) {
envConfig.ResolveVars(vars)
image := envConfig.Values["image"]

var repository, tag, dockerfile, registry string
context := "."
if image != nil {
imageMap := image.(map[string]interface{})

Expand All @@ -405,6 +406,9 @@ func ExtractImageRepoTagDockerfileAndRegistry(envConfig *dx.Manifest, vars map[s
if val, ok := imageMap["tag"]; ok {
tag = val.(string)
}
if val, ok := imageMap["context"]; ok {
context = val.(string)
}
if val, ok := imageMap["dockerfile"]; ok {
dockerfile = val.(string)
}
Expand All @@ -413,5 +417,5 @@ func ExtractImageRepoTagDockerfileAndRegistry(envConfig *dx.Manifest, vars map[s
}
}

return repository, tag, dockerfile, registry
return repository, tag, context, dockerfile, registry
}
3 changes: 2 additions & 1 deletion pkg/dashboard/server/releases.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ func release(w http.ResponseWriter, r *http.Request) {

vars := artifact.CollectVariables()
vars["APP"] = releaseRequest.App
imageRepository, imageTag, dockerfile, registry := gitops.ExtractImageRepoTagDockerfileAndRegistry(manifest, vars)
imageRepository, imageTag, context, dockerfile, registry := gitops.ExtractImageRepoTagDockerfileAndRegistry(manifest, vars)
// Image push happens inside the cluster, pull is handled by the kubelet that doesn't speak cluster local addresses
imageRepository = strings.ReplaceAll(imageRepository, "127.0.0.1:32447", "registry.infrastructure.svc.cluster.local:5000")
imageBuildRequest = &dx.ImageBuildRequest{
Expand All @@ -299,6 +299,7 @@ func release(w http.ResponseWriter, r *http.Request) {
TriggeredBy: user.Login,
Image: imageRepository,
Tag: imageTag,
Context: context,
Dockerfile: dockerfile,
Strategy: strategy,
Registry: registry,
Expand Down
3 changes: 2 additions & 1 deletion pkg/dashboard/worker/gitops.go
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ func processArtifactEvent(

strategy := gitops.ExtractImageStrategy(manifest)
if strategy == "buildpacks" || strategy == "dockerfile" { // image build
imageRepository, imageTag, dockerfile, registry := gitops.ExtractImageRepoTagDockerfileAndRegistry(manifest, vars)
imageRepository, imageTag, context, dockerfile, registry := gitops.ExtractImageRepoTagDockerfileAndRegistry(manifest, vars)
// Image push happens inside the cluster, pull is handled by the kubelet that doesn't speak cluster local addresses
imageRepository = strings.ReplaceAll(imageRepository, "127.0.0.1:32447", "registry.infrastructure.svc.cluster.local:5000")
imageBuildRequest := &dx.ImageBuildRequest{
Expand All @@ -714,6 +714,7 @@ func processArtifactEvent(
TriggeredBy: "policy",
Image: imageRepository,
Tag: imageTag,
Context: context,
Dockerfile: dockerfile,
Strategy: strategy,
Registry: registry,
Expand Down
1 change: 1 addition & 0 deletions pkg/dx/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type ImageBuildRequest struct {
Image string `json:"image"`
Tag string `json:"tag"`
SourcePath string `json:"sourcePath"`
Context string `json:"context"`
Dockerfile string `json:"dockerfile"`
Strategy string `json:"strategy"`
Registry string `json:"registry"`
Expand Down
27 changes: 21 additions & 6 deletions web/src/views/envConfig/imageWidget.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ export default function ImageWidget(props) {
})
}

const setContext = (context) => {
setImage({
...image,
"context": context,
})
}

const setStrategy = (strategy) => {
let registry = {}
registry = registries.find(r => r.variable === "customRegistry")
Expand All @@ -50,6 +57,7 @@ export default function ImageWidget(props) {
"registry": registry.variable,
"repository": registry.url+"/{{ .APP }}",
"tag": "{{ .SHA }}",
"context": ".",
"dockerfile": "Dockerfile"
})
break;
Expand Down Expand Up @@ -192,14 +200,21 @@ export default function ImageWidget(props) {
</div>
<div className="form-group field field-string">
<label className="control-label" htmlFor="root_tag">Tag<span className="required"></span></label>
<input className="form-control" id="root_tag" label="Tag" required="" placeholder="" type="text" list="examples_root_tag" value={image.tag} onChange={e=>setTag(e.target.value)}/>
<input className="form-control max-w-64" id="root_tag" label="Tag" required="" placeholder="" type="text" list="examples_root_tag" value={image.tag} onChange={e=>setTag(e.target.value)}/>
</div>
{ image.strategy === "dockerfile" &&
<div className="form-group field field-string">
<label className="control-label" htmlFor="root_tag">Dockerfile<span className="required"></span></label>
<input className="form-control" id="root_tag" label="Dockerfile" required="" placeholder="" type="text" list="examples_root_tag" value={image.dockerfile} onChange={e=>setDockerfile(e.target.value)}/>
<p className="help-block">Case-sensitive relative path from the project root to the Dockerfile.</p>
</div>
<>
<div className="form-group field field-string">
<label className="control-label" htmlFor="root_tag">Context<span className="required"></span></label>
<input className="form-control max-w-64" id="root_tag" label="Context" required="" placeholder="" type="text" list="examples_root_tag" value={image.context} onChange={e=>setContext(e.target.value)}/>
<p className="help-block">Case-sensitive relative path from the git repository root (signaled as '.') to the project root. Change it for monorepos, like 'backend/'.</p>
</div>
<div className="form-group field field-string">
<label className="control-label" htmlFor="root_tag">Dockerfile<span className="required"></span></label>
<input className="form-control max-w-64" id="root_tag" label="Dockerfile" required="" placeholder="" type="text" list="examples_root_tag" value={image.dockerfile} onChange={e=>setDockerfile(e.target.value)}/>
<p className="help-block">Case-sensitive relative path from the project root to the Dockerfile, like 'backend/Dockerfile'</p>
</div>
</>
}
</fieldset>
</div>
Expand Down

0 comments on commit 50634e8

Please sign in to comment.