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

bake: set cwd:// prefix for bake files path #370

Merged
merged 4 commits into from
Jan 4, 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
28 changes: 27 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()}`);
}
);
94 changes: 45 additions & 49 deletions src/meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
});
return this.generateBakeFile(
{
tags: this.getTags(),
args: {
DOCKER_META_IMAGES: this.getImageNames().join(','),
DOCKER_META_VERSION: this.version.main
}
},
kind
);
} else if (kind == 'labels') {
return this.generateBakeFile(kind, {
labels: this.getLabels().reduce((res, label) => {
const matches = label.match(/([^=]*)=(.*)/);
if (!matches) {
return this.generateBakeFile(
{
labels: this.getLabels().reduce((res, label) => {
const matches = label.match(/([^=]*)=(.*)/);
if (!matches) {
return res;
}
res[matches[1]] = matches[2];
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;
}
Expand Down