Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always pull the container image #272

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 43 additions & 48 deletions internal/infra/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,65 +463,60 @@ func putCloneDir(ctx context.Context, cli *client.Client, updater *Updater, dir
return nil
}

// Pull the image from the registry.
// Always pull the image, even if it's already present locally.
// This is because container image tags are mutable, and we always want to ensure we have the latest version.
func pullImage(ctx context.Context, cli *client.Client, image string) error {
var inspect types.ImageInspect

// check if image exists locally
inspect, _, err := cli.ImageInspectWithRaw(ctx, image)

// pull image if necessary
if err != nil {
Comment on lines -467 to -473
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This diff is horrible, but I essentially removed these 3 lines and decreased the indentation below.

var imagePullOptions types.ImagePullOptions

if strings.HasPrefix(image, "ghcr.io/") {

token := os.Getenv("LOCAL_GITHUB_ACCESS_TOKEN")
if token != "" {
auth := base64.StdEncoding.EncodeToString([]byte("x:" + token))
imagePullOptions = types.ImagePullOptions{
RegistryAuth: fmt.Sprintf("Basic %s", auth),
}
} else {
log.Println("Failed to find credentials for GitHub container registry.")
var imagePullOptions types.ImagePullOptions

if strings.HasPrefix(image, "ghcr.io/") {
token := os.Getenv("LOCAL_GITHUB_ACCESS_TOKEN")
if token != "" {
auth := base64.StdEncoding.EncodeToString([]byte("x:" + token))
imagePullOptions = types.ImagePullOptions{
RegistryAuth: fmt.Sprintf("Basic %s", auth),
}
} else if strings.Contains(image, ".azurecr.io/") {
username := os.Getenv("AZURE_REGISTRY_USERNAME")
password := os.Getenv("AZURE_REGISTRY_PASSWORD")
} else {
log.Println("Failed to find credentials for GitHub container registry.")
}
} else if strings.Contains(image, ".azurecr.io/") {
username := os.Getenv("AZURE_REGISTRY_USERNAME")
password := os.Getenv("AZURE_REGISTRY_PASSWORD")

registryName := strings.Split(image, "/")[0]
registryName := strings.Split(image, "/")[0]

if username != "" && password != "" {
authConfig := registry.AuthConfig{
Username: username,
Password: password,
ServerAddress: registryName,
}
if username != "" && password != "" {
authConfig := registry.AuthConfig{
Username: username,
Password: password,
ServerAddress: registryName,
}

encodedJSON, _ := json.Marshal(authConfig)
authStr := base64.URLEncoding.EncodeToString(encodedJSON)
encodedJSON, _ := json.Marshal(authConfig)
authStr := base64.URLEncoding.EncodeToString(encodedJSON)

imagePullOptions = types.ImagePullOptions{
RegistryAuth: authStr,
}
} else {
log.Println("Failed to find credentials for Azure container registry.")
imagePullOptions = types.ImagePullOptions{
RegistryAuth: authStr,
}
} else {
log.Printf("Failed to find credentials for pulling image: %s\n.", image)
log.Println("Failed to find credentials for Azure container registry.")
}
} else {
log.Printf("Failed to find credentials for pulling image: %s\n.", image)
}

log.Printf("pulling image: %s\n", image)
out, err := cli.ImagePull(ctx, image, imagePullOptions)
if err != nil {
return fmt.Errorf("failed to pull %v: %w", image, err)
}
_, _ = io.Copy(io.Discard, out)
out.Close()
log.Printf("pulling image: %s\n", image)
out, err := cli.ImagePull(ctx, image, imagePullOptions)
if err != nil {
return fmt.Errorf("failed to pull %v: %w", image, err)
}
_, _ = io.Copy(io.Discard, out)
out.Close()

inspect, _, err = cli.ImageInspectWithRaw(ctx, image)
if err != nil {
return fmt.Errorf("failed to inspect %v: %w", image, err)
}
var inspect types.ImageInspect
inspect, _, err = cli.ImageInspectWithRaw(ctx, image)
if err != nil {
return fmt.Errorf("failed to inspect %v: %w", image, err)
}

log.Printf("using image %v at %s\n", image, inspect.ID)
Expand Down
Loading