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
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
2020-04-23 01:04:28 -05:00
|
|
|
"github.com/pkg/errors"
|
2020-02-07 00:17:58 -06:00
|
|
|
log "github.com/sirupsen/logrus"
|
2021-03-28 23:08:40 -05:00
|
|
|
|
|
|
|
"github.com/nektos/act/pkg/common"
|
2019-01-12 22:45:25 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewDockerPullExecutorInput the input for the NewDockerPullExecutor function
|
|
|
|
type NewDockerPullExecutorInput struct {
|
2020-02-07 00:17:58 -06:00
|
|
|
Image string
|
|
|
|
ForcePull bool
|
2021-03-28 23:08:40 -05:00
|
|
|
Platform string
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDockerPullExecutor function to create a run executor for the container
|
|
|
|
func NewDockerPullExecutor(input NewDockerPullExecutorInput) common.Executor {
|
2020-02-07 00:17:58 -06:00
|
|
|
return func(ctx context.Context) error {
|
|
|
|
logger := common.Logger(ctx)
|
2020-02-23 17:01:25 -06:00
|
|
|
logger.Debugf("%sdocker pull %v", logPrefix, input.Image)
|
2019-01-12 22:45:25 -06:00
|
|
|
|
2020-02-07 00:17:58 -06:00
|
|
|
if common.Dryrun(ctx) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
pull := input.ForcePull
|
|
|
|
if !pull {
|
2021-03-28 23:08:40 -05:00
|
|
|
imageExists, err := ImageExistsLocally(ctx, input.Image, input.Platform)
|
2020-02-07 00:17:58 -06:00
|
|
|
log.Debugf("Image exists? %v", imageExists)
|
|
|
|
if err != nil {
|
2021-03-28 23:08:40 -05:00
|
|
|
return errors.WithMessagef(err, "unable to determine if image already exists for image %q (%s)", input.Image, input.Platform)
|
2020-02-07 00:17:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if !imageExists {
|
|
|
|
pull = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !pull {
|
2019-01-12 22:45:25 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
imageRef := cleanImage(input.Image)
|
2021-03-28 23:08:40 -05:00
|
|
|
logger.Debugf("pulling image '%v' (%s)", imageRef, input.Platform)
|
2019-01-12 22:45:25 -06:00
|
|
|
|
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-03-28 23:08:40 -05:00
|
|
|
reader, err := cli.ImagePull(ctx, imageRef, types.ImagePullOptions{
|
|
|
|
Platform: input.Platform,
|
|
|
|
})
|
2020-02-07 00:17:58 -06:00
|
|
|
_ = logDockerResponse(logger, reader, err != nil)
|
2019-01-12 22:45:25 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func cleanImage(image string) string {
|
|
|
|
imageParts := len(strings.Split(image, "/"))
|
|
|
|
if imageParts == 1 {
|
|
|
|
image = fmt.Sprintf("docker.io/library/%s", image)
|
|
|
|
} else if imageParts == 2 {
|
|
|
|
image = fmt.Sprintf("docker.io/%s", image)
|
|
|
|
}
|
|
|
|
|
|
|
|
return image
|
|
|
|
}
|