export async function run()

in Tasks/UniversalPackagesV0/universalpublish.ts [15:173]


export async function run(artifactToolPath: string): Promise<void> {
    const buildIdentityDisplayName: string = null;
    const buildIdentityAccount: string = null;
    try {
        // Get directory to publish
        const publishDir: string = tl.getInput("publishDirectory");
        if (publishDir.length < 1)
        {
            tl.debug(tl.loc("Info_PublishDirectoryNotFound"));
            return;
        }

        let serviceUri: string;
        let feedId: string;
        let projectId: string;
        let packageName: string;
        let version: string;
        let accessToken: string;
        let feedUri: string;
        let execResult: IExecSyncResult;
        const publishedPackageVar: string = tl.getInput("publishedPackageVar");
        const versionRadio = tl.getInput("versionPublishSelector");

        // Feed Auth
        let feedType = tl.getInput("internalOrExternalPublish") || "internal";

        const normalizedFeedType = ["internal", "external"].find((x) =>
            feedType.toUpperCase() === x.toUpperCase());
        if (!normalizedFeedType) {
            throw new Error(tl.loc("UnknownFeedType", feedType));
        }
        feedType = normalizedFeedType;

        let internalAuthInfo: auth.InternalAuthInfo;

        const toolRunnerOptions = artifactToolRunner.getOptions();

        let sessionId: string;

        [serviceUri, packageName, feedId, projectId, accessToken] = authSetup(feedType);

        if (feedType === "internal")
        {
            internalAuthInfo = new auth.InternalAuthInfo([], accessToken);

            toolRunnerOptions.env.UNIVERSAL_PUBLISH_PAT = internalAuthInfo.accessToken;

            let packagingLocation: string;
            try {
                // This call is to get the packaging URI(abc.pkgs.vs.com) which is same for all protocols.
                packagingLocation = await pkgLocationUtils.getNuGetUriFromBaseServiceUri(
                    serviceUri,
                    accessToken);
            } catch (error) {
                logError(error);
                packagingLocation = serviceUri;
            }

            const pkgConn = pkgLocationUtils.getWebApiWithProxy(packagingLocation, accessToken);
            sessionId = await ProvenanceHelper.GetSessionId(
                feedId,
                projectId,
                "upack", /* must match protocol name on the server */
                pkgConn.serverUrl,
                [pkgConn.authHandler],
                pkgConn.options);
        }
        else {
            //Catch the no external point error
            if (!serviceUri) {
                return
            }

            toolRunnerOptions.env.UNIVERSAL_PUBLISH_PAT = accessToken;
        }

        if (versionRadio === "custom"){
            version = tl.getInput("versionPublish");
        }
        else{
            feedUri = await pkgLocationUtils.getFeedUriFromBaseServiceUri(serviceUri, accessToken);
            version = await getNextPackageVersion(feedUri, accessToken, projectId, feedId, packageName);
        }
        tl.debug(tl.loc("Info_UsingArtifactToolPublish"));

        if (sessionId != null) {
            feedId = sessionId;
        }

        // tslint:disable-next-line:no-object-literal-type-assertion
        const publishOptions = {
            artifactToolPath,
            projectId,
            feedId,
            accountUrl: serviceUri,
            packageName,
            packageVersion: version,
        } as artifactToolRunner.IArtifactToolOptions;

        execResult = publishPackageUsingArtifactTool(publishDir, publishOptions, toolRunnerOptions);

        var retries = 0;
        let newVersion: string;

        //If package already exist, retry with a newer version, do not retry for custom version
        while (retries < numRetries && execResult != null && execResult.code === packageAlreadyExistsError && versionRadio !== "custom") {
            [serviceUri, packageName, feedId, projectId, accessToken] = authSetup(feedType);
            if (feedType == "internal") {
                internalAuthInfo = new auth.InternalAuthInfo([], accessToken);
                toolRunnerOptions.env.UNIVERSAL_PUBLISH_PAT = internalAuthInfo.accessToken;
            }

            else {
                //Catch the no external point error
                if (!serviceUri) {
                    return
                }
                toolRunnerOptions.env.UNIVERSAL_PUBLISH_PAT = accessToken;
            }

            feedUri = await pkgLocationUtils.getFeedUriFromBaseServiceUri(serviceUri, accessToken);
            newVersion = await getNextPackageVersion(feedUri, accessToken, projectId, feedId, packageName);

            const publishOptions = {
                artifactToolPath,
                projectId,
                feedId,
                accountUrl: serviceUri,
                packageName,
                packageVersion: newVersion,
            } as artifactToolRunner.IArtifactToolOptions;
            tl.debug(tl.loc("Info_PublishingRetry", publishOptions.packageName, version, newVersion));
            execResult = publishPackageUsingArtifactTool(publishDir, publishOptions, toolRunnerOptions);
            version = newVersion;
            retries++; 
        }

        if (execResult != null && execResult.code === packageAlreadyExistsError) {
            telemetry.logResult("Packaging", "UniversalPackagesCommand", execResult.code);
            throw new Error(tl.loc("Error_UnexpectedErrorArtifactTool",
                execResult.code,
                execResult.stderr ? execResult.stderr.trim() : execResult.stderr));
        }

        if (publishedPackageVar) {
            tl.setVariable(publishedPackageVar, `${packageName} ${version}`);
        }

        tl.setResult(tl.TaskResult.Succeeded, tl.loc("PackagesPublishedSuccessfully"));
    } catch (err) {
        tl.error(err);

        if (buildIdentityDisplayName || buildIdentityAccount) {
            tl.warning(tl.loc("BuildIdentityPermissionsHint", buildIdentityDisplayName, buildIdentityAccount));
        }

        tl.setResult(tl.TaskResult.Failed, tl.loc("PackagesFailedToPublish"));
    }
}