Skip to content

Commit

Permalink
Adds option to download using AzCopy
Browse files Browse the repository at this point in the history
  • Loading branch information
dhadka committed Jun 4, 2020
1 parent 3e40dd3 commit fa95df2
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/cache-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ jobs:
run: |
node -e "Promise.resolve(require('./packages/cache/lib/cache').restoreCache(['test-cache','~/test-cache'],'test-${{ runner.os }}-${{ github.run_id }}'))"
- name: Restore cache using restoreCache() with AzCopy enabled
env:
USE_AZCOPY: true
run: |
node -e "Promise.resolve(require('./packages/cache/lib/cache').restoreCache(['test-cache','~/test-cache'],'test-${{ runner.os }}-${{ github.run_id }}'))"
- name: Verify cache
shell: bash
run: |
Expand Down
31 changes: 29 additions & 2 deletions packages/cache/src/internal/cacheHttpClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as core from '@actions/core'
import {exec} from '@actions/exec'
import {HttpClient, HttpCodes} from '@actions/http-client'
import {BearerCredentialHandler} from '@actions/http-client/auth'
import {
Expand All @@ -9,6 +10,7 @@ import {
import * as crypto from 'crypto'
import * as fs from 'fs'
import * as stream from 'stream'
import {URL} from 'url'
import * as util from 'util'

import * as utils from './cacheUtils'
Expand Down Expand Up @@ -220,7 +222,7 @@ async function pipeResponseToStream(
await pipeline(response.message, output)
}

export async function downloadCache(
async function downloadCacheHttpClient(
archiveLocation: string,
archivePath: string
): Promise<void> {
Expand Down Expand Up @@ -256,6 +258,31 @@ export async function downloadCache(
}
}

export async function downloadCache(
archiveLocation: string,
archivePath: string
): Promise<void> {
const archiveUrl = new URL(archiveLocation)
const useAzCopy = process.env['USE_AZCOPY'] ?? ''

// Use AzCopy to download caches hosted on Azure to improve reliability.
if (
archiveUrl.hostname.endsWith('.blob.core.windows.net') &&
useAzCopy == 'true'
) {
const command = utils.getAzCopyCommand();

if (command) {
core.info(`Downloading cache using ${command}...`)
await exec('azcopy', ['copy', archiveLocation, archivePath])
return
}
}

// Otherwise, download using the Actions http-client.
await downloadCacheHttpClient(archiveLocation, archivePath)
}

// Reserve Cache
export async function reserveCache(
key: string,
Expand Down Expand Up @@ -360,7 +387,7 @@ async function uploadFile(
})
.on('error', error => {
throw new Error(
`Cache upload failed because file read failed with ${error.Message}`
`Cache upload failed because file read failed with ${error.message}`
)
}),
start,
Expand Down
11 changes: 11 additions & 0 deletions packages/cache/src/internal/cacheUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,14 @@ export async function isGnuTarInstalled(): Promise<boolean> {
const versionOutput = await getVersion('tar')
return versionOutput.toLowerCase().includes('gnu tar')
}

export async function getAzCopyCommand(): Promise<string | undefined> {
// On Ubuntu, azcopy points to an earlier version, so prefer the azcopy10 alias
if ((await getVersion('azcopy10')).toLowerCase().includes('azcopy')) {
return 'azcopy10'
} else if ((await getVersion('azcopy')).toLowerCase().includes('azcopy')) {
return 'azcopy'
} else {
return undefined
}
}

0 comments on commit fa95df2

Please sign in to comment.