Skip to content
Open
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
41 changes: 41 additions & 0 deletions __tests__/buildx/build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import * as rimraf from 'rimraf';
import {Context} from '../../src/context';
import {Build} from '../../src/buildx/build';

import {GitContextFormat} from '../../src/types/buildx/build';

const fixturesDir = path.join(__dirname, '..', '.fixtures');
const tmpDir = fs.mkdtempSync(path.join(process.env.TEMP || os.tmpdir(), 'buildx-build-'));
const tmpName = path.join(tmpDir, '.tmpname-jest');
Expand All @@ -41,6 +43,45 @@ afterEach(() => {
rimraf.sync(tmpDir);
});

describe('gitContext', () => {
const originalEnv = process.env;
beforeEach(() => {
jest.resetModules();
process.env = {
...originalEnv,
DOCKER_GIT_CONTEXT_PR_HEAD_REF: ''
};
});
afterEach(() => {
process.env = originalEnv;
});
// prettier-ignore
test.each([
// no format set
['refs/heads/master', undefined, false, 'https://github.com/docker/actions-toolkit.git?ref=refs/heads/master&checksum=860c1904a1ce19322e91ac35af1ab07466440c37'],
['master', undefined, false, 'https://github.com/docker/actions-toolkit.git?ref=refs/heads/master&checksum=860c1904a1ce19322e91ac35af1ab07466440c37'],
['refs/pull/15/merge', undefined, false, 'https://github.com/docker/actions-toolkit.git?ref=refs/pull/15/merge&checksum=860c1904a1ce19322e91ac35af1ab07466440c37'],
['refs/tags/v1.0.0', undefined, false, 'https://github.com/docker/actions-toolkit.git?ref=refs/tags/v1.0.0&checksum=860c1904a1ce19322e91ac35af1ab07466440c37'],
['refs/pull/15/merge', undefined, true, 'https://github.com/docker/actions-toolkit.git?ref=refs/pull/15/head&checksum=860c1904a1ce19322e91ac35af1ab07466440c37'],
// query format
['refs/heads/master', 'query', false, 'https://github.com/docker/actions-toolkit.git?ref=refs/heads/master&checksum=860c1904a1ce19322e91ac35af1ab07466440c37'],
['master', 'query', false, 'https://github.com/docker/actions-toolkit.git?ref=refs/heads/master&checksum=860c1904a1ce19322e91ac35af1ab07466440c37'],
['refs/pull/15/merge', 'query', false, 'https://github.com/docker/actions-toolkit.git?ref=refs/pull/15/merge&checksum=860c1904a1ce19322e91ac35af1ab07466440c37'],
['refs/tags/v1.0.0', 'query', false, 'https://github.com/docker/actions-toolkit.git?ref=refs/tags/v1.0.0&checksum=860c1904a1ce19322e91ac35af1ab07466440c37'],
['refs/pull/15/merge', 'query', true, 'https://github.com/docker/actions-toolkit.git?ref=refs/pull/15/head&checksum=860c1904a1ce19322e91ac35af1ab07466440c37'],
// fragment format
['refs/heads/master', 'fragment', false, 'https://github.com/docker/actions-toolkit.git#860c1904a1ce19322e91ac35af1ab07466440c37'],
['master', 'fragment', false, 'https://github.com/docker/actions-toolkit.git#860c1904a1ce19322e91ac35af1ab07466440c37'],
['refs/pull/15/merge', 'fragment', false, 'https://github.com/docker/actions-toolkit.git#refs/pull/15/merge'],
['refs/tags/v1.0.0', 'fragment', false, 'https://github.com/docker/actions-toolkit.git#860c1904a1ce19322e91ac35af1ab07466440c37'],
['refs/pull/15/merge', 'fragment', true, 'https://github.com/docker/actions-toolkit.git#refs/pull/15/head'],
])('given %p and %p, should return %p', async (ref: string, format: string | undefined, prHeadRef: boolean, expected: string) => {
process.env.DOCKER_DEFAULT_GIT_CONTEXT_PR_HEAD_REF = prHeadRef ? 'true' : '';
const build = new Build();
expect(await build.gitContext(ref, '860c1904a1ce19322e91ac35af1ab07466440c37', format as GitContextFormat)).toEqual(expected);
});
});

