コード例 #1
0
                /// <summary>
                /// Pushes a set of packages from .nupkg files that must exist in <see cref="CheckRepositoryInfo.ReleasesFolder"/>.
                /// </summary>
                /// <param name="pushes">The instances to push (that necessary target this feed).</param>
                /// <returns>The awaitable.</returns>
                public override async Task PushAsync(IEnumerable <ArtifactPush> pushes)
                {
                    string apiKey = null;

                    if (!_packageSource.IsLocal)
                    {
                        apiKey = ResolveAPIKey();
                        if (string.IsNullOrEmpty(apiKey))
                        {
                            Cake.Information($"Could not resolve API key. Push to '{Name}' => '{Url}' is skipped.");
                            return;
                        }
                    }
                    Cake.Information($"Pushing packages to '{Name}' => '{Url}'.");
                    ILogger logger = InitializeAndGetLogger(Cake);
                    PackageUpdateResource updater = await _updater;

                    foreach (ArtifactPush p in pushes)
                    {
                        string         packageString = p.Name + "." + p.Version.WithBuildMetaData(null).ToNormalizedString();
                        NormalizedPath fullPath      = ArtifactType.GlobalInfo.ReleasesFolder.AppendPart(packageString + ".nupkg");
                        await updater.Push(
                            fullPath,
                            string.Empty, // no Symbol source.
                            20,           //20 seconds timeout
                            disableBuffering : false,
                            getApiKey : endpoint => apiKey,
                            getSymbolApiKey : symbolsEndpoint => null,
                            noServiceEndpoint : false,
                            log : logger);
                    }
                    await OnAllArtifactsPushed(pushes);
                }
コード例 #2
0
        private static async Task PushPackageAsync()
        {
            try
            {
                // This code region is referenced by the NuGet docs. Please update the docs if you rename the region
                // or move it to a different file.
                #region PushPackage
                ILogger           logger            = NullLogger.Instance;
                CancellationToken cancellationToken = CancellationToken.None;

                SourceCacheContext    cache      = new SourceCacheContext();
                SourceRepository      repository = Repository.Factory.GetCoreV3("https://api.nuget.org/v3/index.json");
                PackageUpdateResource resource   = await repository.GetResourceAsync <PackageUpdateResource>();

                string apiKey = "my-api-key";

                await resource.Push(
                    "MyPackage.nupkg",
                    symbolSource : null,
                    timeoutInSecond : 5 * 60,
                    disableBuffering : false,
                    getApiKey : packageSource => apiKey,
                    getSymbolApiKey : packageSource => null,
                    noServiceEndpoint : false,
                    skipDuplicate : false,
                    symbolPackageUpdateResource : null,
                    logger);

                #endregion
            }
            catch (Exception e)
            {
                Console.WriteLine($"Pushing package failed: {e}");
            }
        }
