@@ -236,6 +236,29 @@ function getStsClient(region) {
236236 } ) ;
237237}
238238
239+ let defaultSleep = function ( ms ) {
240+ return new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ;
241+ } ;
242+ let sleep = defaultSleep ;
243+
244+ // retryAndBackoff retries with exponential backoff the promise if the error isRetryable upto maxRetries time.
245+ const retryAndBackoff = async ( fn , isRetryable , retries = 0 , maxRetries = 12 , base = 50 ) => {
246+ try {
247+ return await fn ( ) ;
248+ } catch ( err ) {
249+ if ( ! isRetryable ) {
250+ throw err ;
251+ }
252+ // It's retryable, so sleep and retry.
253+ await sleep ( Math . random ( ) * ( Math . pow ( 2 , retries ) * base ) ) ;
254+ retries += 1 ;
255+ if ( retries === maxRetries ) {
256+ throw err ;
257+ }
258+ return await retryAndBackoff ( fn , isRetryable , retries , maxRetries , base ) ;
259+ }
260+ }
261+
239262async function run ( ) {
240263 try {
241264 // Get inputs
@@ -303,17 +326,18 @@ async function run() {
303326
304327 // Get role credentials if configured to do so
305328 if ( roleToAssume ) {
306- const roleCredentials = await assumeRole ( {
307- sourceAccountId,
308- region,
309- roleToAssume,
310- roleExternalId,
311- roleDurationSeconds,
312- roleSessionName,
313- roleSkipSessionTagging,
314- webIdentityTokenFile,
315- webIdentityToken
316- } ) ;
329+ const roleCredentials = await retryAndBackoff (
330+ async ( ) => { return await assumeRole ( {
331+ sourceAccountId,
332+ region,
333+ roleToAssume,
334+ roleExternalId,
335+ roleDurationSeconds,
336+ roleSessionName,
337+ roleSkipSessionTagging,
338+ webIdentityTokenFile,
339+ webIdentityToken
340+ } ) } , true ) ;
317341 exportCredentials ( roleCredentials ) ;
318342 // We need to validate the credentials in 2 of our use-cases
319343 // First: self-hosted runners. If the GITHUB_ACTIONS environment variable
@@ -337,7 +361,14 @@ async function run() {
337361 }
338362}
339363
340- module . exports = run ;
364+ exports . withSleep = function ( s ) {
365+ sleep = s ;
366+ } ;
367+ exports . reset = function ( ) {
368+ sleep = defaultSleep ;
369+ } ;
370+
371+ exports . run = run
341372
342373/* istanbul ignore next */
343374if ( require . main === module ) {
0 commit comments