Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

buildx(build): resolveCacheToAttrs func #350

Merged
merged 1 commit into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions __tests__/buildx/build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,54 @@ describe('resolveSecret', () => {
});
});

describe('resolveCacheToAttrs', () => {
// prettier-ignore
test.each([
[
'',
undefined,
''
],
[
'user/app:cache',
undefined,
'user/app:cache'
],
[
'type=inline',
undefined,
'type=inline'
],
[
'type=gha',
undefined,
'type=gha,repository=docker/actions-toolkit',
],
[
'type=gha,mode=max',
undefined,
'type=gha,mode=max,repository=docker/actions-toolkit',
],
[
'type=gha,mode=max',
'abcd1234',
'type=gha,mode=max,repository=docker/actions-toolkit,ghtoken=abcd1234',
],
[
'type=gha,repository=foo/bar,mode=max',
undefined,
'type=gha,repository=foo/bar,mode=max',
],
[
'type=gha,repository=foo/bar,mode=max',
'abcd1234',
'type=gha,repository=foo/bar,mode=max,ghtoken=abcd1234',
],
])('given %p', async (input: string, githubToken: string | undefined, expected: string) => {
expect(Build.resolveCacheToAttrs(input, githubToken)).toEqual(expected);
});
});

describe('hasLocalExporter', () => {
// prettier-ignore
test.each([
Expand Down
8 changes: 7 additions & 1 deletion __tests__/github.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jest.spyOn(GitHub.prototype, 'repoData').mockImplementation((): Promise<GitHubRe
});

describe('repoData', () => {
it('returns GitHub repository', async () => {
it('returns GitHub repo data', async () => {
const github = new GitHub();
expect((await github.repoData()).name).toEqual('Hello-World');
});
Expand Down Expand Up @@ -89,6 +89,12 @@ describe('apiURL', () => {
});
});

describe('repository', () => {
it('returns GitHub repository', async () => {
expect(GitHub.repository).toEqual('docker/actions-toolkit');
});
});

describe('workflowRunURL', () => {
it('returns 2188748038', async () => {
expect(GitHub.workflowRunURL).toEqual('https://github.com/docker/actions-toolkit/actions/runs/2188748038/attempts/2');
Expand Down
39 changes: 39 additions & 0 deletions src/buildx/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,45 @@ export class Build {
return `${input},builder-id=${GitHub.workflowRunURL}`;
}

public static resolveCacheToAttrs(input: string, githubToken?: string): string {
if (!input) {
return input;
}

let cacheType = 'registry';
let ghaCacheRepository = '';
let ghaCacheGHToken = '';

const fields = parse(input, {
relaxColumnCount: true,
skipEmptyLines: true
})[0];
for (const field of fields) {
const parts = field
.toString()
.split(/(?<=^[^=]+?)=/)
.map(item => item.trim());
if (parts[0] === 'type') {
cacheType = parts[1];
} else if (parts[0] === 'repository') {
ghaCacheRepository = parts[1];
} else if (parts[0] === 'ghtoken') {
ghaCacheGHToken = parts[1];
}
}

if (cacheType === 'gha') {
if (!ghaCacheRepository) {
input = `${input},repository=${GitHub.repository}`;
}
if (!ghaCacheGHToken && githubToken) {
input = `${input},ghtoken=${githubToken}`;
}
}

return input;
}

public static hasLocalExporter(exporters: string[]): boolean {
return Build.hasExporterType('local', exporters);
}
Expand Down
6 changes: 5 additions & 1 deletion src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,14 @@ export class GitHub {
return process.env.GITHUB_API_URL || 'https://api.github.com';
}

static get repository(): string {
return `${github.context.repo.owner}/${github.context.repo.repo}`;
}

static get workflowRunURL(): string {
const runID = process.env.GITHUB_RUN_ID || github.context.runId;
const runAttempt = process.env.GITHUB_RUN_ATTEMPT || 1;
return `${GitHub.serverURL}/${github.context.repo.owner}/${github.context.repo.repo}/actions/runs/${runID}/attempts/${runAttempt}`;
return `${GitHub.serverURL}/${GitHub.repository}/actions/runs/${runID}/attempts/${runAttempt}`;
}

static get actionsRuntimeToken(): GitHubActionsRuntimeToken | undefined {
Expand Down
Loading