Add image version output

This commit is contained in:
CrazyMax 2020-10-25 02:40:42 +01:00
parent 90ec551e12
commit 03eb07f964
No known key found for this signature in database
GPG Key ID: 3248E46B6BB8C7F7
7 changed files with 81 additions and 52 deletions

View File

@ -20,17 +20,6 @@ FROM base AS build
RUN yarn build
FROM deps AS test
COPY --from=docker /usr/local/bin/docker /usr/bin/
ARG TARGETOS
ARG TARGETARCH
ARG BUILDX_VERSION=v0.4.2
ENV RUNNER_TEMP=/tmp/github_runner
ENV RUNNER_TOOL_CACHE=/tmp/github_tool_cache
RUN mkdir -p /usr/local/lib/docker/cli-plugins && \
curl -fsSL https://github.com/docker/buildx/releases/download/$BUILDX_VERSION/buildx-$BUILDX_VERSION.$TARGETOS-$TARGETARCH > /usr/local/lib/docker/cli-plugins/docker-buildx && \
chmod +x /usr/local/lib/docker/cli-plugins/docker-buildx && \
docker buildx version
COPY . .
RUN yarn run test
FROM base AS run-format

View File

@ -116,6 +116,7 @@ Following outputs are available
| Name | Type | Description |
|---------------|---------|---------------------------------------|
| `version` | String | Generated Docker image version |
| `tags` | String | Generated Docker tags |
| `labels` | String | Generated Docker labels |

View File

