Skip to content

Commit 329c1c7

Browse files
committed
bake: handle git auth token when parsing remote definition
Signed-off-by: CrazyMax <[email protected]>
1 parent eb56632 commit 329c1c7

6 files changed

Lines changed: 52 additions & 11 deletions

File tree

.github/workflows/test.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ on:
1515

1616
env:
1717
NODE_VERSION: "20"
18+
BUILDX_VERSION: "v0.14.0-rc1"
1819

1920
jobs:
2021
test:
@@ -102,6 +103,13 @@ jobs:
102103
with:
103104
node-version: ${{ env.NODE_VERSION }}
104105
cache: 'yarn'
106+
-
107+
name: Set up Docker Buildx
108+
if: startsWith(matrix.os, 'ubuntu')
109+
uses: docker/setup-buildx-action@v3
110+
with:
111+
version: ${{ env.BUILDX_VERSION }}
112+
driver: docker
105113
-
106114
name: Install
107115
run: yarn install

__tests__/buildx/bake.test.itg.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,28 @@ maybe('getDefinition', () => {
3535
[
3636
'https://github.com/docker/buildx.git#v0.10.4',
3737
['binaries-cross'],
38-
path.join(fixturesDir, 'bake-buildx-0.10.4-binaries-cross.json')
38+
path.join(fixturesDir, 'bake-buildx-0.10.4-binaries-cross.json'),
39+
false,
3940
],
40-
])('given %p', async (source: string, targets: string[], out: string) => {
41+
// TODO: uncomment this test case when we have access to the private repo using an access token
42+
// [
43+
// 'https://github.com/docker/test-docker-action.git#remote-private',
44+
// ['default'],
45+
// path.join(fixturesDir, 'bake-test-docker-action-remote-private.json'),
46+
// true,
47+
// ]
48+
])('given %p', async (source: string, targets: string[], out: string, auth) => {
49+
const gitAuthToken = process.env.GITHUB_TOKEN || '';
50+
if (auth && !gitAuthToken) {
51+
console.log(`Git auth token not available, skipping test`);
52+
return;
53+
}
4154
const bake = new Bake();
4255
const expectedDef = <BakeDefinition>JSON.parse(fs.readFileSync(out, {encoding: 'utf-8'}).trim())
4356
expect(await bake.getDefinition({
4457
source: source,
45-
targets: targets
58+
targets: targets,
59+
githubToken: gitAuthToken,
4660
})).toEqual(expectedDef);
4761
});
4862
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"target": {
3+
"default": {
4+
"context": "https://github.com/docker/test-docker-action.git#remote-private",
5+
"dockerfile": "Dockerfile",
6+
"tags": [
7+
"foo"
8+
]
9+
}
10+
}
11+
}

dev.Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
ARG NODE_VERSION=20
1818
ARG DOCKER_VERSION=26.0.0
19-
ARG BUILDX_VERSION=0.13.1
19+
ARG BUILDX_VERSION=0.14.0-rc1
2020

2121
FROM node:${NODE_VERSION}-alpine AS base
2222
RUN apk add --no-cache cpio findutils git

src/buildx/bake.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ export interface BakeCmdOpts {
3636
sbom?: string;
3737
source?: string;
3838
targets?: Array<string>;
39+
40+
githubToken?: string; // for auth with remote definitions on private repos
3941
}
4042

4143
export class Bake {
@@ -48,6 +50,13 @@ export class Bake {
4850
public async getDefinition(cmdOpts: BakeCmdOpts, execOptions?: ExecOptions): Promise<BakeDefinition> {
4951
execOptions = execOptions || {ignoreReturnCode: true};
5052
execOptions.ignoreReturnCode = true;
53+
if (cmdOpts.githubToken) {
54+
execOptions.env = Object.assign({}, process.env, {
55+
BUILDX_BAKE_GIT_AUTH_TOKEN: cmdOpts.githubToken
56+
}) as {
57+
[key: string]: string;
58+
};
59+
}
5160

5261
const args = ['bake'];
5362

src/buildx/inputs.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,24 +77,23 @@ export class Inputs {
7777
}
7878

7979
public static resolveBuildSecretString(kvp: string): string {
80-
return Inputs.resolveBuildSecret(kvp, false);
80+
const [key, file] = Inputs.resolveBuildSecret(kvp, false);
81+
return `id=${key},src=${file}`;
8182
}
8283

8384
public static resolveBuildSecretFile(kvp: string): string {
84-
return Inputs.resolveBuildSecret(kvp, true);
85+
const [key, file] = Inputs.resolveBuildSecret(kvp, true);
86+
return `id=${key},src=${file}`;
8587
}
8688

8789
public static resolveBuildSecretEnv(kvp: string): string {
8890
const [key, value] = parseKvp(kvp);
89-
9091
return `id=${key},env=${value}`;
9192
}
9293

93-
public static resolveBuildSecret(kvp: string, file: boolean): string {
94+
public static resolveBuildSecret(kvp: string, file: boolean): [string, string] {
9495
const [key, _value] = parseKvp(kvp);
95-
9696
let value = _value;
97-
9897
if (file) {
9998
if (!fs.existsSync(value)) {
10099
throw new Error(`secret file ${value} not found`);
@@ -103,7 +102,7 @@ export class Inputs {
103102
}
104103
const secretFile = Context.tmpName({tmpdir: Context.tmpDir()});
105104
fs.writeFileSync(secretFile, value);
106-
return `id=${key},src=${secretFile}`;
105+
return [key, secretFile];
107106
}
108107

109108
public static getProvenanceInput(name: string): string {

0 commit comments

Comments
 (0)