feat: retry git push

This commit is contained in:
Peter Evans 2021-06-02 14:52:10 +09:00
parent 29708c335a
commit b8d27464d1
4 changed files with 3011 additions and 2 deletions

2973
dist/index.js vendored

File diff suppressed because one or more lines are too long

View file

@ -35,6 +35,7 @@
"@octokit/plugin-paginate-rest": "2.14.0",
"@octokit/plugin-rest-endpoint-methods": "5.5.0",
"@octokit/plugin-retry": "3.0.7",
"bottleneck": "2.19.5",
"https-proxy-agent": "5.0.0",
"uuid": "8.3.2"
},

View file

@ -7,6 +7,7 @@ import {
import {GitHubHelper} from './github-helper'
import {GitCommandManager} from './git-command-manager'
import {GitAuthHelper} from './git-auth-helper'
import {pushWithRetry} from './git-push-retry'
import * as utils from './utils'
export interface Inputs {
@ -182,7 +183,7 @@ export async function createPullRequest(inputs: Inputs): Promise<void> {
core.startGroup(
`Pushing pull request branch to '${branchRemoteName}/${inputs.branch}'`
)
await git.push([
await pushWithRetry(git, [
'--force-with-lease',
branchRemoteName,
`HEAD:refs/heads/${inputs.branch}`

36
src/git-push-retry.ts Normal file
View file

@ -0,0 +1,36 @@
import Bottleneck from 'bottleneck'
import * as core from '@actions/core'
import {GitCommandManager} from './git-command-manager'
const retryableErrors = [
'You have triggered an abuse detection mechanism and have been temporarily blocked from content creation. Please retry your request again later.'
]
const maxRetries = 3
const waitMilliseconds = 60000
const limiter = new Bottleneck()
limiter.on('failed', async (error, jobInfo) => {
const id = jobInfo.options.id
core.info(`Job '${id}' failed: ${error}`)
if (error.message in retryableErrors && jobInfo.retryCount < maxRetries) {
core.info(`Retrying job '${id}' in ${waitMilliseconds}ms`)
return waitMilliseconds + randomFromInterval(0, 10000)
}
})
limiter.on('retry', (error, jobInfo) =>
core.info(`Now retrying job '${jobInfo.options.id}'`)
)
function randomFromInterval(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min)
}
export async function pushWithRetry(
git: GitCommandManager,
options: string[]
): Promise<void> {
await limiter.schedule({id: 'git push'}, () => git.push(options))
}