feat: allows to specify context to fetch git data

Signed-off-by: Emilien Escalle <emilien.escalle@escemi.com>
This commit is contained in:
Emilien Escalle 2022-12-15 14:46:34 +01:00
parent 9ec57ed1fc
commit 8f8c795666
9 changed files with 177 additions and 73 deletions

View File

@ -22,6 +22,7 @@ ___
* [inputs](#inputs) * [inputs](#inputs)
* [outputs](#outputs) * [outputs](#outputs)
* [environment variables](#environment-variables) * [environment variables](#environment-variables)
* [`context` input](#context-input)
* [`images` input](#images-input) * [`images` input](#images-input)
* [`flavor` input](#flavor-input) * [`flavor` input](#flavor-input)
* [`tags` input](#tags-input) * [`tags` input](#tags-input)
@ -276,7 +277,8 @@ Following inputs can be used as `step.with` keys
> ``` > ```
| Name | Type | Description | | Name | Type | Description |
|---------------------|--------|----------------------------------------------------------| |---------------------|--------|-------------------------------------------------------------------------------|
| `context` | String | Where to get context data. Allowed options are: `workflow` (default), `git`. |
| `images` | List | List of Docker images to use as base name for tags | | `images` | List | List of Docker images to use as base name for tags |
| `tags` | List | List of [tags](#tags-input) as key-value pair attributes | | `tags` | List | List of [tags](#tags-input) as key-value pair attributes |
| `flavor` | List | [Flavor](#flavor-input) to apply | | `flavor` | List | [Flavor](#flavor-input) to apply |
@ -320,6 +322,20 @@ So it can be used with our [Docker Build Push action](https://github.com/docker/
|-------------------------------|------|------------------------------------------------------------------------------------------------------------| |-------------------------------|------|------------------------------------------------------------------------------------------------------------|
| `DOCKER_METADATA_PR_HEAD_SHA` | Bool | If `true`, set associated head SHA instead of commit SHA that triggered the workflow on pull request event | | `DOCKER_METADATA_PR_HEAD_SHA` | Bool | If `true`, set associated head SHA instead of commit SHA that triggered the workflow on pull request event |
## `context` input
`context` defines where to get context metadata:
```yaml
# default
context: workflow
# or
context: git
```
* `workflow`: Get context metadata from the workflow (GitHub context). See https://docs.github.com/en/actions/learn-github-actions/contexts#github-context
* `git`: Get context metadata from the workflow and overrides some of them with current Git context, such as `ref` and `sha`.
## `images` input ## `images` input
`images` defines a list of Docker images to use as base name for [`tags`](#tags-input): `images` defines a list of Docker images to use as base name for [`tags`](#tags-input):

View File

@ -1,6 +1,9 @@
import {beforeEach, describe, expect, test} from '@jest/globals'; import {Context} from '@actions/github/lib/context';
import {beforeEach, describe, expect, test, it, jest} from '@jest/globals';
import * as context from '../src/context'; import * as dotenv from 'dotenv';
import * as fs from 'fs';
import * as path from 'path';
import {ContextSource, getInputs, Inputs} from '../src/context';
describe('getInputs', () => { describe('getInputs', () => {
beforeEach(() => { beforeEach(() => {
@ -20,6 +23,7 @@ describe('getInputs', () => {
['images', 'moby/buildkit\nghcr.io/moby/mbuildkit'], ['images', 'moby/buildkit\nghcr.io/moby/mbuildkit'],
]), ]),
{ {
context: ContextSource.workflow,
bakeTarget: 'docker-metadata-action', bakeTarget: 'docker-metadata-action',
flavor: [], flavor: [],
githubToken: '', githubToken: '',
@ -28,7 +32,7 @@ describe('getInputs', () => {
sepLabels: '\n', sepLabels: '\n',
sepTags: '\n', sepTags: '\n',
tags: [], tags: [],
} as context.Inputs } as Inputs
], ],
[ [
1, 1,
@ -39,6 +43,7 @@ describe('getInputs', () => {
['sep-tags', ','], ['sep-tags', ','],
]), ]),
{ {
context: ContextSource.workflow,
bakeTarget: 'metadata', bakeTarget: 'metadata',
flavor: [], flavor: [],
githubToken: '', githubToken: '',
@ -47,19 +52,54 @@ describe('getInputs', () => {
sepLabels: ',', sepLabels: ',',
sepTags: ',', sepTags: ',',
tags: [], tags: [],
} as context.Inputs } as Inputs
] ]
])( ])(
'[%d] given %p as inputs, returns %p', '[%d] given %p as inputs, returns %p',
async (num: number, inputs: Map<string, string>, expected: context.Inputs) => { async (num: number, inputs: Map<string, string>, expected: Inputs) => {
inputs.forEach((value: string, name: string) => { inputs.forEach((value: string, name: string) => {
setInput(name, value); setInput(name, value);
}); });
expect(await context.getInputs()).toEqual(expected); expect(await getInputs()).toEqual(expected);
} }
); );
}); });
describe('getContext', () => {
it('get context with workflow', async () => {
process.env = dotenv.parse(fs.readFileSync(path.join(__dirname, 'fixtures/event_create_branch.env')));
jest.resetModules();
// eslint-disable-next-line @typescript-eslint/no-var-requires
const {getContext} = require('../src/context');
const contextResult = await getContext(ContextSource.workflow);
expect(contextResult.ref).toEqual('refs/heads/dev');
expect(contextResult.sha).toEqual('5f3331d7f7044c18ca9f12c77d961c4d7cf3276a');
});
it('get context with git', async () => {
jest.resetModules();
// eslint-disable-next-line @typescript-eslint/no-var-requires
const git = require('@docker/actions-toolkit/lib/git');
jest.spyOn(git.Git, 'context').mockImplementation((): Promise<Context> => {
return Promise.resolve({
ref: 'refs/heads/git-test',
sha: 'git-test-sha'
} as Context);
});
// eslint-disable-next-line @typescript-eslint/no-var-requires
const {getContext} = require('../src/context');
const contextResult = await getContext(ContextSource.git);
expect(contextResult.ref).toEqual('refs/heads/git-test');
expect(contextResult.sha).toEqual('git-test-sha');
});
});
// See: https://github.com/actions/toolkit/blob/master/packages/core/src/core.ts#L67 // See: https://github.com/actions/toolkit/blob/master/packages/core/src/core.ts#L67
function getInputName(name: string): string { function getInputName(name: string): string {
return `INPUT_${name.replace(/ /g, '_').toUpperCase()}`; return `INPUT_${name.replace(/ /g, '_').toUpperCase()}`;

View File

@ -2,12 +2,10 @@ import {beforeEach, describe, expect, jest, test} from '@jest/globals';
import * as fs from 'fs'; import * as fs from 'fs';
import * as path from 'path'; import * as path from 'path';
import * as dotenv from 'dotenv'; import * as dotenv from 'dotenv';
import {Context} from '@actions/github/lib/context';
import {GitHub} from '@docker/actions-toolkit/lib/github'; import {GitHub} from '@docker/actions-toolkit/lib/github';
import {Toolkit} from '@docker/actions-toolkit/lib/toolkit'; import {Toolkit} from '@docker/actions-toolkit/lib/toolkit';
import {GitHubRepo} from '@docker/actions-toolkit/lib/types/github'; import {GitHubRepo} from '@docker/actions-toolkit/lib/types/github';
import {ContextSource, getInputs, Inputs} from '../src/context';
import {getInputs, Inputs} from '../src/context';
import {Meta, Version} from '../src/meta'; import {Meta, Version} from '../src/meta';
import repoFixture from './fixtures/repo.json'; import repoFixture from './fixtures/repo.json';
@ -23,6 +21,17 @@ jest.mock('moment-timezone', () => {
return () => (jest.requireActual('moment-timezone') as typeof import('moment-timezone'))('2020-01-10T00:30:00.000Z'); return () => (jest.requireActual('moment-timezone') as typeof import('moment-timezone'))('2020-01-10T00:30:00.000Z');
}); });
/**
* Get a workflow context based on the current environment variables.
*/
const getFreshWorkflowContext = async () => {
jest.resetModules();
// eslint-disable-next-line @typescript-eslint/no-var-requires
const {getContext} = require('../src/context');
const context = await getContext(ContextSource.workflow);
return context;
};
beforeEach(() => { beforeEach(() => {
jest.clearAllMocks(); jest.clearAllMocks();
Object.keys(process.env).forEach(function (key) { Object.keys(process.env).forEach(function (key) {
@ -49,7 +58,8 @@ const tagsLabelsTest = async (name: string, envFile: string, inputs: Inputs, exV
process.env = dotenv.parse(fs.readFileSync(path.join(__dirname, 'fixtures', envFile))); process.env = dotenv.parse(fs.readFileSync(path.join(__dirname, 'fixtures', envFile)));
const toolkit = new Toolkit(); const toolkit = new Toolkit();
const repo = await toolkit.github.repoData(); const repo = await toolkit.github.repoData();
const meta = new Meta({...getInputs(), ...inputs}, new Context(), repo); const context = await getFreshWorkflowContext();
const meta = new Meta({...getInputs(), ...inputs}, context, repo);
const version = meta.version; const version = meta.version;
expect(version).toEqual(exVersion); expect(version).toEqual(exVersion);
@ -2766,7 +2776,8 @@ describe('pr-head-sha', () => {
const toolkit = new Toolkit(); const toolkit = new Toolkit();
const repo = await toolkit.github.repoData(); const repo = await toolkit.github.repoData();
const meta = new Meta({...getInputs(), ...inputs}, new Context(), repo); const context = await getFreshWorkflowContext();
const meta = new Meta({...getInputs(), ...inputs}, context, repo);
const version = meta.version; const version = meta.version;
expect(version).toEqual(exVersion); expect(version).toEqual(exVersion);
@ -3708,7 +3719,8 @@ describe('json', () => {
const toolkit = new Toolkit(); const toolkit = new Toolkit();
const repo = await toolkit.github.repoData(); const repo = await toolkit.github.repoData();
const meta = new Meta({...getInputs(), ...inputs}, new Context(), repo); const context = await getFreshWorkflowContext();
const meta = new Meta({...getInputs(), ...inputs}, context, repo);
const jsonOutput = meta.getJSON(); const jsonOutput = meta.getJSON();
expect(jsonOutput).toEqual(exJSON); expect(jsonOutput).toEqual(exJSON);
@ -4014,7 +4026,8 @@ describe('bake', () => {
const toolkit = new Toolkit(); const toolkit = new Toolkit();
const repo = await toolkit.github.repoData(); const repo = await toolkit.github.repoData();
const meta = new Meta({...getInputs(), ...inputs}, new Context(), repo); const context = await getFreshWorkflowContext();
const meta = new Meta({...getInputs(), ...inputs}, context, repo);
const bakeFile = meta.getBakeFile(); const bakeFile = meta.getBakeFile();
expect(JSON.parse(fs.readFileSync(bakeFile, 'utf8'))).toEqual(exBakeDefinition); expect(JSON.parse(fs.readFileSync(bakeFile, 'utf8'))).toEqual(exBakeDefinition);
@ -4056,11 +4069,13 @@ describe('sepTags', () => {
"user/app:dev,user/app:my,user/app:custom,user/app:tags" "user/app:dev,user/app:my,user/app:custom,user/app:tags"
] ]
])('given %p with %p event', async (name: string, envFile: string, inputs: Inputs, expTags: string) => { ])('given %p with %p event', async (name: string, envFile: string, inputs: Inputs, expTags: string) => {
process.env = dotenv.parse(fs.readFileSync(path.join(__dirname, 'fixtures', envFile))); process.env = dotenv.parse(fs.readFileSync(path.join(__dirname, 'fixtures', envFile)));
const toolkit = new Toolkit(); const toolkit = new Toolkit();
const repo = await toolkit.github.repoData(); const repo = await toolkit.github.repoData();
const meta = new Meta({...getInputs(), ...inputs}, new Context(), repo); const context = await getFreshWorkflowContext();
const meta = new Meta({...getInputs(), ...inputs}, context, repo);
expect(meta.getTags().join(inputs.sepTags)).toEqual(expTags); expect(meta.getTags().join(inputs.sepTags)).toEqual(expTags);
}); });

View File

@ -7,6 +7,10 @@ branding:
color: 'blue' color: 'blue'
inputs: inputs:
context:
description: 'Where to get context data. Allowed options are "workflow" (default), "git".'
default: "workflow"
required: true
images: images:
description: 'List of Docker images to use as base name for tags' description: 'List of Docker images to use as base name for tags'
required: true required: true

22
dist/index.js generated vendored

File diff suppressed because one or more lines are too long

2
dist/index.js.map generated vendored

File diff suppressed because one or more lines are too long

View File

@ -1,7 +1,11 @@
import * as core from '@actions/core'; import * as core from '@actions/core';
import {Context} from '@actions/github/lib/context';
import {Util} from '@docker/actions-toolkit/lib/util'; import {Util} from '@docker/actions-toolkit/lib/util';
import {Git} from '@docker/actions-toolkit/lib/git';
import {GitHub} from '@docker/actions-toolkit/lib/github';
export interface Inputs { export interface Inputs {
context: ContextSource;
images: string[]; images: string[];
tags: string[]; tags: string[];
flavor: string[]; flavor: string[];
@ -14,6 +18,7 @@ export interface Inputs {
export function getInputs(): Inputs { export function getInputs(): Inputs {
return { return {
context: (core.getInput('context') || ContextSource.workflow) as ContextSource,
images: Util.getInputList('images', {ignoreComma: true}), images: Util.getInputList('images', {ignoreComma: true}),
tags: Util.getInputList('tags', {ignoreComma: true}), tags: Util.getInputList('tags', {ignoreComma: true}),
flavor: Util.getInputList('flavor', {ignoreComma: true}), flavor: Util.getInputList('flavor', {ignoreComma: true}),
@ -24,3 +29,45 @@ export function getInputs(): Inputs {
githubToken: core.getInput('github-token') githubToken: core.getInput('github-token')
}; };
} }
export enum ContextSource {
workflow = 'workflow',
git = 'git'
}
export async function getContext(source: ContextSource): Promise<Context> {
switch (source) {
case ContextSource.workflow:
return getContextFromWorkflow();
case ContextSource.git:
return await getContextFromGit();
default:
throw new Error(`Invalid context source: ${source}`);
}
}
function getContextFromWorkflow(): Context {
const context = GitHub.context;
// Needs to override Git reference with pr ref instead of upstream branch ref
// for pull_request_target event
// https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_target
if (/pull_request_target/.test(context.eventName)) {
context.ref = `refs/pull/${context.payload.number}/merge`;
}
// DOCKER_METADATA_PR_HEAD_SHA env var can be used to set associated head
// SHA instead of commit SHA that triggered the workflow on pull request
// event.
if (/true/i.test(process.env.DOCKER_METADATA_PR_HEAD_SHA || '')) {
if ((/pull_request/.test(context.eventName) || /pull_request_target/.test(context.eventName)) && context.payload?.pull_request?.head?.sha != undefined) {
context.sha = context.payload.pull_request.head.sha;
}
}
return context;
}
async function getContextFromGit(): Promise<Context> {
return await Git.context();
}

View File

@ -1,11 +1,9 @@
import * as fs from 'fs'; import * as fs from 'fs';
import * as core from '@actions/core'; import * as core from '@actions/core';
import * as actionsToolkit from '@docker/actions-toolkit'; import * as actionsToolkit from '@docker/actions-toolkit';
import {Context} from '@actions/github/lib/context';
import {GitHub} from '@docker/actions-toolkit/lib/github';
import {Toolkit} from '@docker/actions-toolkit/lib/toolkit'; import {Toolkit} from '@docker/actions-toolkit/lib/toolkit';
import {getInputs, Inputs} from './context'; import {getContext, getInputs, Inputs} from './context';
import {Meta, Version} from './meta'; import {Meta, Version} from './meta';
function setOutput(name: string, value: string) { function setOutput(name: string, value: string) {
@ -16,13 +14,13 @@ function setOutput(name: string, value: string) {
actionsToolkit.run( actionsToolkit.run(
// main // main
async () => { async () => {
const inputs: Inputs = await getInputs(); const inputs: Inputs = getInputs();
if (inputs.images.length == 0) { if (inputs.images.length == 0) {
throw new Error(`images input required`); throw new Error(`images input required`);
} }
const toolkit = new Toolkit({githubToken: inputs.githubToken}); const toolkit = new Toolkit({githubToken: inputs.githubToken});
const context: Context = GitHub.context; const context = await getContext(inputs.context);
const repo = await toolkit.github.repoData(); const repo = await toolkit.github.repoData();
await core.group(`Context info`, async () => { await core.group(`Context info`, async () => {

View File

@ -32,22 +32,6 @@ export class Meta {
private readonly date: Date; private readonly date: Date;
constructor(inputs: Inputs, context: Context, repo: GitHubRepo) { constructor(inputs: Inputs, context: Context, repo: GitHubRepo) {
// Needs to override Git reference with pr ref instead of upstream branch ref
// for pull_request_target event
// https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_target
if (/pull_request_target/.test(context.eventName)) {
context.ref = `refs/pull/${context.payload.number}/merge`;
}
// DOCKER_METADATA_PR_HEAD_SHA env var can be used to set associated head
// SHA instead of commit SHA that triggered the workflow on pull request
// event.
if (/true/i.test(process.env.DOCKER_METADATA_PR_HEAD_SHA || '')) {
if ((/pull_request/.test(context.eventName) || /pull_request_target/.test(context.eventName)) && context.payload?.pull_request?.head?.sha != undefined) {
context.sha = context.payload.pull_request.head.sha;
}
}
this.inputs = inputs; this.inputs = inputs;
this.context = context; this.context = context;
this.repo = repo; this.repo = repo;
@ -373,53 +357,53 @@ export class Meta {
} }
private setGlobalExp(val): string { private setGlobalExp(val): string {
const ctx = this.context; const context = this.context;
const currentDate = this.date; const currentDate = this.date;
return handlebars.compile(val)({ return handlebars.compile(val)({
branch: function () { branch: function () {
if (!/^refs\/heads\//.test(ctx.ref)) { if (!/^refs\/heads\//.test(context.ref)) {
return ''; return '';
} }
return ctx.ref.replace(/^refs\/heads\//g, ''); return context.ref.replace(/^refs\/heads\//g, '');
}, },
tag: function () { tag: function () {
if (!/^refs\/tags\//.test(ctx.ref)) { if (!/^refs\/tags\//.test(context.ref)) {
return ''; return '';
} }
return ctx.ref.replace(/^refs\/tags\//g, ''); return context.ref.replace(/^refs\/tags\//g, '');
}, },
sha: function () { sha: function () {
return ctx.sha.substring(0, 7); return context.sha.substring(0, 7);
}, },
base_ref: function () { base_ref: function () {
if (/^refs\/tags\//.test(ctx.ref) && ctx.payload?.base_ref != undefined) { if (/^refs\/tags\//.test(context.ref) && context.payload?.base_ref != undefined) {
return ctx.payload.base_ref.replace(/^refs\/heads\//g, ''); return context.payload.base_ref.replace(/^refs\/heads\//g, '');
} }
// FIXME: keep this for backward compatibility even if doesn't always seem // FIXME: keep this for backward compatibility even if doesn't always seem
// to return the expected branch. See the comment below. // to return the expected branch. See the comment below.
if (/^refs\/pull\//.test(ctx.ref) && ctx.payload?.pull_request?.base?.ref != undefined) { if (/^refs\/pull\//.test(context.ref) && context.payload?.pull_request?.base?.ref != undefined) {
return ctx.payload.pull_request.base.ref; return context.payload.pull_request.base.ref;
} }
return ''; return '';
}, },
is_default_branch: function () { is_default_branch: function () {
const branch = ctx.ref.replace(/^refs\/heads\//g, ''); const branch = context.ref.replace(/^refs\/heads\//g, '');
// TODO: "base_ref" is available in the push payload but doesn't always seem to // TODO: "base_ref" is available in the push payload but doesn't always seem to
// return the expected branch when the push tag event occurs. It's also not // return the expected branch when the push tag event occurs. It's also not
// documented in GitHub docs: https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#push // documented in GitHub docs: https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#push
// more context: https://github.com/docker/metadata-action/pull/192#discussion_r854673012 // more context: https://github.com/docker/metadata-action/pull/192#discussion_r854673012
// if (/^refs\/tags\//.test(ctx.ref) && ctx.payload?.base_ref != undefined) { // if (/^refs\/tags\//.test(context.ref) && context.payload?.base_ref != undefined) {
// branch = ctx.payload.base_ref.replace(/^refs\/heads\//g, ''); // branch = context.payload.base_ref.replace(/^refs\/heads\//g, '');
// } // }
if (branch == undefined || branch.length == 0) { if (branch == undefined || branch.length == 0) {
return 'false'; return 'false';
} }
if (ctx.payload?.repository?.default_branch == branch) { if (context.payload?.repository?.default_branch == branch) {
return 'true'; return 'true';
} }
// following events always trigger for last commit on default branch // following events always trigger for last commit on default branch
// https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows // https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows
if (/create/.test(ctx.eventName) || /discussion/.test(ctx.eventName) || /issues/.test(ctx.eventName) || /schedule/.test(ctx.eventName)) { if (/create/.test(context.eventName) || /discussion/.test(context.eventName) || /issues/.test(context.eventName) || /schedule/.test(context.eventName)) {
return 'true'; return 'true';
} }
return 'false'; return 'false';