Skip to content

Commit

Permalink
feat(container-metadata): align with container-metadata-action 5.5.1
Browse files Browse the repository at this point in the history
Annotations support, split bake definition into two files, allow images input to be empty to output
bare tags
  • Loading branch information
gperdomor committed Apr 22, 2024
1 parent 5c86d74 commit 1c8187e
Show file tree
Hide file tree
Showing 60 changed files with 932 additions and 224 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
"@actions/github": "6.0.0",
"@actions/io": "1.1.3",
"@nx/devkit": "18.3.3",
"@octokit/openapi-types": "19.1.0",
"@renovate/pep440": "1.0.0",
"@swc/helpers": "0.5.3",
"chalk": "4.1.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/ci-context/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"dependencies": {
"@nx-tools/core": "6.0.0-alpha.2",
"ci-info": "^4.0.0",
"@octokit/openapi-types": "^19.1.0",
"@octokit/openapi-types": "^20.0.0",
"@actions/github": "^6.0.0",
"properties-file": "^3.5.4"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ GITHUB_API_URL=https://api.github.com
GITHUB_BASE_REF=master
GITHUB_ENV=/home/runner/work/_temp/_runner_file_commands/set_env_ca953e95-2f41-4926-bc52-a1c8d90e94c1
GITHUB_EVENT_NAME=pull_request
GITHUB_EVENT_PATH=./packages/container-metadata/tests/fixtures/event_pull_request.json
GITHUB_EVENT_PATH=./__tests__/fixtures/event_pull_request.json
GITHUB_GRAPHQL_URL=https://api.github.com/graphql
GITHUB_HEAD_REF=test-pr
GITHUB_JOB=test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ GITHUB_API_URL=https://api.github.com
GITHUB_BASE_REF=master
GITHUB_ENV=/home/runner/work/_temp/_runner_file_commands/set_env_2f69f48f-fb93-475f-a6a5-91dc7866f518
GITHUB_EVENT_NAME=pull_request_target
GITHUB_EVENT_PATH=./packages/container-metadata/tests/fixtures/event_pull_request_target.json
GITHUB_EVENT_PATH=./__tests__/fixtures/event_pull_request_target.json
GITHUB_GRAPHQL_URL=https://api.github.com/graphql
GITHUB_HEAD_REF=test-pr
GITHUB_JOB=test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ GITHUB_API_URL=https://api.github.com
GITHUB_BASE_REF=
GITHUB_ENV=/home/runner/work/_temp/_runner_file_commands/set_env_f5600b76-1b08-404a-8e90-7f2cd620928b
GITHUB_EVENT_NAME=push
GITHUB_EVENT_PATH=./packages/container-metadata/tests/fixtures/event_push_master.json
GITHUB_EVENT_PATH=./__tests__/fixtures/event_push_master.json
GITHUB_GRAPHQL_URL=https://api.github.com/graphql
GITHUB_HEAD_REF=
GITHUB_JOB=test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ GITHUB_API_URL=https://api.github.com
GITHUB_BASE_REF=
GITHUB_ENV=/home/runner/work/_temp/_runner_file_commands/set_env_7703d3cb-84db-438f-9f97-46e159388a55
GITHUB_EVENT_NAME=push
GITHUB_EVENT_PATH=./packages/container-metadata/tests/fixtures/event_tag_v1.1.1.json
GITHUB_EVENT_PATH=./__tests__/fixtures/event_tag_v1.1.1.json
GITHUB_GRAPHQL_URL=https://api.github.com/graphql
GITHUB_HEAD_REF=
GITHUB_JOB=test
Expand Down
16 changes: 14 additions & 2 deletions packages/container-metadata/src/lib/context.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@ describe('getInputs', () => {
['images', 'moby/buildkit\nghcr.io/moby/mbuildkit'],
]),
{
// context: ContextSource.workflow,
"bake-target": 'container-metadata-action',
flavor: [],
"github-token": '',
images: ['moby/buildkit', 'ghcr.io/moby/mbuildkit'],
labels: [],
annotations: [],
"sep-labels": '\n',
"sep-tags": '\n',
"sep-annotations": '\n',
tags: [],
} as Inputs
],
Expand All @@ -47,15 +50,19 @@ describe('getInputs', () => {
['images', 'moby/buildkit'],
['sep-labels', ','],
['sep-tags', ','],
['sep-annotations', ',']
]),
{
// context: ContextSource.workflow,
"bake-target": 'metadata',
flavor: [],
"github-token": '',
images: ['moby/buildkit'],
labels: [],
annotations: [],
"sep-labels": ',',
"sep-tags": ',',
"sep-annotations": ',',
tags: [],
} as Inputs
],
Expand All @@ -65,13 +72,16 @@ describe('getInputs', () => {
['images', 'moby/buildkit\n#comment\nghcr.io/moby/mbuildkit'],
]),
{
// context: ContextSource.workflow,
"bake-target": 'container-metadata-action',
flavor: [],
"github-token": '',
images: ['moby/buildkit', 'ghcr.io/moby/mbuildkit'],
labels: [],
annotations: [],
"sep-labels": '\n',
"sep-tags": '\n',
"sep-annotations": '\n',
tags: [],
} as Inputs
],
Expand All @@ -94,9 +104,11 @@ describe('getContext', () => {
restore = mockedEnv(
{
...process.env,
...dotenv.parse(fs.readFileSync(path.join(__dirname, '..', '..', 'tests', 'fixtures/event_create_branch.env'))),
...dotenv.parse(
fs.readFileSync(path.join(__dirname, '..', '..', '__tests__', 'fixtures/event_create_branch.env'))
),
},
{ clear: true }
{ restore: true }
);
});

