mirror of
https://github.com/docker/metadata-action.git
synced 2025-08-17 03:39:54 +08:00
Add bake-file output (#36)
Co-authored-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
@@ -1,5 +1,10 @@
|
||||
import csvparse from 'csv-parse/lib/sync';
|
||||
import * as core from '@actions/core';
|
||||
import * as fs from 'fs';
|
||||
import * as os from 'os';
|
||||
import * as path from 'path';
|
||||
|
||||
let _tmpDir: string;
|
||||
|
||||
export interface Inputs {
|
||||
images: string[];
|
||||
@@ -19,6 +24,13 @@ export interface Inputs {
|
||||
githubToken: string;
|
||||
}
|
||||
|
||||
export function tmpDir(): string {
|
||||
if (!_tmpDir) {
|
||||
_tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ghaction-docker-meta-')).split(path.sep).join(path.posix.sep);
|
||||
}
|
||||
return _tmpDir;
|
||||
}
|
||||
|
||||
export function getInputs(): Inputs {
|
||||
return {
|
||||
images: getInputList('images'),
|
||||
|
10
src/main.ts
10
src/main.ts
@@ -1,3 +1,4 @@
|
||||
import * as fs from 'fs';
|
||||
import {getInputs, Inputs} from './context';
|
||||
import * as github from './github';
|
||||
import {Meta, Version} from './meta';
|
||||
@@ -33,6 +34,7 @@ async function run() {
|
||||
core.endGroup();
|
||||
core.setOutput('version', version.main || '');
|
||||
|
||||
// Docker tags
|
||||
const tags: Array<string> = meta.tags();
|
||||
core.startGroup(`Docker tags`);
|
||||
for (let tag of tags) {
|
||||
@@ -41,6 +43,7 @@ async function run() {
|
||||
core.endGroup();
|
||||
core.setOutput('tags', tags.join(inputs.sepTags));
|
||||
|
||||
// Docker labels
|
||||
const labels: Array<string> = meta.labels();
|
||||
core.startGroup(`Docker labels`);
|
||||
for (let label of labels) {
|
||||
@@ -48,6 +51,13 @@ async function run() {
|
||||
}
|
||||
core.endGroup();
|
||||
core.setOutput('labels', labels.join(inputs.sepLabels));
|
||||
|
||||
// Bake definition file
|
||||
const bakeFile: string = meta.bakeFile();
|
||||
core.startGroup(`Bake definition file`);
|
||||
core.info(fs.readFileSync(bakeFile, 'utf8'));
|
||||
core.endGroup();
|
||||
core.setOutput('bake-file', bakeFile);
|
||||
} catch (error) {
|
||||
core.setFailed(error.message);
|
||||
}
|
||||
|
34
src/meta.ts
34
src/meta.ts
@@ -1,7 +1,9 @@
|
||||
import * as handlebars from 'handlebars';
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import moment from 'moment';
|
||||
import * as semver from 'semver';
|
||||
import {Inputs} from './context';
|
||||
import {Inputs, tmpDir} from './context';
|
||||
import * as core from '@actions/core';
|
||||
import {Context} from '@actions/github/lib/context';
|
||||
import {ReposGetResponseData} from '@octokit/types';
|
||||
@@ -143,4 +145,34 @@ export class Meta {
|
||||
labels.push(...this.inputs.labelCustom);
|
||||
return labels;
|
||||
}
|
||||
|
||||
public bakeFile(): string {
|
||||
let jsonLabels = {};
|
||||
for (let label of this.labels()) {
|
||||
const matches = label.match(/([^=]*)=(.*)/);
|
||||
if (!matches) {
|
||||
continue;
|
||||
}
|
||||
jsonLabels[matches[1]] = matches[2];
|
||||
}
|
||||
|
||||
const bakeFile = path.join(tmpDir(), 'ghaction-docker-meta-bake.json').split(path.sep).join(path.posix.sep);
|
||||
fs.writeFileSync(
|
||||
bakeFile,
|
||||
JSON.stringify(
|
||||
{
|
||||
target: {
|
||||
'ghaction-docker-meta': {
|
||||
tags: this.tags(),
|
||||
labels: jsonLabels
|
||||
}
|
||||
}
|
||||
},
|
||||
null,
|
||||
2
|
||||
)
|
||||
);
|
||||
|
||||
return bakeFile;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user