-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: dockerfile action support build context from user input
Signed-off-by: Ash <[email protected]>
- Loading branch information
Showing
7 changed files
with
241 additions
and
112 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,16 @@ | ||
FROM registry.erda.cloud/erda-actions/custom-script:2.0 AS builder | ||
FROM --platform=$TARGETPLATFORM registry.erda.cloud/retag/buildkit:v0.11.3 as buildkit | ||
FROM --platform=$TARGETPLATFORM registry.erda.cloud/erda-x/golang:1.22 AS builder | ||
|
||
ENV CGO_ENABLED 0 | ||
|
||
COPY . /go/src/github.com/erda-project/erda-actions | ||
WORKDIR /go/src/github.com/erda-project/erda-actions | ||
|
||
ENV CGO_ENABLED=0 | ||
|
||
COPY . . | ||
|
||
RUN go build -o /opt/action/run github.com/erda-project/erda-actions/actions/dockerfile/1.0/internal/cmd | ||
|
||
FROM registry.erda.cloud/erda-actions/custom-script:2.0 | ||
FROM --platform=$TARGETPLATFORM registry.erda.cloud/erda-x/debian-bookworm:12 | ||
|
||
COPY --from=builder /opt/action/run /opt/action/run | ||
COPY --from=buildkit /usr/bin/buildctl /usr/bin/buildctl | ||
RUN chmod +x /opt/action/run |
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,91 @@ | ||
package build | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"path" | ||
"path/filepath" | ||
"strconv" | ||
|
||
"github.com/erda-project/erda-actions/actions/dockerfile/1.0/internal/pkg/conf" | ||
pkgconf "github.com/erda-project/erda-actions/pkg/envconf" | ||
"github.com/erda-project/erda-infra/pkg/strutil" | ||
) | ||
|
||
const ( | ||
defaultCaCertPath = "/.buildkit/ca.pem" | ||
defaultCertPath = "/.buildkit/cert.pem" | ||
defaultCertKeyPath = "/.buildkit/key.pem" | ||
) | ||
|
||
type buildkit struct { | ||
config *conf.Conf | ||
args []string | ||
} | ||
|
||
func NewBuildkit(c *conf.Conf) Builder { | ||
args := []string{ | ||
"--addr", c.BuildkitdAddr, | ||
fmt.Sprintf("--tlscacert=%s", defaultCaCertPath), | ||
fmt.Sprintf("--tlscert=%s", defaultCertPath), | ||
fmt.Sprintf("--tlskey=%s", defaultCertKeyPath), | ||
"build", | ||
"--frontend", "dockerfile.v0", | ||
"--local", "context=" + c.Context, | ||
"--local", "dockerfile=" + resolveDockerfileDir(c), | ||
"--opt", fmt.Sprintf("platform=%s", pkgconf.GetTargetPlatforms()), | ||
} | ||
|
||
return &buildkit{ | ||
config: c, | ||
args: args, | ||
} | ||
} | ||
|
||
func (b *buildkit) Build(p *Params, o *OutPut) error { | ||
// generate command from action params provided | ||
b.appendBuildArgs(p.Args) | ||
b.appendBuildContexts(p.BuildContext) | ||
|
||
// set output | ||
b.args = append(b.args, | ||
"--output", | ||
fmt.Sprintf("type=image,name=%s,push=%s", o.Image, strconv.FormatBool(o.Push)), | ||
) | ||
|
||
buildCmd := exec.Command("buildctl", b.args...) | ||
fmt.Println(strutil.Join(buildCmd.Args, " ", false)) | ||
|
||
buildCmd.Dir = b.config.WorkDir | ||
buildCmd.Stdout = os.Stdout | ||
buildCmd.Stderr = os.Stderr | ||
|
||
if err := buildCmd.Run(); err != nil { | ||
return fmt.Errorf("failed to build image: %v", err) | ||
} | ||
|
||
fmt.Fprintf(os.Stdout, "Successfully built and pushed image: %s\n", o.Image) | ||
return nil | ||
} | ||
|
||
func resolveDockerfileDir(c *conf.Conf) string { | ||
if path.IsAbs(c.Path) { | ||
return filepath.Dir(c.Path) | ||
} | ||
return filepath.Dir(path.Join(c.Context, c.Path)) | ||
} | ||
|
||
func (b *buildkit) appendBuildArgs(args map[string]string) { | ||
for k, v := range args { | ||
b.args = append(b.args, "--opt", fmt.Sprintf("build-arg:%s=%s", k, v)) | ||
} | ||
} | ||
|
||
func (b *buildkit) appendBuildContexts(buildContexts map[string]string) { | ||
for k, v := range buildContexts { | ||
b.args = append(b.args, | ||
"--local", fmt.Sprintf("%s=%s", k, v), | ||
"--opt", fmt.Sprintf("context:%s=local:%s", k, k)) | ||
} | ||
} |
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,77 @@ | ||
package build | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"strconv" | ||
|
||
"github.com/erda-project/erda-actions/actions/dockerfile/1.0/internal/pkg/conf" | ||
"github.com/erda-project/erda-actions/pkg/docker" | ||
"github.com/erda-project/erda/apistructs" | ||
) | ||
|
||
type dockerBuilder struct { | ||
config *conf.Conf | ||
args []string | ||
} | ||
|
||
func NewDocker(c *conf.Conf) Builder { | ||
args := []string{ | ||
"build", | ||
".", // set current dir is build context | ||
"-f", c.Path, | ||
} | ||
|
||
return &dockerBuilder{ | ||
config: c, | ||
args: args, | ||
} | ||
} | ||
|
||
func (d *dockerBuilder) Build(p *Params, o *OutPut) error { | ||
// generate command from action params provided | ||
d.appendBuildArgs(p.Args) | ||
d.appendBuildContexts(p.BuildContext) | ||
|
||
// set resource limit | ||
d.args = append(d.args, | ||
"--cpu-quota", strconv.FormatFloat(d.config.CPU*100000, 'f', 0, 64), | ||
"--memory", strconv.FormatInt(int64(d.config.Memory*apistructs.MB), 10), | ||
) | ||
|
||
// generate build command | ||
buildCmd := exec.Command("docker", d.args...) | ||
fmt.Fprintf(os.Stdout, "Docker build command: %v\n", buildCmd.Args) | ||
|
||
buildCmd.Stdout = os.Stdout | ||
buildCmd.Stderr = os.Stderr | ||
buildCmd.Dir = d.config.WorkDir | ||
|
||
if err := buildCmd.Run(); err != nil { | ||
return fmt.Errorf("failed to build docker image: %v", err) | ||
} | ||
|
||
if o.Push { | ||
if err := docker.PushByCmd(o.Image, ""); err != nil { | ||
return fmt.Errorf("failed to push docker image: %v", err) | ||
} | ||
} | ||
|
||
fmt.Fprintf(os.Stdout, "Successfully built and pushed image: %s\n", o.Image) | ||
return nil | ||
} | ||
|
||
// appendBuildArgs | ||
func (d *dockerBuilder) appendBuildArgs(args map[string]string) { | ||
for k, v := range args { | ||
d.args = append(d.args, "--build-arg", fmt.Sprintf("%s=%s", k, v)) | ||
} | ||
} | ||
|
||
// appendBuildContexts | ||
func (d *dockerBuilder) appendBuildContexts(contexts map[string]string) { | ||
for k, v := range contexts { | ||
d.args = append(d.args, "--build-context", fmt.Sprintf("%s=%s", k, v)) | ||
} | ||
} |
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.