Expand Down
15 changes: 15 additions & 0 deletions packages/container-metadata/src/lib/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import { ExecutorContext, names } from '@nx/devkit';
export interface Inputs {
'bake-target': string;
'github-token': string;
'sep-annotations': string;
'sep-labels': string;
'sep-tags': string;
annotations: string[];
flavor: string[];
images: string[];
labels: string[];
Expand All @@ -22,12 +24,25 @@ export function getInputs(options: Partial<Inputs>, ctx?: ExecutorContext): Inpu
fallback: options['bake-target'] || 'container-metadata-action',
}),
'github-token': core.getInput('github-token'),
'sep-annotations': core.getInput('sep-annotations', {
prefix,
fallback: options['sep-annotations'] || '\n',
trimWhitespace: false,
}),
'sep-labels': core.getInput('sep-labels', {
prefix,
fallback: options['sep-labels'] || '\n',
trimWhitespace: false,
}),
'sep-tags': core.getInput('sep-tags', { prefix, fallback: options['sep-tags'] || '\n', trimWhitespace: false }),
annotations: core
.getInputList('annotations', {
prefix,
fallback: options.annotations,
ignoreComma: true,
comment: '#',
})
.map((flavor) => core.interpolate(flavor)),
flavor: core
.getInputList('flavor', { prefix, fallback: options.flavor, ignoreComma: true, comment: '#' })
.map((flavor) => core.interpolate(flavor)),
Expand Down
2 changes: 1 addition & 1 deletion packages/container-metadata/src/lib/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export function Transform(inputs: string[]): Image[] {
}

function output(images: Image[]): Image[] {
const group = `Processing images input`;
const group = 'Processing images input';
logger.startGroup(group);
for (const image of images) {
logger.info(`name=${image.name},enable=${image.enable}`);
Expand Down
57 changes: 46 additions & 11 deletions packages/container-metadata/src/lib/main.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { RunnerContext as Context, ContextProxyFactory, RepoMetadata, RepoProxyFactory } from '@nx-tools/ci-context';
import { logger } from '@nx-tools/core';
import { isDebug, logger } from '@nx-tools/core';
import { ExecutorContext } from '@nx/devkit';
import * as fs from 'node:fs';
import { Inputs, getInputs } from './context';
import { Meta, Version } from './meta';

function setOutput(name: string, value: string) {
// core.setOutput(name, value);
// core.exportVariable(`DOCKER_METADATA_OUTPUT_${name.replace(/\W/g, '_').toUpperCase()}`, value);
}

export async function getMetadata(options: Partial<Inputs>, ctx?: ExecutorContext): Promise<Meta> {
const inputs: Inputs = getInputs(options, ctx);
if (inputs.images.length == 0) {
throw new Error(`images input required`);
}

const context: Context = await ContextProxyFactory.create();
const repo: RepoMetadata = await RepoProxyFactory.create(inputs['github-token']);

Expand All @@ -23,6 +24,12 @@ export async function getMetadata(options: Partial<Inputs>, ctx?: ExecutorContex
logger.info(`runId: ${context.runId}`);
});

if (isDebug()) {
await logger.group(`Context payload`, async () => {
logger.info(JSON.stringify(context.payload, null, 2));
});
}

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

const version: Version = meta.version;
Expand All @@ -33,6 +40,7 @@ export async function getMetadata(options: Partial<Inputs>, ctx?: ExecutorContex
logger.info(version.main || '');
});
}
setOutput('version', version.main || '');

// Docker tags
const tags: Array<string> = meta.getTags();
Expand All @@ -45,26 +53,53 @@ export async function getMetadata(options: Partial<Inputs>, ctx?: ExecutorContex
}
});
}
setOutput('tags', tags.join(inputs['sep-tags']));

// Docker labels
const labels: Array<string> = meta.getLabels();
await logger.group(`Docker labels`, async () => {
for (const label of labels) {
logger.info(label);
}
setOutput('labels', labels.join(inputs['sep-labels']));
});

// Annotations
const annotationsRaw: Array<string> = meta.getAnnotations();
const annotationsLevels = process.env.DOCKER_METADATA_ANNOTATIONS_LEVELS || 'manifest';
await logger.group(`Annotations`, async () => {
const annotations: Array<string> = [];
for (const level of annotationsLevels.split(',')) {
annotations.push(
...annotationsRaw.map((label) => {
const v = `${level}:${label}`;
logger.info(v);
return v;
})
);
}
setOutput(`annotations`, annotations.join(inputs['sep-annotations']));
});

// JSON
const jsonOutput = meta.getJSON();
const jsonOutput = meta.getJSON(annotationsLevels.split(','));
await logger.group(`JSON output`, async () => {
logger.info(JSON.stringify(jsonOutput, null, 2));
setOutput('json', JSON.stringify(jsonOutput));
});

// Bake file definition
const bakeFile: string = meta.getBakeFile();
await logger.group(`Bake file definition`, async () => {
logger.info(fs.readFileSync(bakeFile, 'utf8'));
});
// Bake files
for (const kind of ['tags', 'labels', 'annotations:' + annotationsLevels]) {
const outputName = kind.split(':')[0];
const bakeFile: string = meta.getBakeFile(kind);
await logger.group(`Bake file definition (${outputName})`, async () => {
logger.info(fs.readFileSync(bakeFile, 'utf8'));
setOutput(`bake-file-${outputName}`, bakeFile);
});
}

// Bake file with tags and labels
setOutput(`bake-file`, `${meta.getBakeFileTagsLabels()}`);

return meta;
}
Loading

0 comments on commit 1c8187e

Please sign in to comment.