Add retry for uploading artifact (#13)

This commit is contained in:
Nick Cipollo
2020-03-05 11:39:02 -05:00
committed by GitHub
parent d57d57cad2
commit 2ea88b1f32
8 changed files with 238 additions and 222 deletions

View File

@@ -2,53 +2,58 @@ import * as core from '@actions/core';
import { Artifact } from "./Artifact";
import { Releases } from "./Releases";
import { ReposListAssetsForReleaseResponseItem } from "@octokit/rest";
import { ErrorMessage } from './ErrorMessage';
export interface ArtifactUploader {
uploadArtifacts(artifacts: Artifact[], releaseId: number, uploadUrl: string): Promise<void>
}
export class GithubArtifactUploader implements ArtifactUploader {
private releases: Releases
private replacesExistingArtifacts: boolean = true
constructor(releases: Releases, replacesExistingArtifacts: boolean) {
this.releases = releases
this.replacesExistingArtifacts = replacesExistingArtifacts
constructor(
private releases: Releases,
private replacesExistingArtifacts: boolean = true,
) {
}
async uploadArtifacts(artifacts: Artifact[],
releaseId: number,
uploadUrl: string): Promise<void> {
if(this.replacesExistingArtifacts) {
if (this.replacesExistingArtifacts) {
await this.deleteUpdatedArtifacts(artifacts, releaseId)
}
for (const artifact of artifacts) {
try {
await this.releases.uploadArtifact(uploadUrl,
artifact.contentLength,
artifact.contentType,
artifact.readFile(),
artifact.name)
} catch (error) {
const message = `Failed to upload artifact ${artifact.name}. Does it already exist?`
core.warning(message)
}
await this.uploadArtifact(artifact, uploadUrl)
}
return Promise.resolve()
}
async deleteUpdatedArtifacts(artifacts: Artifact[], releaseId: number) {
const response = await this.releases.listArtifactsForRelease(releaseId)
private async uploadArtifact(artifact: Artifact, uploadUrl: string, retry = 3) {
try {
core.debug(`Uploading artifact ${artifact.name}...`)
await this.releases.uploadArtifact(uploadUrl,
artifact.contentLength,
artifact.contentType,
artifact.readFile(),
artifact.name)
} catch (error) {
if (error.status >= 500 && retry > 0) {
core.warning(`Failed to upload artifact ${artifact.name}. ${error.message}. Retrying...`)
await this.uploadArtifact(artifact, uploadUrl, retry - 1)
} else {
core.warning(`Failed to upload artifact ${artifact.name}. ${error.message}.`)
}
}
}
private async deleteUpdatedArtifacts(artifacts: Artifact[], releaseId: number): Promise<void> {
const response = await this.releases.listArtifactsForRelease(releaseId)
const releaseAssets = response.data
const assetByName = new Map<string, ReposListAssetsForReleaseResponseItem>()
const assetByName: Record<string, ReposListAssetsForReleaseResponseItem> = {}
releaseAssets.forEach(asset => {
assetByName[asset.name] = asset
});
for (const artifact of artifacts) {
const asset = assetByName[artifact.name]
if (asset) {
core.debug(`Deleting exist artifact ${artifact.name}...`)
await this.releases.deleteArtifact(asset.id)
}
}