Merge pull request #370 from crazy-max/bake-cwd

bake: set cwd:// prefix for bake files path
This commit is contained in:
Tõnis Tiigi 2024-01-04 09:32:49 -08:00 committed by GitHub
commit 41e1fe3437
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 82 additions and 55 deletions

View file

@ -424,7 +424,7 @@ jobs:
name: Set up Docker Buildx name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3 uses: docker/setup-buildx-action@v3
with: with:
version: v0.12.0-rc1 version: latest
- -
name: Build name: Build
uses: docker/bake-action@v4 uses: docker/bake-action@v4
@ -455,3 +455,29 @@ jobs:
- -
name: Print envs name: Print envs
run: env|sort run: env|sort
bake-cwd:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v4
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
version: latest
-
name: Docker meta
id: docker_meta
uses: ./
-
name: Build
uses: docker/bake-action@v4
with:
files: |
./test/docker-bake.hcl
${{ steps.docker_meta.outputs.bake-file-tags }}
${{ steps.docker_meta.outputs.bake-file-labels }}
targets: |
release

2
dist/index.js generated vendored

File diff suppressed because one or more lines are too long

2
dist/index.js.map generated vendored

File diff suppressed because one or more lines are too long

View file

@ -94,17 +94,22 @@ actionsToolkit.run(
setOutput('json', JSON.stringify(jsonOutput)); setOutput('json', JSON.stringify(jsonOutput));
}); });
// Specifying local and remote bake files is supported since Buildx 0.12.0.
// Set cwd:// prefix for local bake files to avoid ambiguity with remote
// https://github.com/docker/buildx/pull/1838
const bakeFileCwdPrefix = (await toolkit.buildx.versionSatisfies('>=0.12.0').catch(() => false)) ? 'cwd://' : '';
// Bake files // Bake files
for (const kind of ['tags', 'labels', 'annotations:' + annotationsLevels]) { for (const kind of ['tags', 'labels', 'annotations:' + annotationsLevels]) {
const outputName = kind.split(':')[0]; const outputName = kind.split(':')[0];
const bakeFile: string = meta.getBakeFile(kind); const bakeFile: string = meta.getBakeFile(kind);
await core.group(`Bake file definition (${outputName})`, async () => { await core.group(`Bake file definition (${outputName})`, async () => {
core.info(fs.readFileSync(bakeFile, 'utf8')); core.info(fs.readFileSync(bakeFile, 'utf8'));
setOutput(`bake-file-${outputName}`, bakeFile); setOutput(`bake-file-${outputName}`, `${bakeFileCwdPrefix}${bakeFile}`);
}); });
} }
// Bake file with tags and labels // Bake file with tags and labels
setOutput(`bake-file`, meta.getBakeFileTagsLabels()); setOutput(`bake-file`, `${bakeFileCwdPrefix}${meta.getBakeFileTagsLabels()}`);
} }
); );

View file

@ -522,70 +522,66 @@ export class Meta {
public getBakeFile(kind: string): string { public getBakeFile(kind: string): string {
if (kind == 'tags') { if (kind == 'tags') {
return this.generateBakeFile(kind, { return this.generateBakeFile(
tags: this.getTags(), {
args: { tags: this.getTags(),
DOCKER_META_IMAGES: this.getImageNames().join(','), args: {
DOCKER_META_VERSION: this.version.main DOCKER_META_IMAGES: this.getImageNames().join(','),
} DOCKER_META_VERSION: this.version.main
});
} else if (kind == 'labels') {
return this.generateBakeFile(kind, {
labels: this.getLabels().reduce((res, label) => {
const matches = label.match(/([^=]*)=(.*)/);
if (!matches) {
return res;
} }
res[matches[1]] = matches[2]; },
return res; kind
}, {}) );
}); } else if (kind == 'labels') {
return this.generateBakeFile(
{
labels: this.getLabels().reduce((res, label) => {
const matches = label.match(/([^=]*)=(.*)/);
if (!matches) {
return res;
}
res[matches[1]] = matches[2];
return res;
}, {})
},
kind
);
} else if (kind.startsWith('annotations:')) { } else if (kind.startsWith('annotations:')) {
const name = kind.split(':')[0]; const name = kind.split(':')[0];
const annotations: Array<string> = []; const annotations: Array<string> = [];
for (const level of kind.split(':')[1].split(',')) { for (const level of kind.split(':')[1].split(',')) {
annotations.push(...this.getAnnotations().map(label => `${level}:${label}`)); annotations.push(...this.getAnnotations().map(label => `${level}:${label}`));
} }
return this.generateBakeFile(name, { return this.generateBakeFile(
annotations: annotations {
}); annotations: annotations
},
name
);
} }
throw new Error(`Unknown bake file type: ${kind}`); throw new Error(`Unknown bake file type: ${kind}`);
} }
public getBakeFileTagsLabels(): string { public getBakeFileTagsLabels(): string {
const bakeFile = path.join(ToolkitContext.tmpDir(), 'docker-metadata-action-bake.json'); return this.generateBakeFile({
fs.writeFileSync( tags: this.getTags(),
bakeFile, labels: this.getLabels().reduce((res, label) => {
JSON.stringify( const matches = label.match(/([^=]*)=(.*)/);
{ if (!matches) {
target: { return res;
[this.inputs.bakeTarget]: { }
tags: this.getTags(), res[matches[1]] = matches[2];
labels: this.getLabels().reduce((res, label) => { return res;
const matches = label.match(/([^=]*)=(.*)/); }, {}),
if (!matches) { args: {
return res; DOCKER_META_IMAGES: this.getImageNames().join(','),
} DOCKER_META_VERSION: this.version.main
res[matches[1]] = matches[2]; }
return res; });
}, {}),
args: {
DOCKER_META_IMAGES: this.getImageNames().join(','),
DOCKER_META_VERSION: this.version.main
}
}
}
},
null,
2
)
);
return bakeFile;
} }
private generateBakeFile(name: string, dt): string { private generateBakeFile(dt, suffix?: string): string {
const bakeFile = path.join(ToolkitContext.tmpDir(), `docker-metadata-action-bake-${name}.json`); const bakeFile = path.join(ToolkitContext.tmpDir(), `docker-metadata-action-bake${suffix ? `-${suffix}` : ''}.json`);
fs.writeFileSync(bakeFile, JSON.stringify({target: {[this.inputs.bakeTarget]: dt}}, null, 2)); fs.writeFileSync(bakeFile, JSON.stringify({target: {[this.inputs.bakeTarget]: dt}}, null, 2));
return bakeFile; return bakeFile;
} }