describe('resolveImageID', () => {
it('matches', async () => {
const imageID = 'sha256:bfb45ab72e46908183546477a08f8867fc40cebadd00af54b071b097aed127a9';
Expand Down
79 changes: 0 additions & 79 deletions __tests__/context.test.ts

This file was deleted.

29 changes: 28 additions & 1 deletion src/buildx/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
import fs from 'fs';
import path from 'path';
import * as core from '@actions/core';
import * as github from '@actions/github';
import {parse} from 'csv-parse/sync';

import {Buildx} from './buildx';
import {Context} from '../context';
import {GitHub} from '../github';
import {Util} from '../util';

import {BuildMetadata} from '../types/buildx/build';
import {BuildMetadata, GitContextFormat} from '../types/buildx/build';
import {VertexWarning} from '../types/buildkit/client';
import {ProvenancePredicate} from '../types/intoto/slsa_provenance/v0.2/provenance';

Expand All @@ -48,6 +49,32 @@ export class Build {
this.metadataFilename = `build-metadata-${Util.generateRandomString()}.json`;
}

public async gitContext(ref?: string, sha?: string, format?: GitContextFormat): Promise<string> {
const setPullRequestHeadRef: boolean = !!(process.env.DOCKER_DEFAULT_GIT_CONTEXT_PR_HEAD_REF && process.env.DOCKER_DEFAULT_GIT_CONTEXT_PR_HEAD_REF === 'true');
ref = ref || github.context.ref;
sha = sha || github.context.sha;
if (!ref.startsWith('refs/')) {
ref = `refs/heads/${ref}`;
} else if (ref.startsWith(`refs/pull/`) && setPullRequestHeadRef) {
ref = ref.replace(/\/merge$/g, '/head');
}
const baseURL = `${GitHub.serverURL}/${github.context.repo.owner}/${github.context.repo.repo}.git`;
if (!format) {
if (await this.buildx.versionSatisfies('>=0.29.0')) {
format = 'query';
} else {
format = 'fragment';
}
Comment on lines +63 to +67
Copy link
Member Author

@crazy-max crazy-max Oct 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Defaults to query format since Buildx 0.29.0 related to docker/buildx#3415

There will be a follow-up on build-push-action and bake-action to set BUILDX_SEND_GIT_QUERY_AS_INPUT accordingly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still depends on the dockerfile version as well, so not sure if we can change default before BUILDX_SEND_GIT_QUERY_AS_INPUT

}
if (format === 'query') {
return `${baseURL}?ref=${ref}${sha !== '' ? `&checksum=${sha}` : ''}`;
}
if (sha && !ref.startsWith(`refs/pull/`)) {
return `${baseURL}#${sha}`;
}
return `${baseURL}#${ref}`;
}

public getImageIDFilePath(): string {
return path.join(Context.tmpDir(), this.iidFilename);
}
Expand Down
24 changes: 0 additions & 24 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ import fs from 'fs';
import os from 'os';
import path from 'path';
import * as tmp from 'tmp';
import * as github from '@actions/github';

import {GitHub} from './github';

export class Context {
private static readonly _tmpDir = fs.mkdtempSync(path.join(Context.ensureDirExists(process.env.RUNNER_TEMP || os.tmpdir()), 'docker-actions-toolkit-'));
Expand All @@ -37,25 +34,4 @@ export class Context {
public static tmpName(options?: tmp.TmpNameOptions): string {
return tmp.tmpNameSync(options);
}

public static gitRef(): string {
return Context.parseGitRef(github.context.ref, github.context.sha);
}

public static parseGitRef(ref: string, sha: string): string {
const setPullRequestHeadRef: boolean = !!(process.env.DOCKER_DEFAULT_GIT_CONTEXT_PR_HEAD_REF && process.env.DOCKER_DEFAULT_GIT_CONTEXT_PR_HEAD_REF === 'true');
if (sha && ref && !ref.startsWith('refs/')) {
ref = `refs/heads/${ref}`;
}
if (sha && !ref.startsWith(`refs/pull/`)) {
ref = sha;
} else if (ref.startsWith(`refs/pull/`) && setPullRequestHeadRef) {
ref = ref.replace(/\/merge$/g, '/head');
}
return ref;
}

public static gitContext(): string {
return `${GitHub.serverURL}/${github.context.repo.owner}/${github.context.repo.repo}.git#${Context.gitRef()}`;
}
}
2 changes: 2 additions & 0 deletions src/types/buildx/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

export type GitContextFormat = 'fragment' | 'query';

export type BuildMetadata = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[key: string]: any;
Expand Down
Loading