mirror of
https://code.forgejo.org/forgejo/runner.git
synced 2025-08-16 18:01:34 +00:00
Refs https://github.com/nektos/act/pull/3225 --- * added info log when container image platform mismatched * inline image architecture comparison into ImageExistsLocally function as per review request (cherry picked from commit 77d31c96a9486553908fdeb1c3053015238d5a0a) <!--start release-notes-assistant--> <!--URL:https://code.forgejo.org/forgejo/runner--> - features - [PR](https://code.forgejo.org/forgejo/runner/pulls/826): <!--number 826 --><!--line 0 --><!--description ZmVhdDogYWRkZWQgaW5mbyBsb2cgd2hlbiBjb250YWluZXIgaW1hZ2UgcGxhdGZvcm0gbWlzbWF0Y2hlZA==-->feat: added info log when container image platform mismatched<!--description--> <!--end release-notes-assistant--> Co-authored-by: Ryan Fleet <rmfleet@gmail.com> Reviewed-on: https://code.forgejo.org/forgejo/runner/pulls/826 Reviewed-by: Mathieu Fenniak <mfenniak@noreply.code.forgejo.org> Co-authored-by: Earl Warren <contact@earl-warren.org> Co-committed-by: Earl Warren <contact@earl-warren.org>
66 lines
1.7 KiB
Go
66 lines
1.7 KiB
Go
//go:build !(WITHOUT_DOCKER || !(linux || darwin || windows || netbsd))
|
|
|
|
package container
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"code.forgejo.org/forgejo/runner/v9/act/common"
|
|
cerrdefs "github.com/containerd/errdefs"
|
|
"github.com/docker/docker/api/types/image"
|
|
)
|
|
|
|
// ImageExistsLocally returns a boolean indicating if an image with the
|
|
// requested name, tag and architecture exists in the local docker image store
|
|
func ImageExistsLocally(ctx context.Context, imageName, platform string) (bool, error) {
|
|
cli, err := GetDockerClient(ctx)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
defer cli.Close()
|
|
|
|
inspectImage, err := cli.ImageInspect(ctx, imageName)
|
|
if cerrdefs.IsNotFound(err) {
|
|
return false, nil
|
|
} else if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
imagePlatform := fmt.Sprintf("%s/%s", inspectImage.Os, inspectImage.Architecture)
|
|
|
|
if platform == "" || platform == "any" || imagePlatform == platform {
|
|
return true, nil
|
|
}
|
|
|
|
logger := common.Logger(ctx)
|
|
logger.Infof("image found but platform does not match: %s (image) != %s (platform)\n", imagePlatform, platform)
|
|
|
|
return false, nil
|
|
}
|
|
|
|
// RemoveImage removes image from local store, the function is used to run different
|
|
// container image architectures
|
|
func RemoveImage(ctx context.Context, imageName string, force, pruneChildren bool) (bool, error) {
|
|
cli, err := GetDockerClient(ctx)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
defer cli.Close()
|
|
|
|
inspectImage, err := cli.ImageInspect(ctx, imageName)
|
|
if cerrdefs.IsNotFound(err) {
|
|
return false, nil
|
|
} else if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
if _, err = cli.ImageRemove(ctx, inspectImage.ID, image.RemoveOptions{
|
|
Force: force,
|
|
PruneChildren: pruneChildren,
|
|
}); err != nil {
|
|
return false, err
|
|
}
|
|
|
|
return true, nil
|
|
}
|