Skip to content

Commit

Permalink
DOCKER_METADATA_SHORT_SHA_LENGTH env var to customize short commit SH…
Browse files Browse the repository at this point in the history
…A length

Signed-off-by: CrazyMax <[email protected]>
  • Loading branch information
crazy-max committed Oct 31, 2024
1 parent bb9c6dd commit ac8181a
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 6 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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_SHA_SHORT_LENGTH: ${{ matrix.short-length }}
27 changes: 23 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -725,6 +726,24 @@ tags: |
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:

```yaml
Expand Down
17 changes: 15 additions & 2 deletions src/meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down Expand Up @@ -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, 12);
val = Meta.shortSha(this.context.sha);
}

const vraw = this.setValue(val, tag);
Expand Down Expand Up @@ -373,7 +375,7 @@ export class Meta {
return context.ref.replace(/^refs\/tags\//g, '');
},
sha: function () {
return context.sha.substring(0, 12);
return Meta.shortSha(context.sha);
},
base_ref: function () {
if (/^refs\/tags\//.test(context.ref) && context.payload?.base_ref != undefined) {
Expand Down Expand Up @@ -593,4 +595,15 @@ 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 && !isNaN(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);
}
}

0 comments on commit ac8181a

Please sign in to comment.