2023-01-10 16:08:57 -06:00
|
|
|
//go:build !(WITHOUT_DOCKER || !(linux || darwin || windows))
|
|
|
|
|
2019-01-12 22:45:25 -06:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
2020-02-07 00:17:58 -06:00
|
|
|
"context"
|
2019-01-12 22:45:25 -06:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/pkg/archive"
|
2021-03-28 23:08:40 -05:00
|
|
|
|
|
|
|
// github.com/docker/docker/builder/dockerignore is deprecated
|
|
|
|
"github.com/moby/buildkit/frontend/dockerfile/dockerignore"
|
2023-01-16 11:26:22 -06:00
|
|
|
"github.com/moby/patternmatcher"
|
2021-03-28 23:08:40 -05:00
|
|
|
|
|
|
|
"github.com/nektos/act/pkg/common"
|
2019-01-12 22:45:25 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewDockerBuildExecutor function to create a run executor for the container
|
|
|
|
func NewDockerBuildExecutor(input NewDockerBuildExecutorInput) common.Executor {
|
2020-02-07 00:17:58 -06:00
|
|
|
return func(ctx context.Context) error {
|
|
|
|
logger := common.Logger(ctx)
|
2021-11-03 13:47:09 -05:00
|
|
|
if input.Platform != "" {
|
|
|
|
logger.Infof("%sdocker build -t %s --platform %s %s", logPrefix, input.ImageTag, input.Platform, input.ContextDir)
|
|
|
|
} else {
|
|
|
|
logger.Infof("%sdocker build -t %s %s", logPrefix, input.ImageTag, input.ContextDir)
|
|
|
|
}
|
2020-02-07 00:17:58 -06:00
|
|
|
if common.Dryrun(ctx) {
|
2019-01-12 22:45:25 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-03 23:15:42 -05:00
|
|
|
cli, err := GetDockerClient(ctx)
|
2019-01-12 22:45:25 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-10-24 11:50:43 -05:00
|
|
|
defer cli.Close()
|
2019-01-12 22:45:25 -06:00
|
|
|
|
2020-02-07 00:17:58 -06:00
|
|
|
logger.Debugf("Building image from '%v'", input.ContextDir)
|
2019-01-12 22:45:25 -06:00
|
|
|
|
|
|
|
tags := []string{input.ImageTag}
|
|
|
|
options := types.ImageBuildOptions{
|
2023-01-12 15:29:30 -06:00
|
|
|
Tags: tags,
|
|
|
|
Remove: true,
|
|
|
|
Platform: input.Platform,
|
|
|
|
AuthConfigs: LoadDockerAuthConfigs(ctx),
|
2023-02-08 11:14:43 -06:00
|
|
|
Dockerfile: input.Dockerfile,
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
2021-08-03 12:39:56 -05:00
|
|
|
var buildContext io.ReadCloser
|
|
|
|
if input.Container != nil {
|
|
|
|
buildContext, err = input.Container.GetContainerArchive(ctx, input.ContextDir+"/.")
|
|
|
|
} else {
|
2023-02-08 11:14:43 -06:00
|
|
|
buildContext, err = createBuildContext(ctx, input.ContextDir, input.Dockerfile)
|
2021-08-03 12:39:56 -05:00
|
|
|
}
|
2019-01-12 22:45:25 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer buildContext.Close()
|
|
|
|
|
2021-03-28 23:08:40 -05:00
|
|
|
logger.Debugf("Creating image from context dir '%s' with tag '%s' and platform '%s'", input.ContextDir, input.ImageTag, input.Platform)
|
2020-02-07 00:17:58 -06:00
|
|
|
resp, err := cli.ImageBuild(ctx, buildContext, options)
|
2019-03-01 21:16:43 -06:00
|
|
|
|
2020-02-07 00:17:58 -06:00
|
|
|
err = logDockerResponse(logger, resp.Body, err != nil)
|
2019-01-12 22:45:25 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2022-06-17 10:55:21 -05:00
|
|
|
func createBuildContext(ctx context.Context, contextDir string, relDockerfile string) (io.ReadCloser, error) {
|
|
|
|
common.Logger(ctx).Debugf("Creating archive for build context dir '%s' with relative dockerfile '%s'", contextDir, relDockerfile)
|
2019-01-12 22:45:25 -06:00
|
|
|
|
|
|
|
// And canonicalize dockerfile name to a platform-independent one
|
|
|
|
relDockerfile = archive.CanonicalTarNameForPath(relDockerfile)
|
|
|
|
|
|
|
|
f, err := os.Open(filepath.Join(contextDir, ".dockerignore"))
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
var excludes []string
|
|
|
|
if err == nil {
|
|
|
|
excludes, err = dockerignore.ReadAll(f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If .dockerignore mentions .dockerignore or the Dockerfile
|
|
|
|
// then make sure we send both files over to the daemon
|
|
|
|
// because Dockerfile is, obviously, needed no matter what, and
|
|
|
|
// .dockerignore is needed to know if either one needs to be
|
|
|
|
// removed. The daemon will remove them for us, if needed, after it
|
|
|
|
// parses the Dockerfile. Ignore errors here, as they will have been
|
|
|
|
// caught by validateContextDirectory above.
|
|
|
|
var includes = []string{"."}
|
2023-01-16 11:26:22 -06:00
|
|
|
keepThem1, _ := patternmatcher.Matches(".dockerignore", excludes)
|
|
|
|
keepThem2, _ := patternmatcher.Matches(relDockerfile, excludes)
|
2019-01-12 22:45:25 -06:00
|
|
|
if keepThem1 || keepThem2 {
|
|
|
|
includes = append(includes, ".dockerignore", relDockerfile)
|
|
|
|
}
|
|
|
|
|
|
|
|
compression := archive.Uncompressed
|
|
|
|
buildCtx, err := archive.TarWithOptions(contextDir, &archive.TarOptions{
|
|
|
|
Compression: compression,
|
|
|
|
ExcludePatterns: excludes,
|
|
|
|
IncludeFiles: includes,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return buildCtx, nil
|
|
|
|
}
|