Skip to content

Commit

Permalink
buildx: improve vspec fingerprint for caching
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
  • Loading branch information
crazy-max committed Jul 6, 2023
1 parent f8a4791 commit 1e227a2
Showing 1 changed file with 39 additions and 20 deletions.
59 changes: 39 additions & 20 deletions src/buildx/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import crypto from 'crypto';
import fs from 'fs';
import os from 'os';
import path from 'path';
Expand All @@ -29,6 +30,7 @@ import {Context} from '../context';
import {Exec} from '../exec';
import {Docker} from '../docker/docker';
import {Git} from '../git';
import {Util} from '../util';

import {GitHubRelease} from '../types/github';

Expand All @@ -50,23 +52,25 @@ export class Install {
*/
public async download(version: string): Promise<string> {
const release: GitHubRelease = await Install.getRelease(version);
const fversion = release.tag_name.replace(/^v+|v+$/g, '');
core.debug(`Install.download version: ${fversion}`);
core.debug(`Install.download release tag name: ${release.tag_name}`);

const c = semver.clean(fversion) || '';
const vspec = await this.vspec(release.tag_name);
core.debug(`Install.download vspec: ${vspec}`);

const c = semver.clean(vspec) || '';
if (!semver.valid(c)) {
throw new Error(`Invalid Buildx version "${fversion}".`);
throw new Error(`Invalid Buildx version "${vspec}".`);
}

const installCache = new InstallCache('buildx-dl-bin', fversion);
const installCache = new InstallCache('buildx-dl-bin', vspec);

const cacheFoundPath = await installCache.find();
if (cacheFoundPath) {
core.info(`Buildx binary found in ${cacheFoundPath}`);
return cacheFoundPath;
}

const downloadURL = util.format('https://github.com/docker/buildx/releases/download/v%s/%s', fversion, this.filename(fversion));
const downloadURL = util.format('https://github.com/docker/buildx/releases/download/v%s/%s', vspec, this.filename(vspec));
core.info(`Downloading ${downloadURL}`);

const htcDownloadPath = await tc.downloadTool(downloadURL);
Expand All @@ -83,20 +87,8 @@ export class Install {
* @returns path to the buildx binary
*/
public async build(gitContext: string): Promise<string> {
// eslint-disable-next-line prefer-const
let [repo, ref] = gitContext.split('#');
if (ref.length == 0) {
ref = 'master';
}

let vspec: string;
// TODO: include full ref as fingerprint. Use commit sha as best-effort in the meantime.
if (ref.match(/^[0-9a-fA-F]{40}$/)) {
vspec = ref;
} else {
vspec = await Git.remoteSha(repo, ref, process.env.GIT_AUTH_TOKEN);
}
core.debug(`Install.build: tool version spec ${vspec}`);
const vspec = await this.vspec(gitContext);
core.debug(`Install.build vspec: ${vspec}`);

const installCache = new InstallCache('buildx-build-bin', vspec);

Expand Down Expand Up @@ -228,6 +220,33 @@ export class Install {
return util.format('buildx-v%s.%s-%s%s', version, platform, arch, ext);
}

private async vspec(versionOrRef: string): Promise<string> {
if (!Util.isValidRef(versionOrRef)) {
const v = versionOrRef.replace(/^v+|v+$/g, '');
core.info(`Use ${v} version spec cache key for ${versionOrRef}`);
return v;
}

// eslint-disable-next-line prefer-const
let [baseURL, ref] = versionOrRef.split('#');
if (ref.length == 0) {
ref = 'master';
}

let sha: string;
if (ref.match(/^[0-9a-fA-F]{40}$/)) {
sha = ref;
} else {
sha = await Git.remoteSha(baseURL, ref, process.env.GIT_AUTH_TOKEN);
}

const [owner, repo] = baseURL.substring('https://github.com/'.length).split('/');
const key = `${owner}/${Util.trimSuffix(repo, '.git')}/${sha}`;
const hash = crypto.createHash('sha256').update(key).digest('hex');
core.info(`Use ${hash} version spec cache key for ${key}`);
return hash;
}

public static async getRelease(version: string): Promise<GitHubRelease> {
const url = `https://raw.githubusercontent.com/docker/actions-toolkit/main/.github/buildx-releases.json`;
const http: httpm.HttpClient = new httpm.HttpClient('docker-actions-toolkit');
Expand Down

0 comments on commit 1e227a2

Please sign in to comment.