diff --git a/.github/workflows/test-podman.yaml b/.github/workflows/test-podman.yaml index 4ab94dbab..200c770d9 100644 --- a/.github/workflows/test-podman.yaml +++ b/.github/workflows/test-podman.yaml @@ -18,17 +18,16 @@ jobs: - uses: ./.github/composite/go-setup - name: Install Podman run: | - # TODO uncomment following once https:.github.com/containers/podman/pull/16781 is in the kubic repository - #. /etc/os-release - #sudo mkdir -p /etc/apt/keyrings - #curl -fsSL https://download.opensuse.org/repositories/devel:kubic:libcontainers:unstable/xUbuntu_${VERSION_ID}/Release.key \ - # | gpg --dearmor \ - # | sudo tee /etc/apt/keyrings/devel_kubic_libcontainers_unstable.gpg > /dev/null - #echo \ - # "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/devel_kubic_libcontainers_unstable.gpg]\ - # https://download.opensuse.org/repositories/devel:kubic:libcontainers:unstable/xUbuntu_${VERSION_ID}/ /" \ - # | sudo tee /etc/apt/sources.list.d/devel:kubic:libcontainers:unstable.list > /dev/null - #sudo apt-get update -qq + . /etc/os-release + sudo mkdir -p /etc/apt/keyrings + curl -fsSL https://download.opensuse.org/repositories/devel:kubic:libcontainers:unstable/xUbuntu_${VERSION_ID}/Release.key \ + | gpg --dearmor \ + | sudo tee /etc/apt/keyrings/devel_kubic_libcontainers_unstable.gpg > /dev/null + echo \ + "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/devel_kubic_libcontainers_unstable.gpg]\ + https://download.opensuse.org/repositories/devel:kubic:libcontainers:unstable/xUbuntu_${VERSION_ID}/ /" \ + | sudo tee /etc/apt/sources.list.d/devel:kubic:libcontainers:unstable.list > /dev/null + sudo apt-get update -qq sudo apt-get -qq -y install podman podman info - name: Install Binaries diff --git a/pkg/builders/s2i/builder.go b/pkg/builders/s2i/builder.go index c7fb5950f..f7df3c129 100644 --- a/pkg/builders/s2i/builder.go +++ b/pkg/builders/s2i/builder.go @@ -3,6 +3,8 @@ package s2i import ( "archive/tar" "context" + "crypto/sha1" + "encoding/hex" "errors" "fmt" "io" @@ -256,6 +258,14 @@ func (b *Builder) Build(ctx context.Context, f fn.Function, platforms []fn.Platf // s2i apparently is not excluding the files in --as-dockerfile mode exclude := regexp.MustCompile(cfg.ExcludeRegExp) + // if exists, patch dockerfile to using cache mount + if _, e := os.Stat(cfg.AsDockerfile); e == nil { + err = patchDockerfile(cfg.AsDockerfile, f) + if err != nil { + return err + } + } + const up = ".." + string(os.PathSeparator) go func() { tw := tar.NewWriter(pw) @@ -333,6 +343,7 @@ func (b *Builder) Build(ctx context.Context, f fn.Function, platforms []fn.Platf opts := types.ImageBuildOptions{ Tags: []string{f.Build.Image}, PullParent: true, + Version: types.BuilderBuildKit, } resp, err := client.ImageBuild(ctx, pr, opts) @@ -356,6 +367,20 @@ func (b *Builder) Build(ctx context.Context, f fn.Function, platforms []fn.Platf return jsonmessage.DisplayJSONMessagesStream(resp.Body, out, fd, isTerminal, nil) } +func patchDockerfile(path string, f fn.Function) error { + data, err := os.ReadFile(path) + if err != nil { + return err + } + re := regexp.MustCompile(`RUN (.*assemble)`) + s := sha1.Sum([]byte(f.Root)) + mountCmd := "--mount=type=cache,target=/tmp/artifacts/,uid=1001,id=" + hex.EncodeToString(s[:8]) + replacement := fmt.Sprintf("RUN %s \\\n $1", mountCmd) + newDockerFileStr := re.ReplaceAllString(string(data), replacement) + + return os.WriteFile(path, []byte(newDockerFileStr), 0644) +} + func s2iScriptURL(ctx context.Context, cli DockerClient, image string) (string, error) { img, _, err := cli.ImageInspectWithRaw(ctx, image) if err != nil {