@ -39,6 +39,7 @@ describe('tags and labels', () => {
{
images: ['user/app'],
},
undefined,
[],
[
"org.opencontainers.image.title=Hello-World",
@ -56,6 +57,7 @@ describe('tags and labels', () => {
{
images: ['user/app'],
},
undefined,
[],
[
"org.opencontainers.image.title=Hello-World",
@ -73,6 +75,7 @@ describe('tags and labels', () => {
{
images: ['user/app'],
},
'pr-2',
[
'user/app:pr-2'
],
@ -92,6 +95,7 @@ describe('tags and labels', () => {
{
images: ['user/app'],
},
'dev',
[
'user/app:dev'
],
@ -111,6 +115,7 @@ describe('tags and labels', () => {
{
images: ['user/app'],
},
'edge',
[
'user/app:edge'
],
@ -130,6 +135,7 @@ describe('tags and labels', () => {
{
images: ['user/app'],
},
'1.1.1',
[
'user/app:1.1.1',
'user/app:latest'
@ -150,6 +156,7 @@ describe('tags and labels', () => {
{
images: ['user/app'],
},
'nightly',
[
'user/app:nightly'
],
@ -169,6 +176,7 @@ describe('tags and labels', () => {
{
images: ['user/app'],
},
'release1',
[
'user/app:release1'
],
@ -188,6 +196,7 @@ describe('tags and labels', () => {
{
images: ['user/app'],
},
'1.1.1',
[
'user/app:1.1.1',
'user/app:latest'
@ -208,6 +217,7 @@ describe('tags and labels', () => {
{
images: ['user/app'],
},
'edge',
[
'user/app:edge'
],
@ -227,6 +237,7 @@ describe('tags and labels', () => {
{
images: ['org/app', 'ghcr.io/user/app'],
},
'pr-2',
[
'org/app:pr-2',
'ghcr.io/user/app:pr-2'
@ -247,6 +258,7 @@ describe('tags and labels', () => {
{
images: ['org/app', 'ghcr.io/user/app'],
},
'dev',
[
'org/app:dev',
'ghcr.io/user/app:dev'
@ -267,6 +279,7 @@ describe('tags and labels', () => {
{
images: ['org/app', 'ghcr.io/user/app'],
},
'edge',
[
'org/app:edge',
'ghcr.io/user/app:edge'
@ -287,6 +300,7 @@ describe('tags and labels', () => {
{
images: ['org/app', 'ghcr.io/user/app'],
},
'nightly',
[
'org/app:nightly',
'ghcr.io/user/app:nightly'
@ -307,6 +321,7 @@ describe('tags and labels', () => {
{
images: ['org/app', 'ghcr.io/user/app'],
},
'1.1.1',
[
'org/app:1.1.1',
'org/app:latest',
@ -330,6 +345,7 @@ describe('tags and labels', () => {
images: ['org/app', 'ghcr.io/user/app'],
tagSha: true,
},
'pr-2',
[
'org/app:pr-2',
'org/app:sha-1e9249f',
@ -353,6 +369,7 @@ describe('tags and labels', () => {
images: ['org/app', 'ghcr.io/user/app'],
tagSha: true,
},
'dev',
[
'org/app:dev',
'org/app:sha-90dd603',
@ -376,6 +393,7 @@ describe('tags and labels', () => {
images: ['org/app', 'ghcr.io/user/app'],
tagSha: true,
},
'edge',
[
'org/app:edge',
'org/app:sha-90dd603',
@ -399,6 +417,7 @@ describe('tags and labels', () => {
images: ['org/app', 'ghcr.io/user/app'],
tagSha: true,
},
'nightly',
[
'org/app:nightly',
'org/app:sha-90dd603',
@ -422,6 +441,7 @@ describe('tags and labels', () => {
images: ['org/app', 'ghcr.io/user/app'],
tagSha: true,
},
'1.1.1',
[
'org/app:1.1.1',
'org/app:latest',
@ -448,6 +468,7 @@ describe('tags and labels', () => {
tagSha: true,
tagEdge: 'dev'
},
'edge',
[
'org/app:edge',
'org/app:sha-90dd603',
@ -472,6 +493,7 @@ describe('tags and labels', () => {
tagSha: true,
tagEdge: 'dev'
},
'master',
[
'org/app:master',
'org/app:sha-90dd603',
@ -489,7 +511,7 @@ describe('tags and labels', () => {
"org.opencontainers.image.licenses=MIT"
]
],
])('given %p event ', async (envFile, inputs, exTags, exLabels) => {
])('given %p event ', async (envFile, inputs, exVersion, exTags, exLabels) => {
process.env = dotenv.parse(fs.readFileSync(path.join(__dirname, 'fixtures', envFile)));
console.log(process.env);
@ -499,6 +521,10 @@ describe('tags and labels', () => {
const repo = await github.repo(process.env.GITHUB_TOKEN || '');
const meta = new Meta(inputs as Inputs, context, repo);
const version = meta.version();
console.log(version)
expect(version).toEqual(exVersion);
const tags = meta.tags();
console.log(tags)
expect(tags).toEqual(exTags);

View File

@ -31,6 +31,8 @@ inputs:
required: true
outputs:
version:
description: 'Generated Docker image version'
tags:
description: 'Generated Docker tags'
labels:

47
dist/index.js generated vendored
View File

@ -126,13 +126,18 @@ function run() {
core.info(`runId: ${context.runId}`);
core.endGroup();
const meta = new meta_1.Meta(inputs, context, repo);
const version = meta.version();
core.startGroup(`Docker image version`);
core.info(`${version}`);
core.endGroup();
core.setOutput('version', version);
const tags = meta.tags();
core.startGroup(`Generated Docker tags`);
core.startGroup(`Docker tags`);
core.info(JSON.stringify(tags));
core.endGroup();
core.setOutput('tags', tags.join(inputs.sepTags));
const labels = meta.labels();
core.startGroup(`Generated Docker labels`);
core.startGroup(`Docker labels`);
core.info(JSON.stringify(labels));
core.endGroup();
core.setOutput('labels', labels.join(inputs.sepTags));
@ -165,6 +170,24 @@ class Meta {
this.context = context;
this.repo = repo;
}
version() {
if (/schedule/.test(this.context.eventName)) {
return 'nightly';
}
else if (/^refs\/tags\//.test(this.context.ref)) {
const tag = this.context.ref.replace(/^refs\/tags\//g, '').replace(/\//g, '-');
const sver = semver.clean(tag);
return sver ? sver : tag;
}
else if (/^refs\/heads\//.test(this.context.ref)) {
const branch = this.context.ref.replace(/^refs\/heads\//g, '').replace(/\//g, '-');
return this.inputs.tagEdge === branch ? 'edge' : branch;
}
else if (/^refs\/pull\//.test(this.context.ref)) {
const pr = this.context.ref.replace(/^refs\/pull\//g, '').replace(/\/merge$/g, '');
return `pr-${pr}`;
}
}
tags() {
let tags = [];
for (const image of this.inputs.images) {
@ -196,7 +219,7 @@ class Meta {
`org.opencontainers.image.description=${this.repo.description || ''}`,
`org.opencontainers.image.url=${this.repo.html_url || ''}`,
`org.opencontainers.image.source=${this.repo.clone_url || ''}`,
`org.opencontainers.image.version=${this.labelVersion() || ''}`,
`org.opencontainers.image.version=${this.version() || ''}`,
`org.opencontainers.image.created=${new Date().toISOString()}`,
`org.opencontainers.image.revision=${this.context.sha || ''}`,
`org.opencontainers.image.licenses=${((_a = this.repo.license) === null || _a === void 0 ? void 0 : _a.spdx_id) || ''}`
@ -224,24 +247,6 @@ class Meta {
const pr = this.context.ref.replace(/^refs\/pull\//g, '').replace(/\/merge$/g, '');
return [`${image}:pr-${pr}`];
}
labelVersion() {
if (/schedule/.test(this.context.eventName)) {
return 'nightly';
}
else if (/^refs\/tags\//.test(this.context.ref)) {
const tag = this.context.ref.replace(/^refs\/tags\//g, '').replace(/\//g, '-');
const sver = semver.clean(tag);
return sver ? sver : tag;
}
else if (/^refs\/heads\//.test(this.context.ref)) {
const branch = this.context.ref.replace(/^refs\/heads\//g, '').replace(/\//g, '-');
return this.inputs.tagEdge === branch ? 'edge' : branch;
}
else if (/^refs\/pull\//.test(this.context.ref)) {
const pr = this.context.ref.replace(/^refs\/pull\//g, '').replace(/\/merge$/g, '');
return `pr-${pr}`;
}
}
}
exports.Meta = Meta;
//# sourceMappingURL=meta.js.map

View File

@ -28,14 +28,20 @@ async function run() {
const meta: Meta = new Meta(inputs, context, repo);
const version: string | undefined = meta.version();
core.startGroup(`Docker image version`);
core.info(`${version}`);
core.endGroup();
core.setOutput('version', version);
const tags: Array<string> = meta.tags();
core.startGroup(`Generated Docker tags`);
core.startGroup(`Docker tags`);
core.info(JSON.stringify(tags));
core.endGroup();
core.setOutput('tags', tags.join(inputs.sepTags));
const labels: Array<string> = meta.labels();
core.startGroup(`Generated Docker labels`);
core.startGroup(`Docker labels`);
core.info(JSON.stringify(labels));
core.endGroup();
core.setOutput('labels', labels.join(inputs.sepTags));

View File

@ -18,6 +18,22 @@ export class Meta {
this.repo = repo;
}
public version(): string | undefined {
if (/schedule/.test(this.context.eventName)) {
return 'nightly';
} else if (/^refs\/tags\//.test(this.context.ref)) {
const tag = this.context.ref.replace(/^refs\/tags\//g, '').replace(/\//g, '-');
const sver = semver.clean(tag);
return sver ? sver : tag;
} else if (/^refs\/heads\//.test(this.context.ref)) {
const branch = this.context.ref.replace(/^refs\/heads\//g, '').replace(/\//g, '-');
return this.inputs.tagEdge === branch ? 'edge' : branch;
} else if (/^refs\/pull\//.test(this.context.ref)) {
const pr = this.context.ref.replace(/^refs\/pull\//g, '').replace(/\/merge$/g, '');
return `pr-${pr}`;
}
}
public tags(): Array<string> {
let tags: Array<string> = [];
for (const image of this.inputs.images) {
@ -45,7 +61,7 @@ export class Meta {
`org.opencontainers.image.description=${this.repo.description || ''}`,
`org.opencontainers.image.url=${this.repo.html_url || ''}`,
`org.opencontainers.image.source=${this.repo.clone_url || ''}`,
`org.opencontainers.image.version=${this.labelVersion() || ''}`,
`org.opencontainers.image.version=${this.version() || ''}`,
`org.opencontainers.image.created=${new Date().toISOString()}`,
`org.opencontainers.image.revision=${this.context.sha || ''}`,
`org.opencontainers.image.licenses=${this.repo.license?.spdx_id || ''}`
@ -77,20 +93,4 @@ export class Meta {
const pr = this.context.ref.replace(/^refs\/pull\//g, '').replace(/\/merge$/g, '');
return [`${image}:pr-${pr}`];
}
private labelVersion(): string | undefined {
if (/schedule/.test(this.context.eventName)) {
return 'nightly';
} else if (/^refs\/tags\//.test(this.context.ref)) {
const tag = this.context.ref.replace(/^refs\/tags\//g, '').replace(/\//g, '-');
const sver = semver.clean(tag);
return sver ? sver : tag;
} else if (/^refs\/heads\//.test(this.context.ref)) {
const branch = this.context.ref.replace(/^refs\/heads\//g, '').replace(/\//g, '-');
return this.inputs.tagEdge === branch ? 'edge' : branch;
} else if (/^refs\/pull\//.test(this.context.ref)) {
const pr = this.context.ref.replace(/^refs\/pull\//g, '').replace(/\/merge$/g, '');
return `pr-${pr}`;
}
}
}