Compare commits

...

8 Commits

Author SHA1 Message Date
CrazyMax
369eb591f4
Merge pull request #480 from crazy-max/back-to-sha-7
Some checks failed
ci / multi-images (push) Has been cancelled
ci / tag-schedule () (push) Has been cancelled
ci / tag-schedule (cron-{{date 'YYYYMMDD'}}) (push) Has been cancelled
ci / tag-schedule (schedule) (push) Has been cancelled
ci / tag-schedule ({{date 'YYYYMMDD-HHmmss'}}) (push) Has been cancelled
ci / tag-match (\d.\d, 0) (push) Has been cancelled
ci / tag-match (\d.\d.\d, 0) (push) Has been cancelled
ci / tag-match (v(.*), 1) (push) Has been cancelled
ci / tag-semver (auto) (push) Has been cancelled
ci / tag-semver (false) (push) Has been cancelled
ci / tag-semver (true) (push) Has been cancelled
ci / flavor (push) Has been cancelled
ci / images (push) Has been cancelled
ci / custom-labels-annotations (push) Has been cancelled
ci / global-exps (push) Has been cancelled
ci / json (push) Has been cancelled
ci / docker-push (push) Has been cancelled
ci / bake (push) Has been cancelled
ci / sep-tags ( ) (push) Has been cancelled
ci / sep-tags (,) (push) Has been cancelled
ci / output-env (push) Has been cancelled
ci / bake-annotations (push) Has been cancelled
ci / no-images (push) Has been cancelled
ci / bake-cwd (push) Has been cancelled
ci / sha-short () (push) Has been cancelled
ci / sha-short (16) (push) Has been cancelled
ci / dump (push) Has been cancelled
test / test (push) Has been cancelled
validate / prepare (push) Has been cancelled
validate / validate (push) Has been cancelled
back to commit sha length of 7
2024-11-19 18:26:39 +01:00
CrazyMax
7d870ce854
chore: update generated content
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
2024-11-19 18:24:29 +01:00
CrazyMax
e44a9cd788
back to commit sha length of 7
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
2024-11-19 18:24:29 +01:00
CrazyMax
8cb0002d6a
Merge pull request #478 from crazy-max/commit-date-request
commiter_date: fix github api request fallback
2024-11-19 17:54:49 +01:00
CrazyMax
e01ddd3e3c
chore: update generated content
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
2024-11-19 17:22:11 +01:00
CrazyMax
861d98a3bd
commiter_date: fix github api request fallback
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
2024-11-19 17:22:11 +01:00
CrazyMax
359e915ab3
Merge pull request #475 from crazy-max/commit-date-changes
commit_date: code cleanup and readme updates
2024-11-19 15:51:51 +01:00
CrazyMax
0c395ebe62
commit_date: code cleanup and readme updates
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
2024-11-19 15:48:18 +01:00
8 changed files with 112 additions and 88 deletions

View File

