revert changes to set associated head sha on pull request event

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax 2022-10-17 11:49:09 +02:00
parent 12cce9efe0
commit a99e71cdc7
No known key found for this signature in database
GPG Key ID: 3248E46B6BB8C7F7
5 changed files with 381 additions and 5 deletions

View File

@ -20,6 +20,7 @@ ___
* [Customizing](#customizing) * [Customizing](#customizing)
* [inputs](#inputs) * [inputs](#inputs)
* [outputs](#outputs) * [outputs](#outputs)
* [environment variables](#environment-variables)
* [`images` input](#images-input) * [`images` input](#images-input)
* [`flavor` input](#flavor-input) * [`flavor` input](#flavor-input)
* [`tags` input](#tags-input) * [`tags` input](#tags-input)
@ -295,6 +296,12 @@ Following outputs are available
| `json` | String | JSON output of tags and labels | | `json` | String | JSON output of tags and labels |
| `bake-file` | File | [Bake file definition](https://docs.docker.com/build/customize/bake/file-definition/) path | | `bake-file` | File | [Bake file definition](https://docs.docker.com/build/customize/bake/file-definition/) path |
### environment variables
| Name | Type | Description |
|-------------------------------|------|------------------------------------------------------------------------------------------------------------|
| `DOCKER_METADATA_PR_HEAD_SHA` | Bool | If `true`, set associated head SHA instead of commit SHA that triggered the workflow on pull request event |
## `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

@ -2067,6 +2067,354 @@ describe('latest', () => {
}); });
describe('pr', () => { describe('pr', () => {
// prettier-ignore
// eslint-disable-next-line jest/expect-expect
test.each([
[
'pr01',
'event_pull_request.env',
{
images: ['user/app'],
} as Inputs,
{
main: 'pr-15',
partial: [],
latest: false
} as Version,
[
'user/app:pr-15'
],
[
"org.opencontainers.image.title=Hello-World",
"org.opencontainers.image.description=This your first repo!",
"org.opencontainers.image.url=https://github.com/octocat/Hello-World",
"org.opencontainers.image.source=https://github.com/octocat/Hello-World",
"org.opencontainers.image.version=pr-15",
"org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
"org.opencontainers.image.revision=a9c8c5828b91be19d9728548b24759e352367ef1",
"org.opencontainers.image.licenses=MIT"
]
],
[
'pr02',
'event_pull_request.env',
{
images: ['org/app', 'ghcr.io/user/app'],
} as Inputs,
{
main: 'pr-15',
partial: [],
latest: false
} as Version,
[
'org/app:pr-15',
'ghcr.io/user/app:pr-15'
],
[
"org.opencontainers.image.title=Hello-World",
"org.opencontainers.image.description=This your first repo!",
"org.opencontainers.image.url=https://github.com/octocat/Hello-World",
"org.opencontainers.image.source=https://github.com/octocat/Hello-World",
"org.opencontainers.image.version=pr-15",
"org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
"org.opencontainers.image.revision=a9c8c5828b91be19d9728548b24759e352367ef1",
"org.opencontainers.image.licenses=MIT"
]
],
[
'pr03',
'event_pull_request.env',
{
images: ['org/app', 'ghcr.io/user/app'],
tags: [
`type=ref,event=pr`,
`type=sha`
]
} as Inputs,
{
main: 'pr-15',
partial: ['sha-a9c8c58'],
latest: false
} as Version,
[
'org/app:pr-15',
'org/app:sha-a9c8c58',
'ghcr.io/user/app:pr-15',
'ghcr.io/user/app:sha-a9c8c58'
],
[
"org.opencontainers.image.title=Hello-World",
"org.opencontainers.image.description=This your first repo!",
"org.opencontainers.image.url=https://github.com/octocat/Hello-World",
"org.opencontainers.image.source=https://github.com/octocat/Hello-World",
"org.opencontainers.image.version=pr-15",
"org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
"org.opencontainers.image.revision=a9c8c5828b91be19d9728548b24759e352367ef1",
"org.opencontainers.image.licenses=MIT"
]
],
[
'pr04',
'event_pull_request.env',
{
images: ['org/app', 'ghcr.io/user/app'],
tags: [
`type=sha,priority=2000`,
`type=ref,event=pr`
]
} as Inputs,
{
main: 'sha-a9c8c58',
partial: ['pr-15'],
latest: false
} as Version,
[
'org/app:sha-a9c8c58',
'org/app:pr-15',
'ghcr.io/user/app:sha-a9c8c58',
'ghcr.io/user/app:pr-15'
],
[
"org.opencontainers.image.title=Hello-World",
"org.opencontainers.image.description=This your first repo!",
"org.opencontainers.image.url=https://github.com/octocat/Hello-World",
"org.opencontainers.image.source=https://github.com/octocat/Hello-World",
"org.opencontainers.image.version=sha-a9c8c58",
"org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
"org.opencontainers.image.revision=a9c8c5828b91be19d9728548b24759e352367ef1",
"org.opencontainers.image.licenses=MIT"
]
],
[
'pr05',
'event_pull_request.env',
{
images: ['org/app', 'ghcr.io/user/app'],
tags: [
`type=ref,event=pr`
],
flavor: [
`prefix=glo-`,
`suffix=-bal`
]
} as Inputs,
{
main: 'pr-15-bal',
partial: [],
latest: false
} as Version,
[
'org/app:pr-15-bal',
'ghcr.io/user/app:pr-15-bal'
],
[
"org.opencontainers.image.title=Hello-World",
"org.opencontainers.image.description=This your first repo!",
"org.opencontainers.image.url=https://github.com/octocat/Hello-World",
"org.opencontainers.image.source=https://github.com/octocat/Hello-World",
"org.opencontainers.image.version=pr-15-bal",
"org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
"org.opencontainers.image.revision=a9c8c5828b91be19d9728548b24759e352367ef1",
"org.opencontainers.image.licenses=MIT"
]
],
[
'pr06',
'event_pull_request.env',
{
images: ['org/app', 'ghcr.io/user/app'],
tags: [
`type=ref,event=pr,prefix=`
],
flavor: [
`prefix=glo-`,
`suffix=-bal`
]
} as Inputs,
{
main: '15-bal',
partial: [],
latest: false
} as Version,
[
'org/app:15-bal',
'ghcr.io/user/app:15-bal'
],
[
"org.opencontainers.image.title=Hello-World",
"org.opencontainers.image.description=This your first repo!",
"org.opencontainers.image.url=https://github.com/octocat/Hello-World",
"org.opencontainers.image.source=https://github.com/octocat/Hello-World",
"org.opencontainers.image.version=15-bal",
"org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
"org.opencontainers.image.revision=a9c8c5828b91be19d9728548b24759e352367ef1",
"org.opencontainers.image.licenses=MIT"
]
],
[
'pr07',
'event_pull_request_target.env',
{
images: ['org/app', 'ghcr.io/user/app'],
tags: [
`type=sha,priority=2000`,
`type=ref,event=pr`
]
} as Inputs,
{
main: 'sha-2665741',
partial: ['pr-15'],
latest: false
} as Version,
[
'org/app:sha-2665741',
'org/app:pr-15',
'ghcr.io/user/app:sha-2665741',
'ghcr.io/user/app:pr-15'
],
[
"org.opencontainers.image.title=Hello-World",
"org.opencontainers.image.description=This your first repo!",
"org.opencontainers.image.url=https://github.com/octocat/Hello-World",
"org.opencontainers.image.source=https://github.com/octocat/Hello-World",
"org.opencontainers.image.version=sha-2665741",
"org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
"org.opencontainers.image.revision=266574110acf203503badf966df2ea24b5d732d7",
"org.opencontainers.image.licenses=MIT"
]
],
[
'pr08',
'event_pull_request_target.env',
{
images: ['org/app', 'ghcr.io/user/app'],
tags: [
`type=ref,event=pr,prefix=`
],
flavor: [
`prefix=glo-`,
`suffix=-bal`
]
} as Inputs,
{
main: '15-bal',
partial: [],
latest: false
} as Version,
[
'org/app:15-bal',
'ghcr.io/user/app:15-bal'
],
[
"org.opencontainers.image.title=Hello-World",
"org.opencontainers.image.description=This your first repo!",
"org.opencontainers.image.url=https://github.com/octocat/Hello-World",
"org.opencontainers.image.source=https://github.com/octocat/Hello-World",
"org.opencontainers.image.version=15-bal",
"org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
"org.opencontainers.image.revision=266574110acf203503badf966df2ea24b5d732d7",
"org.opencontainers.image.licenses=MIT"
]
],
[
'pr09',
'event_pull_request_target.env',
{
images: ['org/app'],
tags: [
`type=ref,event=tag`,
`type=ref,event=pr`,
`type=ref,event=branch`,
`type=sha`,
`type=sha,format=long`
]
} as Inputs,
{
main: 'pr-15',
partial: [
'sha-2665741',
'sha-266574110acf203503badf966df2ea24b5d732d7'
],
latest: false
} as Version,
[
'org/app:pr-15',
'org/app:sha-2665741',
'org/app:sha-266574110acf203503badf966df2ea24b5d732d7'
],
[
"org.opencontainers.image.title=Hello-World",
"org.opencontainers.image.description=This your first repo!",
"org.opencontainers.image.url=https://github.com/octocat/Hello-World",
"org.opencontainers.image.source=https://github.com/octocat/Hello-World",
"org.opencontainers.image.version=pr-15",
"org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
"org.opencontainers.image.revision=266574110acf203503badf966df2ea24b5d732d7",
"org.opencontainers.image.licenses=MIT"
]
],
[
'pr10',
'event_pull_request_target.env',
{
images: ['org/app'],
tags: [
`type=raw,value=mytag-{{base_ref}}`,
`type=raw,mytag-defbranch,enable={{is_default_branch}}`
]
} as Inputs,
{
main: 'mytag-master',
partial: [],
latest: false
} as Version,
[
'org/app:mytag-master'
],
[
"org.opencontainers.image.title=Hello-World",
"org.opencontainers.image.description=This your first repo!",
"org.opencontainers.image.url=https://github.com/octocat/Hello-World",
"org.opencontainers.image.source=https://github.com/octocat/Hello-World",
"org.opencontainers.image.version=mytag-master",
"org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
"org.opencontainers.image.revision=266574110acf203503badf966df2ea24b5d732d7",
"org.opencontainers.image.licenses=MIT"
]
],
[
'pr11',
'event_pull_request.env',
{
images: ['org/app'],
tags: [
`type=raw,value=mytag-{{base_ref}}`,
`type=raw,mytag-defbranch,enable={{is_default_branch}}`
]
} as Inputs,
{
main: 'mytag-master',
partial: [],
latest: false
} as Version,
[
'org/app:mytag-master'
],
[
"org.opencontainers.image.title=Hello-World",
"org.opencontainers.image.description=This your first repo!",
"org.opencontainers.image.url=https://github.com/octocat/Hello-World",
"org.opencontainers.image.source=https://github.com/octocat/Hello-World",
"org.opencontainers.image.version=mytag-master",
"org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
"org.opencontainers.image.revision=a9c8c5828b91be19d9728548b24759e352367ef1",
"org.opencontainers.image.licenses=MIT"
]
],
])('given %p with %p event', tagsLabelsTest);
});
describe('pr-head-sha', () => {
// prettier-ignore // prettier-ignore
// eslint-disable-next-line jest/expect-expect // eslint-disable-next-line jest/expect-expect
test.each([ test.each([
@ -2411,7 +2759,23 @@ describe('pr', () => {
"org.opencontainers.image.licenses=MIT" "org.opencontainers.image.licenses=MIT"
] ]
], ],
])('given %p with %p event', tagsLabelsTest); ])('given %p with %p event', async (name: string, envFile: string, inputs: Inputs, exVersion: Version, exTags: Array<string>, exLabels: Array<string>) => {
process.env = dotenv.parse(fs.readFileSync(path.join(__dirname, 'fixtures', envFile)));
process.env.DOCKER_METADATA_PR_HEAD_SHA = 'true';
const context = github.context();
const repo = await github.repo(process.env.GITHUB_TOKEN || '');
const meta = new Meta({...getInputs(), ...inputs}, context, repo);
const version = meta.version;
expect(version).toEqual(exVersion);
const tags = meta.getTags();
expect(tags).toEqual(exTags);
const labels = meta.getLabels();
expect(labels).toEqual(exLabels);
});
}); });
describe('schedule', () => { describe('schedule', () => {

2
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

@ -37,9 +37,14 @@ export class Meta {
context.ref = `refs/pull/${context.payload.number}/merge`; 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) { 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; context.sha = context.payload.pull_request.head.sha;
} }
}
this.inputs = inputs; this.inputs = inputs;
this.context = context; this.context = context;