From 03eb07f964cfd25a420a4e1711b308b2732386ee Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Sun, 25 Oct 2020 02:40:42 +0100 Subject: [PATCH] Add image version output --- Dockerfile | 11 ---------- README.md | 1 + __tests__/meta.test.ts | 28 ++++++++++++++++++++++++- action.yml | 2 ++ dist/index.js | 47 +++++++++++++++++++++++------------------- src/main.ts | 10 +++++++-- src/meta.ts | 34 +++++++++++++++--------------- 7 files changed, 81 insertions(+), 52 deletions(-) diff --git a/Dockerfile b/Dockerfile index 85ed642..49a7366 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/README.md b/README.md index d7abff6..2a9afcf 100644 --- a/README.md +++ b/README.md @@ -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 | diff --git a/__tests__/meta.test.ts b/__tests__/meta.test.ts index 24571aa..a77462a 100644 --- a/__tests__/meta.test.ts +++ b/__tests__/meta.test.ts @@ -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); diff --git a/action.yml b/action.yml index 014f36f..6712b4e 100644 --- a/action.yml +++ b/action.yml @@ -31,6 +31,8 @@ inputs: required: true outputs: + version: + description: 'Generated Docker image version' tags: description: 'Generated Docker tags' labels: diff --git a/dist/index.js b/dist/index.js index b7422ee..e9945f1 100644 --- a/dist/index.js +++ b/dist/index.js @@ -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 diff --git a/src/main.ts b/src/main.ts index deb28bc..ff73bcf 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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 = 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 = 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)); diff --git a/src/meta.ts b/src/meta.ts index 5a44733..1cb39e8 100644 --- a/src/meta.ts +++ b/src/meta.ts @@ -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 { let tags: Array = []; 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}`; - } - } }