3
0
Fork 0
mirror of https://gitea.com/docker/metadata-action.git synced 2024-11-21 19:49:32 +01:00

Merge pull request #467 from crazy-max/increase-short-sha-size

increase short commit hash length to 12 for uniqueness
This commit is contained in:
CrazyMax 2024-10-31 11:05:50 +01:00 committed by GitHub
commit 9b90a2e882
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 138 additions and 72 deletions

View file

@ -549,3 +549,33 @@ jobs:
cwd://${{ steps.docker_meta.outputs.bake-file-labels }}
targets: |
release
sha-short:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
short-length:
- ''
- 16
steps:
-
name: Checkout
uses: actions/checkout@v4
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
version: ${{ env.BUILDX_VERSION }}
driver: docker
-
name: Docker meta
uses: ./
with:
images: |
${{ env.DOCKER_IMAGE }}
ghcr.io/name/app
tags: |
type=sha
env:
DOCKER_METADATA_SHORT_SHA_LENGTH: ${{ matrix.short-length }}

View file

@ -360,10 +360,11 @@ So it can be used with our [Docker Build Push action](https://github.com/docker/
### 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 |
| `DOCKER_METADATA_ANNOTATIONS_LEVELS` | String | Comma separated list of annotations levels to set for annotations output separated (default `manifest`) |
| 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 |
| `DOCKER_METADATA_SHORT_SHA_LENGTH` | Number | Specifies the length of the [short commit SHA](#typesha) to ensure uniqueness. Default is `12`, but can be increased for larger repositories. |
| `DOCKER_METADATA_ANNOTATIONS_LEVELS` | String | Comma separated list of annotations levels to set for annotations output separated (default `manifest`) |
## `context` input
@ -722,7 +723,26 @@ tags: |
type=sha,format=long
```
Output Git short commit (or long if specified) as Docker tag like `sha-ad132f5`.
Output Git short commit (or long if specified) as Docker tag like
`sha-860c1904a1ce`.
By default, the length of the short commit SHA is `12` characters. You can
increase this length for larger repositories by setting the
[`DOCKER_METADATA_SHORT_SHA_LENGTH` environment variable](#environment-variables):
```yaml
-
name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
images: |
name/app
tags: |
type=sha
env:
DOCKER_METADATA_SHORT_SHA_LENGTH: 16
```
Extended attributes and default values:

View file

@ -310,14 +310,14 @@ describe('push', () => {
} as Inputs,
{
main: 'dev',
partial: ['sha-860c190'],
partial: ['sha-860c1904a1ce'],
latest: false
} as Version,
[
'org/app:dev',
'org/app:sha-860c190',
'org/app:sha-860c1904a1ce',
'ghcr.io/user/app:dev',
'ghcr.io/user/app:sha-860c190'
'ghcr.io/user/app:sha-860c1904a1ce'
],
[
"org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
@ -343,14 +343,14 @@ describe('push', () => {
} as Inputs,
{
main: 'edge',
partial: ['sha-2665741'],
partial: ['sha-266574110acf'],
latest: false
} as Version,
[
'org/app:edge',
'org/app:sha-2665741',
'org/app:sha-266574110acf',
'ghcr.io/user/app:edge',
'ghcr.io/user/app:sha-2665741'
'ghcr.io/user/app:sha-266574110acf'
],
[
"org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
@ -376,14 +376,14 @@ describe('push', () => {
} as Inputs,
{
main: 'edge',
partial: ['sha-860c190'],
partial: ['sha-860c1904a1ce'],
latest: false
} as Version,
[
'org/app:edge',
'org/app:sha-860c190',
'org/app:sha-860c1904a1ce',
'ghcr.io/user/app:edge',
'ghcr.io/user/app:sha-860c190'
'ghcr.io/user/app:sha-860c1904a1ce'
],
[
"org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
@ -408,13 +408,13 @@ describe('push', () => {
],
} as Inputs,
{
main: 'sha-2665741',
main: 'sha-266574110acf',
partial: [],
latest: false
} as Version,
[
'org/app:sha-2665741',
'ghcr.io/user/app:sha-2665741'
'org/app:sha-266574110acf',
'ghcr.io/user/app:sha-266574110acf'
],
[
"org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
@ -424,7 +424,7 @@ describe('push', () => {
"org.opencontainers.image.source=https://github.com/octocat/Hello-World",
"org.opencontainers.image.title=Hello-World",
"org.opencontainers.image.url=https://github.com/octocat/Hello-World",
"org.opencontainers.image.version=sha-2665741"
"org.opencontainers.image.version=sha-266574110acf"
],
undefined
],
@ -439,13 +439,13 @@ describe('push', () => {
],
} as Inputs,
{
main: 'sha-983315b',
main: 'sha-983315b5e8d4',
partial: [],
latest: false
} as Version,
[
'org/app:sha-983315b',
'ghcr.io/user/app:sha-983315b'
'org/app:sha-983315b5e8d4',
'ghcr.io/user/app:sha-983315b5e8d4'
],
[
"org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
@ -455,7 +455,7 @@ describe('push', () => {
"org.opencontainers.image.source=https://github.com/octocat/Hello-World",
"org.opencontainers.image.title=Hello-World",
"org.opencontainers.image.url=https://github.com/octocat/Hello-World",
"org.opencontainers.image.version=sha-983315b"
"org.opencontainers.image.version=sha-983315b5e8d4"
],
undefined
],
@ -1341,17 +1341,17 @@ describe('tag', () => {
} as Inputs,
{
main: '1.0.0',
partial: ['1.0', 'sha-860c190'],
partial: ['1.0', 'sha-860c1904a1ce'],
latest: true
} as Version,
[
'org/app:1.0.0',
'org/app:1.0',
'org/app:sha-860c190',
'org/app:sha-860c1904a1ce',
'org/app:latest',
'ghcr.io/user/app:1.0.0',
'ghcr.io/user/app:1.0',
'ghcr.io/user/app:sha-860c190',
'ghcr.io/user/app:sha-860c1904a1ce',
'ghcr.io/user/app:latest'
],
[
@ -1381,17 +1381,17 @@ describe('tag', () => {
} as Inputs,
{
main: '1.0.0',
partial: ['1.0', 'sha-860c190'],
partial: ['1.0', 'sha-860c1904a1ce'],
latest: true
} as Version,
[
'org/app:1.0.0',
'org/app:1.0',
'org/app:sha-860c190',
'org/app:sha-860c1904a1ce',
'org/app:latest',
'ghcr.io/user/app:1.0.0',
'ghcr.io/user/app:1.0',
'ghcr.io/user/app:sha-860c190',
'ghcr.io/user/app:sha-860c1904a1ce',
'ghcr.io/user/app:latest'
],
[
@ -1423,17 +1423,17 @@ describe('tag', () => {
} as Inputs,
{
main: '1.0.0-dev',
partial: ['1.0', 'sha-860c190-dev'],
partial: ['1.0', 'sha-860c1904a1ce-dev'],
latest: true
} as Version,
[
'org/app:1.0.0-dev',
'org/app:1.0',
'org/app:sha-860c190-dev',
'org/app:sha-860c1904a1ce-dev',
'org/app:latest',
'ghcr.io/user/app:1.0.0-dev',
'ghcr.io/user/app:1.0',
'ghcr.io/user/app:sha-860c190-dev',
'ghcr.io/user/app:sha-860c1904a1ce-dev',
'ghcr.io/user/app:latest'
],
[
@ -1460,16 +1460,16 @@ describe('tag', () => {
]
} as Inputs,
{
main: 'v1.1.1-860c190-foo',
main: 'v1.1.1-860c1904a1ce-foo',
partial: [
'master-foo'
],
latest: false
} as Version,
[
'org/app:v1.1.1-860c190-foo',
'org/app:v1.1.1-860c1904a1ce-foo',
'org/app:master-foo',
'ghcr.io/user/app:v1.1.1-860c190-foo',
'ghcr.io/user/app:v1.1.1-860c1904a1ce-foo',
'ghcr.io/user/app:master-foo'
],
[
@ -1480,7 +1480,7 @@ describe('tag', () => {
"org.opencontainers.image.source=https://github.com/octocat/Hello-World",
"org.opencontainers.image.title=Hello-World",
"org.opencontainers.image.url=https://github.com/octocat/Hello-World",
"org.opencontainers.image.version=v1.1.1-860c190-foo"
"org.opencontainers.image.version=v1.1.1-860c1904a1ce-foo"
],
undefined
],
@ -2334,14 +2334,14 @@ describe('pr', () => {
} as Inputs,
{
main: 'pr-15',
partial: ['sha-a9c8c58'],
partial: ['sha-a9c8c5828b91'],
latest: false
} as Version,
[
'org/app:pr-15',
'org/app:sha-a9c8c58',
'org/app:sha-a9c8c5828b91',
'ghcr.io/user/app:pr-15',
'ghcr.io/user/app:sha-a9c8c58'
'ghcr.io/user/app:sha-a9c8c5828b91'
],
[
"org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
@ -2366,14 +2366,14 @@ describe('pr', () => {
]
} as Inputs,
{
main: 'sha-a9c8c58',
main: 'sha-a9c8c5828b91',
partial: ['pr-15'],
latest: false
} as Version,
[
'org/app:sha-a9c8c58',
'org/app:sha-a9c8c5828b91',
'org/app:pr-15',
'ghcr.io/user/app:sha-a9c8c58',
'ghcr.io/user/app:sha-a9c8c5828b91',
'ghcr.io/user/app:pr-15'
],
[
@ -2384,7 +2384,7 @@ describe('pr', () => {
"org.opencontainers.image.source=https://github.com/octocat/Hello-World",
"org.opencontainers.image.title=Hello-World",
"org.opencontainers.image.url=https://github.com/octocat/Hello-World",
"org.opencontainers.image.version=sha-a9c8c58"
"org.opencontainers.image.version=sha-a9c8c5828b91"
],
undefined
],
@ -2467,14 +2467,14 @@ describe('pr', () => {
]
} as Inputs,
{
main: 'sha-2665741',
main: 'sha-266574110acf',
partial: ['pr-15'],
latest: false
} as Version,
[
'org/app:sha-2665741',
'org/app:sha-266574110acf',
'org/app:pr-15',
'ghcr.io/user/app:sha-2665741',
'ghcr.io/user/app:sha-266574110acf',
'ghcr.io/user/app:pr-15'
],
[
@ -2485,7 +2485,7 @@ describe('pr', () => {
"org.opencontainers.image.source=https://github.com/octocat/Hello-World",
"org.opencontainers.image.title=Hello-World",
"org.opencontainers.image.url=https://github.com/octocat/Hello-World",
"org.opencontainers.image.version=sha-2665741"
"org.opencontainers.image.version=sha-266574110acf"
],
undefined
],
@ -2539,14 +2539,14 @@ describe('pr', () => {
{
main: 'pr-15',
partial: [
'sha-2665741',
'sha-266574110acf',
'sha-266574110acf203503badf966df2ea24b5d732d7'
],
latest: false
} as Version,
[
'org/app:pr-15',
'org/app:sha-2665741',
'org/app:sha-266574110acf',
'org/app:sha-266574110acf203503badf966df2ea24b5d732d7'
],
[
@ -2691,14 +2691,14 @@ describe('pr-head-sha', () => {
} as Inputs,
{
main: 'pr-15',
partial: ['sha-3370e22'],
partial: ['sha-3370e228f220'],
latest: false
} as Version,
[
'org/app:pr-15',
'org/app:sha-3370e22',
'org/app:sha-3370e228f220',
'ghcr.io/user/app:pr-15',
'ghcr.io/user/app:sha-3370e22'
'ghcr.io/user/app:sha-3370e228f220'
],
[
"org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
@ -2722,14 +2722,14 @@ describe('pr-head-sha', () => {
]
} as Inputs,
{
main: 'sha-3370e22',
main: 'sha-3370e228f220',
partial: ['pr-15'],
latest: false
} as Version,
[
'org/app:sha-3370e22',
'org/app:sha-3370e228f220',
'org/app:pr-15',
'ghcr.io/user/app:sha-3370e22',
'ghcr.io/user/app:sha-3370e228f220',
'ghcr.io/user/app:pr-15'
],
[
@ -2740,7 +2740,7 @@ describe('pr-head-sha', () => {
"org.opencontainers.image.source=https://github.com/octocat/Hello-World",
"org.opencontainers.image.title=Hello-World",
"org.opencontainers.image.url=https://github.com/octocat/Hello-World",
"org.opencontainers.image.version=sha-3370e22"
"org.opencontainers.image.version=sha-3370e228f220"
]
],
[
@ -2820,14 +2820,14 @@ describe('pr-head-sha', () => {
]
} as Inputs,
{
main: 'sha-3370e22',
main: 'sha-3370e228f220',
partial: ['pr-15'],
latest: false
} as Version,
[
'org/app:sha-3370e22',
'org/app:sha-3370e228f220',
'org/app:pr-15',
'ghcr.io/user/app:sha-3370e22',
'ghcr.io/user/app:sha-3370e228f220',
'ghcr.io/user/app:pr-15'
],
[
@ -2838,7 +2838,7 @@ describe('pr-head-sha', () => {
"org.opencontainers.image.source=https://github.com/octocat/Hello-World",
"org.opencontainers.image.title=Hello-World",
"org.opencontainers.image.url=https://github.com/octocat/Hello-World",
"org.opencontainers.image.version=sha-3370e22"
"org.opencontainers.image.version=sha-3370e228f220"
]
],
[
@ -2890,14 +2890,14 @@ describe('pr-head-sha', () => {
{
main: 'pr-15',
partial: [
'sha-3370e22',
'sha-3370e228f220',
'sha-3370e228f2209994d57af4427fe64e71bb79ac96'
],
latest: false
} as Version,
[
'org/app:pr-15',
'org/app:sha-3370e22',
'org/app:sha-3370e228f220',
'org/app:sha-3370e228f2209994d57af4427fe64e71bb79ac96'
],
[
@ -3121,14 +3121,14 @@ describe('schedule', () => {
} as Inputs,
{
main: 'nightly',
partial: ['sha-860c190'],
partial: ['sha-860c1904a1ce'],
latest: false
} as Version,
[
'org/app:nightly',
'org/app:sha-860c190',
'org/app:sha-860c1904a1ce',
'ghcr.io/user/app:nightly',
'ghcr.io/user/app:sha-860c190'
'ghcr.io/user/app:sha-860c1904a1ce'
],
[
"org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
@ -3154,7 +3154,7 @@ describe('schedule', () => {
]
} as Inputs,
{
main: 'sha-860c190',
main: 'sha-860c1904a1ce',
partial: [
'nightly',
'defbranch'
@ -3162,10 +3162,10 @@ describe('schedule', () => {
latest: false
} as Version,
[
'org/app:sha-860c190',
'org/app:sha-860c1904a1ce',
'org/app:nightly',
'org/app:defbranch',
'ghcr.io/user/app:sha-860c190',
'ghcr.io/user/app:sha-860c1904a1ce',
'ghcr.io/user/app:nightly',
'ghcr.io/user/app:defbranch'
],
@ -3177,7 +3177,7 @@ describe('schedule', () => {
"org.opencontainers.image.source=https://github.com/octocat/Hello-World",
"org.opencontainers.image.title=Hello-World",
"org.opencontainers.image.url=https://github.com/octocat/Hello-World",
"org.opencontainers.image.version=sha-860c190"
"org.opencontainers.image.version=sha-860c1904a1ce"
],
undefined
],

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

@ -14,6 +14,8 @@ import * as icl from './image';
import * as tcl from './tag';
import * as fcl from './flavor';
const defaultShortShaLength = 12;
export interface Version {
main: string | undefined;
partial: string[];
@ -307,7 +309,7 @@ export class Meta {
let val = this.context.sha;
if (tag.attrs['format'] === tcl.ShaFormat.Short) {
val = this.context.sha.substring(0, 7);
val = Meta.shortSha(this.context.sha);
}
const vraw = this.setValue(val, tag);
@ -373,7 +375,7 @@ export class Meta {
return context.ref.replace(/^refs\/tags\//g, '');
},
sha: function () {
return context.sha.substring(0, 7);
return Meta.shortSha(context.sha);
},
base_ref: function () {
if (/^refs\/tags\//.test(context.ref) && context.payload?.base_ref != undefined) {
@ -593,4 +595,18 @@ export class Meta {
private static sanitizeTag(tag: string): string {
return tag.replace(/[^a-zA-Z0-9._-]+/g, '-');
}
private static shortSha(sha: string): string {
let shortShaLength = defaultShortShaLength;
if (process.env.DOCKER_METADATA_SHORT_SHA_LENGTH) {
if (isNaN(Number(process.env.DOCKER_METADATA_SHORT_SHA_LENGTH))) {
throw new Error(`DOCKER_METADATA_SHORT_SHA_LENGTH is not a valid number: ${process.env.DOCKER_METADATA_SHORT_SHA_LENGTH}`);
}
shortShaLength = Number(process.env.DOCKER_METADATA_SHORT_SHA_LENGTH);
}
if (shortShaLength >= sha.length) {
return sha;
}
return sha.substring(0, shortShaLength);
}
}