2019-01-13 12:45:25 +08:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
2020-02-07 14:17:58 +08:00
|
|
|
"context"
|
2021-05-06 00:37:17 +08:00
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
2022-06-11 05:16:42 +08:00
|
|
|
"fmt"
|
2019-01-13 12:45:25 +08:00
|
|
|
|
2021-11-14 03:35:45 +08:00
|
|
|
"github.com/docker/distribution/reference"
|
2019-01-13 12:45:25 +08:00
|
|
|
"github.com/docker/docker/api/types"
|
2020-02-07 14:17:58 +08:00
|
|
|
log "github.com/sirupsen/logrus"
|
2021-03-29 12:08:40 +08:00
|
|
|
|
|
|
|
"github.com/nektos/act/pkg/common"
|
2019-01-13 12:45:25 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewDockerPullExecutorInput the input for the NewDockerPullExecutor function
|
|
|
|
type NewDockerPullExecutorInput struct {
|
2020-02-07 14:17:58 +08:00
|
|
|
Image string
|
|
|
|
ForcePull bool
|
2021-03-29 12:08:40 +08:00
|
|
|
Platform string
|
2021-05-06 00:37:17 +08:00
|
|
|
Username string
|
|
|
|
Password string
|
2019-01-13 12:45:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDockerPullExecutor function to create a run executor for the container
|
|
|
|
func NewDockerPullExecutor(input NewDockerPullExecutorInput) common.Executor {
|
2020-02-07 14:17:58 +08:00
|
|
|
return func(ctx context.Context) error {
|
|
|
|
logger := common.Logger(ctx)
|
2020-02-24 07:01:25 +08:00
|
|
|
logger.Debugf("%sdocker pull %v", logPrefix, input.Image)
|
2019-01-13 12:45:25 +08:00
|
|
|
|
2020-02-07 14:17:58 +08:00
|
|
|
if common.Dryrun(ctx) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
pull := input.ForcePull
|
|
|
|
if !pull {
|
2021-03-29 12:08:40 +08:00
|
|
|
imageExists, err := ImageExistsLocally(ctx, input.Image, input.Platform)
|
2020-02-07 14:17:58 +08:00
|
|
|
log.Debugf("Image exists? %v", imageExists)
|
|
|
|
if err != nil {
|
2022-06-11 05:16:42 +08:00
|
|
|
return fmt.Errorf("unable to determine if image already exists for image '%s' (%s): %w", input.Image, input.Platform, err)
|
2020-02-07 14:17:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if !imageExists {
|
|
|
|
pull = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !pull {
|
2019-01-13 12:45:25 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
imageRef := cleanImage(input.Image)
|
2021-03-29 12:08:40 +08:00
|
|
|
logger.Debugf("pulling image '%v' (%s)", imageRef, input.Platform)
|
2019-01-13 12:45:25 +08:00
|
|
|
|
2020-05-04 12:15:42 +08:00
|
|
|
cli, err := GetDockerClient(ctx)
|
2019-01-13 12:45:25 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-10-25 00:50:43 +08:00
|
|
|
defer cli.Close()
|
2019-01-13 12:45:25 +08:00
|
|
|
|
2021-05-06 00:37:17 +08:00
|
|
|
imagePullOptions, err := getImagePullOptions(ctx, input)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
reader, err := cli.ImagePull(ctx, imageRef, imagePullOptions)
|
|
|
|
|
2020-02-07 14:17:58 +08:00
|
|
|
_ = logDockerResponse(logger, reader, err != nil)
|
2019-01-13 12:45:25 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-06 00:37:17 +08:00
|
|
|
func getImagePullOptions(ctx context.Context, input NewDockerPullExecutorInput) (types.ImagePullOptions, error) {
|
|
|
|
imagePullOptions := types.ImagePullOptions{
|
|
|
|
Platform: input.Platform,
|
|
|
|
}
|
2021-11-28 02:21:32 +08:00
|
|
|
|
2021-05-06 00:37:17 +08:00
|
|
|
if input.Username != "" && input.Password != "" {
|
|
|
|
logger := common.Logger(ctx)
|
|
|
|
logger.Debugf("using authentication for docker pull")
|
|
|
|
|
|
|
|
authConfig := types.AuthConfig{
|
|
|
|
Username: input.Username,
|
|
|
|
Password: input.Password,
|
|
|
|
}
|
|
|
|
|
|
|
|
encodedJSON, err := json.Marshal(authConfig)
|
|
|
|
if err != nil {
|
|
|
|
return imagePullOptions, err
|
|
|
|
}
|
|
|
|
|
2021-11-28 02:21:32 +08:00
|
|
|
imagePullOptions.RegistryAuth = base64.URLEncoding.EncodeToString(encodedJSON)
|
|
|
|
} else {
|
|
|
|
authConfig, err := LoadDockerAuthConfig(input.Image)
|
|
|
|
if err != nil {
|
|
|
|
return imagePullOptions, err
|
|
|
|
}
|
|
|
|
if authConfig.Username == "" && authConfig.Password == "" {
|
|
|
|
return imagePullOptions, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
encodedJSON, err := json.Marshal(authConfig)
|
|
|
|
if err != nil {
|
|
|
|
return imagePullOptions, err
|
|
|
|
}
|
|
|
|
|
2021-05-06 00:37:17 +08:00
|
|
|
imagePullOptions.RegistryAuth = base64.URLEncoding.EncodeToString(encodedJSON)
|
|
|
|
}
|
|
|
|
|
|
|
|
return imagePullOptions, nil
|
|
|
|
}
|
|
|
|
|
2019-01-13 12:45:25 +08:00
|
|
|
func cleanImage(image string) string {
|
2021-11-14 03:35:45 +08:00
|
|
|
ref, err := reference.ParseAnyReference(image)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return ""
|
2019-01-13 12:45:25 +08:00
|
|
|
}
|
|
|
|
|
2021-11-14 03:35:45 +08:00
|
|
|
return ref.String()
|
2019-01-13 12:45:25 +08:00
|
|
|
}
|