コード例 #3
0
 public static async Task UploadPackageAsync
 (
     PackageUpdateResource uploadResource,
     bool noServiceEndpoint,
     string packagePath,
     string apiKey,
     SymbolPackageUpdateResourceV3 symbolsResource
 )
 {
     if (symbolsResource is not null && IsValidSymbols(packagePath))
     {
         await uploadResource.Push
         (
             packagePath, symbolsResource.SourceUri.ToString(), 600, false, _ => apiKey, _ => apiKey,
             noServiceEndpoint, true, symbolsResource,
             NukeLoggerAdapter.Instance
         );
     }
コード例 #4
0
        private static async Task PushPackageAsync(
            PackageUpdateResource packageUpdateResource,
            PackageInfo package,
            string apiKey)
        {
            for (var attempt = 1; attempt <= _maxRetryCount; attempt++)
            {
                // Fail fast if a parallel push operation has already failed
                _packagePushCancellationTokenSource.Token.ThrowIfCancellationRequested();

                Log.WriteInformation($"Attempting to publish package {package.Identity} (Attempt: {attempt})");

                try
                {
                    await packageUpdateResource.Push(
                        package.PackagePath,
                        symbolSource : null,
                        timeoutInSecond : (int)_packagePushTimeout.TotalSeconds,
                        disableBuffering : false,
                        getApiKey : _ => apiKey,
                        getSymbolApiKey : _ => null,
                        log : NullLogger.Instance);

                    Log.WriteInformation($"Done publishing package {package.Identity}");
                    return;
                }
                catch (Exception ex) when(attempt < _maxRetryCount)  // allow exception to be thrown at the last attempt
                {
                    // Write in a single call as multiple WriteLine statements can get interleaved causing
                    // confusion when reading logs.
                    Log.WriteInformation(
                        $"Attempt {attempt} failed to publish package {package.Identity}." +
                        Environment.NewLine +
                        ex.ToString() +
                        Environment.NewLine +
                        "Retrying...");
                }
                catch
                {
                    _packagePushCancellationTokenSource.Cancel();
                    throw;
                }
            }
        }
コード例 #5
0
        public static async Task Run(
            ISettings settings,
            IPackageSourceProvider sourceProvider,
            string packagePath,
            string source,
            string apiKey,
            int timeoutSeconds,
            bool disableBuffering,
            bool noSymbols,
            ILogger logger)
        {
            source = CommandRunnerUtility.ResolveSource(sourceProvider, source);

            if (timeoutSeconds == 0)
            {
                timeoutSeconds = 5 * 60;
            }

            PackageUpdateResource packageUpdateResource = await CommandRunnerUtility.GetPackageUpdateResource(sourceProvider, source);

            // only push to SymbolSource when the actual package is being pushed to the official NuGet.org
            string symbolsSource = string.Empty;

            Uri sourceUri = packageUpdateResource.SourceUri;

            if (!noSymbols && !sourceUri.IsFile && sourceUri.IsAbsoluteUri)
            {
                if (sourceUri.Host.Equals(NuGetConstants.NuGetHostName, StringComparison.OrdinalIgnoreCase) || // e.g. nuget.org
                    sourceUri.Host.EndsWith("." + NuGetConstants.NuGetHostName, StringComparison.OrdinalIgnoreCase))    // *.nuget.org, e.g. www.nuget.org
                {
                    symbolsSource = NuGetConstants.DefaultSymbolServerUrl;
                }
            }

            await packageUpdateResource.Push(
                packagePath,
                symbolsSource,
                timeoutSeconds,
                disableBuffering,
                endpoint => CommandRunnerUtility.GetApiKey(settings, endpoint, apiKey),
                logger);
        }
コード例 #6
0
        private async Task PushPackageAsync(PackageUpdateResource packageUpdateResource, PackageInfo package)
        {
            for (var attempt = 1; attempt <= _maxRetryCount; attempt++)
            {
                // Fail fast if a parallel push operation has already failed
                _packagePushCancellationTokenSource.Token.ThrowIfCancellationRequested();

                Log.LogMessage($"Attempting to publish package {package.Identity} (Attempt: {attempt})");

                try
                {
                    await packageUpdateResource.Push(
                        package.PackagePath,
                        symbolSource : null,
                        timeoutInSecond : TimeoutSeconds,
                        disableBuffering : false,
                        getApiKey : _ => ApiKey,
                        getSymbolApiKey : _ => null,
                        noServiceEndpoint : false,
                        log : NullLogger.Instance);

                    Log.LogMessage(MessageImportance.High, $"Published package {package.Identity}");

                    return;
                }
                catch (Exception ex) when(attempt < _maxRetryCount)  // allow exception to be thrown at the last attempt
                {
                    Log.LogMessage(
                        MessageImportance.High,
                        $"Attempt {attempt} failed to publish package {package.Identity}." +
                        Environment.NewLine +
                        ex +
                        Environment.NewLine +
                        "Retrying...");
                }
                catch
                {
                    _packagePushCancellationTokenSource.Cancel();
                    throw;
                }
            }
        }
コード例 #7
0
ファイル: Push.cs プロジェクト: ScriptBox21/NuGet-Entropy
        static async Task PushAsync(
            FindPackageByIdResource findPackageById,
            PackageUpdateResource packageUpdate,
            string apiKey,
            PackageIdentity identity,
            string nupkgPath,
            object pushedVersionsLock,
            Func <string, SemaphoreSlim> getIdSemaphore,
            Dictionary <string, Task <HashSet <NuGetVersion> > > pushedVersions,
            object consoleLock,
            bool allowRetry)
        {
            // Get the list of existing versions.
            Task <HashSet <NuGetVersion> > versionsTask;

            lock (pushedVersionsLock)
            {
                if (!pushedVersions.TryGetValue(identity.Id, out versionsTask))
                {
                    versionsTask = GetVersionsAsync(findPackageById, identity.Id, consoleLock);
                    pushedVersions.Add(identity.Id, versionsTask);
                }
            }

            var versions = await versionsTask;

            lock (pushedVersionsLock)
            {
                if (versions.Contains(identity.Version))
                {
                    return;
                }
            }

            Console.WriteLine($"  Pushing {identity.Id} {identity.Version.ToNormalizedString()}...");
            try
            {
                var idSemaphore = getIdSemaphore(identity.Id);
                await idSemaphore.WaitAsync();

                try
                {
                    await packageUpdate.Push(
                        nupkgPath,
                        symbolSource : string.Empty,
                        timeoutInSecond : 300,
                        disableBuffering : false,
                        getApiKey : _ => apiKey,
                        getSymbolApiKey : null,
                        noServiceEndpoint : false,
                        skipDuplicate : false,
                        symbolPackageUpdateResource : null,
                        log : NullLogger.Instance); // new ConsoleLogger());
                }
                finally
                {
                    idSemaphore.Release();
                }

                lock (pushedVersionsLock)
                {
                    versions.Add(identity.Version);
                }
            }
            catch (HttpRequestException ex) when(ex.Message.StartsWith("Response status code does not indicate success: 409 ") && allowRetry)
            {
                await PushAsync(
                    findPackageById,
                    packageUpdate,
                    apiKey,
                    identity,
                    nupkgPath,
                    pushedVersionsLock,
                    getIdSemaphore,
                    pushedVersions,
                    consoleLock,
                    allowRetry : false);
            }
            catch (Exception ex)
            {
                lock (consoleLock)
                {
                    Console.WriteLine($"  Push of {identity.Id} {identity.Version.ToNormalizedString()} ({new FileInfo(nupkgPath).Length} bytes) failed with exception:");
                    Console.WriteLine(ex);
                }
                return;
            }
        }