Push pushes an artifact from the content store to an OCI registry
(ctx context.Context, reference string)
| 15 | |
| 16 | // Push pushes an artifact from the content store to an OCI registry |
| 17 | func Push(ctx context.Context, reference string) error { |
| 18 | store, err := content.NewStore() |
| 19 | if err != nil { |
| 20 | return fmt.Errorf("creating content store: %w", err) |
| 21 | } |
| 22 | |
| 23 | img, err := store.GetArtifactImage(reference) |
| 24 | if err != nil { |
| 25 | return fmt.Errorf("loading artifact from store: %w", err) |
| 26 | } |
| 27 | |
| 28 | // Get metadata to restore annotations |
| 29 | metadata, err := store.GetArtifactMetadata(reference) |
| 30 | if err != nil { |
| 31 | return fmt.Errorf("loading artifact metadata: %w", err) |
| 32 | } |
| 33 | |
| 34 | // Convert to OCI format and restore annotations if present |
| 35 | if len(metadata.Annotations) > 0 { |
| 36 | img = mutate.MediaType(img, types.OCIManifestSchema1) |
| 37 | img = mutate.Annotations(img, metadata.Annotations).(v1.Image) |
| 38 | } |
| 39 | |
| 40 | // Wrap as a spec-compliant OCI artifact so the pushed manifest includes |
| 41 | // artifactType and an empty config descriptor. |
| 42 | img = content.NewArtifactImage(img, "application/vnd.docker.agent.config.v1+json") |
| 43 | |
| 44 | ref, err := name.ParseReference(reference) |
| 45 | if err != nil { |
| 46 | return fmt.Errorf("parsing registry reference %s: %w", reference, err) |
| 47 | } |
| 48 | |
| 49 | if err := crane.Push(img, ref.String(), crane.WithContext(ctx), crane.WithTransport(NewTransport(ctx))); err != nil { |
| 50 | return fmt.Errorf("pushing image to registry %s: %w", reference, err) |
| 51 | } |
| 52 | |
| 53 | return nil |
| 54 | } |