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
uses: docker/setup-buildx-action@v3
with:
version: v0.12.0-rc1
version: latest
-
name: Build
uses: docker/bake-action@v4
@ -455,3 +455,29 @@ jobs:
-
name: Print envs
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));
});
// 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
for (const kind of ['tags', 'labels', 'annotations:' + annotationsLevels]) {
const outputName = kind.split(':')[0];
const bakeFile: string = meta.getBakeFile(kind);
await core.group(`Bake file definition (${outputName})`, async () => {
core.info(fs.readFileSync(bakeFile, 'utf8'));
setOutput(`bake-file-${outputName}`, bakeFile);
setOutput(`bake-file-${outputName}`, `${bakeFileCwdPrefix}${bakeFile}`);
});
}
// 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 {
if (kind == 'tags') {
return this.generateBakeFile(kind, {
tags: this.getTags(),
args: {
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;
return this.generateBakeFile(
{
tags: this.getTags(),
args: {
DOCKER_META_IMAGES: this.getImageNames().join(','),
DOCKER_META_VERSION: this.version.main
}
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:')) {
const name = kind.split(':')[0];
const annotations: Array<string> = [];
for (const level of kind.split(':')[1].split(',')) {
annotations.push(...this.getAnnotations().map(label => `${level}:${label}`));
}
return this.generateBakeFile(name, {
annotations: annotations
});
return this.generateBakeFile(
{
annotations: annotations
},
name
);
}
throw new Error(`Unknown bake file type: ${kind}`);
}
public getBakeFileTagsLabels(): string {
const bakeFile = path.join(ToolkitContext.tmpDir(), 'docker-metadata-action-bake.json');
fs.writeFileSync(
bakeFile,
JSON.stringify(
{
target: {
[this.inputs.bakeTarget]: {
tags: this.getTags(),
labels: this.getLabels().reduce((res, label) => {
const matches = label.match(/([^=]*)=(.*)/);
if (!matches) {
return res;
}
res[matches[1]] = matches[2];
return res;
}, {}),
args: {
DOCKER_META_IMAGES: this.getImageNames().join(','),
DOCKER_META_VERSION: this.version.main
}
}
}
},
null,
2
)
);
return bakeFile;
return this.generateBakeFile({
tags: this.getTags(),
labels: this.getLabels().reduce((res, label) => {
const matches = label.match(/([^=]*)=(.*)/);
if (!matches) {
return res;
}
res[matches[1]] = matches[2];
return res;
}, {}),
args: {
DOCKER_META_IMAGES: this.getImageNames().join(','),
DOCKER_META_VERSION: this.version.main
}
});
}
private generateBakeFile(name: string, dt): string {
const bakeFile = path.join(ToolkitContext.tmpDir(), `docker-metadata-action-bake-${name}.json`);
private generateBakeFile(dt, suffix?: string): string {
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));
return bakeFile;
}