public override async Task RunAsync(CancellationToken cancellationToken)
 {
     await foreach (var manifest in _repository.GetAllManifestPropertiesAsync())
     {
         _client.GetArtifact($"library/node", manifest.Digest);
     }
 }
Example #2
0
        public async Task DeleteImagesAsync()
        {
            Environment.SetEnvironmentVariable("REGISTRY_ENDPOINT", TestEnvironment.Endpoint);

            #region Snippet:ContainerRegistry_Tests_Samples_DeleteImageAsync
#if SNIPPET
            using System.Linq;
            using Azure.Containers.ContainerRegistry;
            using Azure.Identity;
#endif

            // Get the service endpoint from the environment
            Uri endpoint = new Uri(Environment.GetEnvironmentVariable("REGISTRY_ENDPOINT"));

            // Create a new ContainerRegistryClient
            ContainerRegistryClient client = new ContainerRegistryClient(endpoint, new DefaultAzureCredential(),
                                                                         new ContainerRegistryClientOptions()
            {
                Audience = ContainerRegistryAudience.AzureResourceManagerPublicCloud
            });

            // Iterate through repositories
            AsyncPageable <string> repositoryNames = client.GetRepositoryNamesAsync();
            await foreach (string repositoryName in repositoryNames)
            {
                ContainerRepository repository = client.GetRepository(repositoryName);

                // Obtain the images ordered from newest to oldest
                AsyncPageable <ArtifactManifestProperties> imageManifests =
                    repository.GetAllManifestPropertiesAsync(manifestOrder: ArtifactManifestOrder.LastUpdatedOnDescending);

                // Delete images older than the first three.
                await foreach (ArtifactManifestProperties imageManifest in imageManifests.Skip(3))
                {
                    RegistryArtifact image = repository.GetArtifact(imageManifest.Digest);
                    Console.WriteLine($"Deleting image with digest {imageManifest.Digest}.");
                    Console.WriteLine($"   Deleting the following tags from the image: ");
                    foreach (var tagName in imageManifest.Tags)
                    {
                        Console.WriteLine($"        {imageManifest.RepositoryName}:{tagName}");
                        await image.DeleteTagAsync(tagName);
                    }
                    await image.DeleteAsync();
                }
            }

            #endregion
        }