Skip to content

Commit

Permalink
bake: set cwd:// prefix for bake files path
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <[email protected]>
  • Loading branch information
crazy-max committed Dec 20, 2023
1 parent 74fa878 commit f258a66
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 51 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -455,3 +455,25 @@ 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: meta
uses: ./
-
name: Bake files
run: |
echo ${{ steps.meta.outputs.bake-file }}
echo ${{ steps.meta.outputs.bake-file-tags }}
echo ${{ steps.meta.outputs.bake-file-labels }}
11 changes: 10 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,16 @@ actionsToolkit.run(
});
}

const meta: Meta = new Meta(inputs, context, repo);
const bakeFileCwdPrefix = await toolkit.buildx
.versionSatisfies('>=0.12.0')
.then(ok => {
return ok ? 'cwd://' : '';
})
.catch(() => {
return '';
});

const meta: Meta = new Meta(inputs, context, repo, bakeFileCwdPrefix);

const version: Version = meta.version;
if (meta.version.main == undefined || meta.version.main.length == 0) {
Expand Down
98 changes: 48 additions & 50 deletions src/meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ export class Meta {
private readonly tags: tcl.Tag[];
private readonly flavor: fcl.Flavor;
private readonly date: Date;
private readonly bakeFileCwdPrefix: string;

constructor(inputs: Inputs, context: Context, repo: GitHubRepo) {
constructor(inputs: Inputs, context: Context, repo: GitHubRepo, bakeFileCwdPrefix?: string) {
this.inputs = inputs;
this.context = context;
this.repo = repo;
Expand All @@ -40,6 +41,7 @@ export class Meta {
this.flavor = fcl.Transform(inputs.flavor);
this.date = new Date();
this.version = this.getVersion();
this.bakeFileCwdPrefix = bakeFileCwdPrefix || '';
}

private getVersion(): Version {
Expand Down Expand Up @@ -522,70 +524,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(), `${this.bakeFileCwdPrefix}docker-metadata-action-bake${suffix ? `-${suffix}` : ''}.json`);
fs.writeFileSync(bakeFile, JSON.stringify({target: {[this.inputs.bakeTarget]: dt}}, null, 2));
return bakeFile;
}
Expand Down

0 comments on commit f258a66

Please sign in to comment.