@ -45,6 +45,7 @@ ___
* [`{{base_ref}}`](#base_ref)
* [`{{is_default_branch}}`](#is_default_branch)
* [`{{date '<format>' tz='<timezone>'}}`](#date-format-tztimezone)
* [`{{commit_date '<format>' tz='<timezone>'}}`](#commit_date-format-tztimezone)
* [Major version zero](#major-version-zero)
* [JSON output object](#json-output-object)
* [Overwrite labels and annotations](#overwrite-labels-and-annotations)
@ -360,11 +361,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_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`) |
| 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 `7`, 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
@ -724,9 +725,9 @@ tags: |
```
Output Git short commit (or long if specified) as Docker tag like
`sha-860c1904a1ce`.
`sha-860c190`.
By default, the length of the short commit SHA is `12` characters. You can
By default, the length of the short commit SHA is `7` characters. You can
increase this length for larger repositories by setting the
[`DOCKER_METADATA_SHORT_SHA_LENGTH` environment variable](#environment-variables):
@ -741,7 +742,7 @@ increase this length for larger repositories by setting the
tags: |
type=sha
env:
DOCKER_METADATA_SHORT_SHA_LENGTH: 16
DOCKER_METADATA_SHORT_SHA_LENGTH: 12
```
Extended attributes and default values:
@ -891,13 +892,14 @@ Default `tz` is UTC.
#### `{{commit_date '<format>' tz='<timezone>'}}`
Returns the date when current git commit is committed.
rendered by its [moment format](https://momentjs.com/docs/#/displaying/format/).
Returns the date when the current git commit is committed, rendered by its
[moment format](https://momentjs.com/docs/#/displaying/format/). It falls back
to the current date if the commit date is not available.
Default `tz` is UTC.
| Expression | Output example |
|----------------------------------------------|-----------------------------------------|
| Expression | Output example |
|-----------------------------------------------------|-----------------------------------------|
| `{{commit_date 'YYYYMMDD'}}` | `20200110` |
| `{{commit_date 'dddd, MMMM Do YYYY, h:mm:ss a'}}` | `Friday, January 10th 2020, 3:25:50 pm` |
| `{{commit_date 'YYYYMMDD-HHmmss' tz='Asia/Tokyo'}}` | `20200110-093000` |

View File

@ -205,5 +205,19 @@ export const context = {
};
export const getOctokit = jest.fn(() => ({
request: () => Promise.resolve({data: {committer: {date: '2024-11-13T13:42:28Z'}}})
rest: {
repos: {
getCommit: jest.fn(() =>
Promise.resolve({
data: {
commit: {
committer: {
date: '2024-11-13T13:42:28Z'
}
}
}
})
)
}
}
}));

View File

@ -5,9 +5,9 @@ import * as path from 'path';
import {Context} from '@actions/github/lib/context';
import {Git} from '@docker/actions-toolkit/lib/git';
import {GitHub} from '@docker/actions-toolkit/lib/github';
import {Toolkit} from '@docker/actions-toolkit/lib/toolkit';
import {ContextSource, getContext, getInputs, Inputs} from '../src/context';
import {Toolkit} from '@docker/actions-toolkit/lib/toolkit';
const toolkit = new Toolkit({githubToken: 'fake-github-token'});

View File

@ -2,10 +2,11 @@ import {beforeEach, describe, expect, jest, test} from '@jest/globals';
import * as fs from 'fs';
import * as path from 'path';
import * as dotenv from 'dotenv';
import {Context} from '@actions/github/lib/context';
import {GitHub} from '@docker/actions-toolkit/lib/github';
import {Toolkit} from '@docker/actions-toolkit/lib/toolkit';
import {GitHubRepo} from '@docker/actions-toolkit/lib/types/github';
import {Context} from '@actions/github/lib/context';
import {ContextSource, getContext, getInputs, Inputs} from '../src/context';
import {Meta, Version} from '../src/meta';
@ -318,14 +319,14 @@ describe('push', () => {
} as Inputs,
{
main: 'dev',
partial: ['sha-860c1904a1ce'],
partial: ['sha-860c190'],
latest: false
} as Version,
[
'org/app:dev',
'org/app:sha-860c1904a1ce',
'org/app:sha-860c190',
'ghcr.io/user/app:dev',
'ghcr.io/user/app:sha-860c1904a1ce'
'ghcr.io/user/app:sha-860c190'
],
[
"org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
@ -351,14 +352,14 @@ describe('push', () => {
} as Inputs,
{
main: 'edge',
partial: ['sha-266574110acf'],
partial: ['sha-2665741'],
latest: false
} as Version,
[
'org/app:edge',
'org/app:sha-266574110acf',
'org/app:sha-2665741',
'ghcr.io/user/app:edge',
'ghcr.io/user/app:sha-266574110acf'
'ghcr.io/user/app:sha-2665741'
],
[
"org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
@ -384,14 +385,14 @@ describe('push', () => {
} as Inputs,
{
main: 'edge',
partial: ['sha-860c1904a1ce'],
partial: ['sha-860c190'],
latest: false
} as Version,
[
'org/app:edge',
'org/app:sha-860c1904a1ce',
'org/app:sha-860c190',
'ghcr.io/user/app:edge',
'ghcr.io/user/app:sha-860c1904a1ce'
'ghcr.io/user/app:sha-860c190'
],
[
"org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
@ -416,13 +417,13 @@ describe('push', () => {
],
} as Inputs,
{
main: 'sha-266574110acf',
main: 'sha-2665741',
partial: [],
latest: false
} as Version,
[
'org/app:sha-266574110acf',
'ghcr.io/user/app:sha-266574110acf'
'org/app:sha-2665741',
'ghcr.io/user/app:sha-2665741'
],
[
"org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
@ -432,7 +433,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-266574110acf"
"org.opencontainers.image.version=sha-2665741"
],
undefined
],
@ -447,13 +448,13 @@ describe('push', () => {
],
} as Inputs,
{
main: 'sha-983315b5e8d4',
main: 'sha-983315b',
partial: [],
latest: false
} as Version,
[
'org/app:sha-983315b5e8d4',
'ghcr.io/user/app:sha-983315b5e8d4'
'org/app:sha-983315b',
'ghcr.io/user/app:sha-983315b'
],
[
"org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
@ -463,7 +464,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-983315b5e8d4"
"org.opencontainers.image.version=sha-983315b"
],
undefined
],
@ -1355,17 +1356,17 @@ describe('tag', () => {
} as Inputs,
{
main: '1.0.0',
partial: ['1.0', 'sha-860c1904a1ce'],
partial: ['1.0', 'sha-860c190'],
latest: true
} as Version,
[
'org/app:1.0.0',
'org/app:1.0',
'org/app:sha-860c1904a1ce',
'org/app:sha-860c190',
'org/app:latest',
'ghcr.io/user/app:1.0.0',
'ghcr.io/user/app:1.0',
'ghcr.io/user/app:sha-860c1904a1ce',
'ghcr.io/user/app:sha-860c190',
'ghcr.io/user/app:latest'
],
[
@ -1395,17 +1396,17 @@ describe('tag', () => {
} as Inputs,
{
main: '1.0.0',
partial: ['1.0', 'sha-860c1904a1ce'],
partial: ['1.0', 'sha-860c190'],
latest: true
} as Version,
[
'org/app:1.0.0',
'org/app:1.0',
'org/app:sha-860c1904a1ce',
'org/app:sha-860c190',
'org/app:latest',
'ghcr.io/user/app:1.0.0',
'ghcr.io/user/app:1.0',
'ghcr.io/user/app:sha-860c1904a1ce',
'ghcr.io/user/app:sha-860c190',
'ghcr.io/user/app:latest'
],
[
@ -1437,17 +1438,17 @@ describe('tag', () => {
} as Inputs,
{
main: '1.0.0-dev',
partial: ['1.0', 'sha-860c1904a1ce-dev'],
partial: ['1.0', 'sha-860c190-dev'],
latest: true
} as Version,
[
'org/app:1.0.0-dev',
'org/app:1.0',
'org/app:sha-860c1904a1ce-dev',
'org/app:sha-860c190-dev',
'org/app:latest',
'ghcr.io/user/app:1.0.0-dev',
'ghcr.io/user/app:1.0',
'ghcr.io/user/app:sha-860c1904a1ce-dev',
'ghcr.io/user/app:sha-860c190-dev',
'ghcr.io/user/app:latest'
],
[
@ -1474,16 +1475,16 @@ describe('tag', () => {
]
} as Inputs,
{
main: 'v1.1.1-860c1904a1ce-foo',
main: 'v1.1.1-860c190-foo',
partial: [
'master-foo'
],
latest: false
} as Version,
[
'org/app:v1.1.1-860c1904a1ce-foo',
'org/app:v1.1.1-860c190-foo',
'org/app:master-foo',
'ghcr.io/user/app:v1.1.1-860c1904a1ce-foo',
'ghcr.io/user/app:v1.1.1-860c190-foo',
'ghcr.io/user/app:master-foo'
],
[
@ -1494,7 +1495,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-860c1904a1ce-foo"
"org.opencontainers.image.version=v1.1.1-860c190-foo"
],
undefined
],
@ -2348,14 +2349,14 @@ describe('pr', () => {
} as Inputs,
{
main: 'pr-15',
partial: ['sha-a9c8c5828b91'],
partial: ['sha-a9c8c58'],
latest: false
} as Version,
[
'org/app:pr-15',
'org/app:sha-a9c8c5828b91',
'org/app:sha-a9c8c58',
'ghcr.io/user/app:pr-15',
'ghcr.io/user/app:sha-a9c8c5828b91'
'ghcr.io/user/app:sha-a9c8c58'
],
[
"org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
@ -2380,14 +2381,14 @@ describe('pr', () => {
]
} as Inputs,
{
main: 'sha-a9c8c5828b91',
main: 'sha-a9c8c58',
partial: ['pr-15'],
latest: false
} as Version,
[
'org/app:sha-a9c8c5828b91',
'org/app:sha-a9c8c58',
'org/app:pr-15',
'ghcr.io/user/app:sha-a9c8c5828b91',
'ghcr.io/user/app:sha-a9c8c58',
'ghcr.io/user/app:pr-15'
],
[
@ -2398,7 +2399,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-a9c8c5828b91"
"org.opencontainers.image.version=sha-a9c8c58"
],
undefined
],
@ -2481,14 +2482,14 @@ describe('pr', () => {
]
} as Inputs,
{
main: 'sha-266574110acf',
main: 'sha-2665741',
partial: ['pr-15'],
latest: false
} as Version,
[
'org/app:sha-266574110acf',
'org/app:sha-2665741',
'org/app:pr-15',
'ghcr.io/user/app:sha-266574110acf',
'ghcr.io/user/app:sha-2665741',
'ghcr.io/user/app:pr-15'
],
[
@ -2499,7 +2500,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-266574110acf"
"org.opencontainers.image.version=sha-2665741"
],
undefined
],
@ -2553,14 +2554,14 @@ describe('pr', () => {
{
main: 'pr-15',
partial: [
'sha-266574110acf',
'sha-2665741',
'sha-266574110acf203503badf966df2ea24b5d732d7'
],
latest: false
} as Version,
[
'org/app:pr-15',
'org/app:sha-266574110acf',
'org/app:sha-2665741',
'org/app:sha-266574110acf203503badf966df2ea24b5d732d7'
],
[
@ -2734,14 +2735,14 @@ describe('pr-head-sha', () => {
} as Inputs,
{
main: 'pr-15',
partial: ['sha-3370e228f220'],
partial: ['sha-3370e22'],
latest: false
} as Version,
[
'org/app:pr-15',
'org/app:sha-3370e228f220',
'org/app:sha-3370e22',
'ghcr.io/user/app:pr-15',
'ghcr.io/user/app:sha-3370e228f220'
'ghcr.io/user/app:sha-3370e22'
],
[
"org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
@ -2765,14 +2766,14 @@ describe('pr-head-sha', () => {
]
} as Inputs,
{
main: 'sha-3370e228f220',
main: 'sha-3370e22',
partial: ['pr-15'],
latest: false
} as Version,
[
'org/app:sha-3370e228f220',
'org/app:sha-3370e22',
'org/app:pr-15',
'ghcr.io/user/app:sha-3370e228f220',
'ghcr.io/user/app:sha-3370e22',
'ghcr.io/user/app:pr-15'
],
[
@ -2783,7 +2784,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-3370e228f220"
"org.opencontainers.image.version=sha-3370e22"
]
],
[
@ -2863,14 +2864,14 @@ describe('pr-head-sha', () => {
]
} as Inputs,
{
main: 'sha-3370e228f220',
main: 'sha-3370e22',
partial: ['pr-15'],
latest: false
} as Version,
[
'org/app:sha-3370e228f220',
'org/app:sha-3370e22',
'org/app:pr-15',
'ghcr.io/user/app:sha-3370e228f220',
'ghcr.io/user/app:sha-3370e22',
'ghcr.io/user/app:pr-15'
],
[
@ -2881,7 +2882,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-3370e228f220"
"org.opencontainers.image.version=sha-3370e22"
]
],
[
@ -2933,14 +2934,14 @@ describe('pr-head-sha', () => {
{
main: 'pr-15',
partial: [
'sha-3370e228f220',
'sha-3370e22',
'sha-3370e228f2209994d57af4427fe64e71bb79ac96'
],
latest: false
} as Version,
[
'org/app:pr-15',
'org/app:sha-3370e228f220',
'org/app:sha-3370e22',
'org/app:sha-3370e228f2209994d57af4427fe64e71bb79ac96'
],
[
@ -3196,14 +3197,14 @@ describe('schedule', () => {
} as Inputs,
{
main: 'nightly',
partial: ['sha-860c1904a1ce'],
partial: ['sha-860c190'],
latest: false
} as Version,
[
'org/app:nightly',
'org/app:sha-860c1904a1ce',
'org/app:sha-860c190',
'ghcr.io/user/app:nightly',
'ghcr.io/user/app:sha-860c1904a1ce'
'ghcr.io/user/app:sha-860c190'
],
[
"org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
@ -3229,7 +3230,7 @@ describe('schedule', () => {
]
} as Inputs,
{
main: 'sha-860c1904a1ce',
main: 'sha-860c190',
partial: [
'nightly',
'defbranch'
@ -3237,10 +3238,10 @@ describe('schedule', () => {
latest: false
} as Version,
[
'org/app:sha-860c1904a1ce',
'org/app:sha-860c190',
'org/app:nightly',
'org/app:defbranch',
'ghcr.io/user/app:sha-860c1904a1ce',
'ghcr.io/user/app:sha-860c190',
'ghcr.io/user/app:nightly',
'ghcr.io/user/app:defbranch'
],
@ -3252,7 +3253,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-860c1904a1ce"
"org.opencontainers.image.version=sha-860c190"
],
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

@ -119,11 +119,18 @@ async function getCommitDateFromWorkflow(sha: string, toolkit: Toolkit): Promise
}
// fallback to github api for commit date
const commit = await toolkit.github.octokit.request('GET /repos/{owner}/{repo}/commits/{commit_sha}', {
commit_sha: sha,
owner: GitHub.context.repo.owner,
repo: GitHub.context.repo.repo
});
return new Date(commit.data.committer.date);
try {
const commit = await toolkit.github.octokit.rest.repos.getCommit({
owner: GitHub.context.repo.owner,
repo: GitHub.context.repo.repo,
ref: sha
});
if (commit.data.commit.committer?.date) {
return new Date(commit.data.commit.committer.date);
}
throw new Error('Committer date not found');
} catch (error) {
core.debug(`Failed to get commit date from GitHub API: ${error.message}`);
return new Date();
}
}

View File

@ -13,7 +13,7 @@ import * as icl from './image';
import * as tcl from './tag';
import * as fcl from './flavor';
const defaultShortShaLength = 12;
const defaultShortShaLength = 7;
export interface Version {
main: string | undefined;