2022-10-12 07:07:13 +02:00
|
|
|
import * as core from '@actions/core';
|
2024-11-13 16:39:24 +01:00
|
|
|
import {Context as GithubContext} from '@actions/github/lib/context';
|
2023-02-20 22:32:55 +01:00
|
|
|
import {Util} from '@docker/actions-toolkit/lib/util';
|
2022-12-15 14:46:34 +01:00
|
|
|
import {Git} from '@docker/actions-toolkit/lib/git';
|
|
|
|
import {GitHub} from '@docker/actions-toolkit/lib/github';
|
2024-11-13 16:39:24 +01:00
|
|
|
import {Toolkit} from '@docker/actions-toolkit/lib/toolkit';
|
|
|
|
|
|
|
|
export interface Context extends GithubContext {
|
|
|
|
commitDate: Date;
|
|
|
|
}
|
2020-10-25 02:25:23 +01:00
|
|
|
|
|
|
|
export interface Inputs {
|
2022-12-15 14:46:34 +01:00
|
|
|
context: ContextSource;
|
2020-10-25 02:25:23 +01:00
|
|
|
images: string[];
|
2021-03-29 13:04:53 +02:00
|
|
|
tags: string[];
|
|
|
|
flavor: string[];
|
|
|
|
labels: string[];
|
2023-11-30 15:03:24 +01:00
|
|
|
annotations: string[];
|
2020-10-25 02:25:23 +01:00
|
|
|
sepTags: string;
|
|
|
|
sepLabels: string;
|
2023-11-30 15:03:24 +01:00
|
|
|
sepAnnotations: string;
|
2021-04-30 00:51:48 +02:00
|
|
|
bakeTarget: string;
|
2020-10-25 02:25:23 +01:00
|
|
|
githubToken: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getInputs(): Inputs {
|
|
|
|
return {
|
2022-12-15 14:46:34 +01:00
|
|
|
context: (core.getInput('context') || ContextSource.workflow) as ContextSource,
|
2023-06-13 12:23:13 +02:00
|
|
|
images: Util.getInputList('images', {ignoreComma: true, comment: '#'}),
|
|
|
|
tags: Util.getInputList('tags', {ignoreComma: true, comment: '#'}),
|
|
|
|
flavor: Util.getInputList('flavor', {ignoreComma: true, comment: '#'}),
|
|
|
|
labels: Util.getInputList('labels', {ignoreComma: true, comment: '#'}),
|
2023-11-30 15:03:24 +01:00
|
|
|
annotations: Util.getInputList('annotations', {ignoreComma: true, comment: '#'}),
|
2022-10-07 22:45:46 +02:00
|
|
|
sepTags: core.getInput('sep-tags', {trimWhitespace: false}) || `\n`,
|
|
|
|
sepLabels: core.getInput('sep-labels', {trimWhitespace: false}) || `\n`,
|
2023-11-30 15:03:24 +01:00
|
|
|
sepAnnotations: core.getInput('sep-annotations', {trimWhitespace: false}) || `\n`,
|
2021-05-10 15:54:35 +02:00
|
|
|
bakeTarget: core.getInput('bake-target') || `docker-metadata-action`,
|
2020-10-25 02:25:23 +01:00
|
|
|
githubToken: core.getInput('github-token')
|
|
|
|
};
|
|
|
|
}
|
2022-12-15 14:46:34 +01:00
|
|
|
|
|
|
|
export enum ContextSource {
|
|
|
|
workflow = 'workflow',
|
|
|
|
git = 'git'
|
|
|
|
}
|
|
|
|
|
2024-11-13 16:39:24 +01:00
|
|
|
export async function getContext(source: ContextSource, toolkit: Toolkit): Promise<Context> {
|
2022-12-15 14:46:34 +01:00
|
|
|
switch (source) {
|
|
|
|
case ContextSource.workflow:
|
2024-11-13 16:39:24 +01:00
|
|
|
return await getContextFromWorkflow(toolkit);
|
2022-12-15 14:46:34 +01:00
|
|
|
case ContextSource.git:
|
|
|
|
return await getContextFromGit();
|
|
|
|
default:
|
|
|
|
throw new Error(`Invalid context source: ${source}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-13 16:39:24 +01:00
|
|
|
async function getContextFromWorkflow(toolkit: Toolkit): Promise<Context> {
|
2022-12-15 14:46:34 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-13 16:39:24 +01:00
|
|
|
return {
|
|
|
|
commitDate: await getCommitDateFromWorkflow(context.sha, toolkit),
|
|
|
|
...context
|
|
|
|
} as Context;
|
2022-12-15 14:46:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async function getContextFromGit(): Promise<Context> {
|
2024-11-13 16:39:24 +01:00
|
|
|
const ctx = await Git.context();
|
|
|
|
|
|
|
|
return {
|
|
|
|
commitDate: await Git.commitDate(ctx.sha),
|
|
|
|
...ctx
|
|
|
|
} as Context;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getCommitDateFromWorkflow(sha: string, toolkit: Toolkit): Promise<Date> {
|
|
|
|
const event = GitHub.context.payload as unknown as {
|
|
|
|
// branch push
|
|
|
|
commits?: Array<{
|
|
|
|
timestamp: string;
|
|
|
|
// commit sha
|
|
|
|
id: string;
|
|
|
|
}>;
|
|
|
|
// tags
|
|
|
|
head_commit?: {
|
|
|
|
timestamp: string;
|
|
|
|
// commit sha
|
|
|
|
id: string;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
if (event.commits) {
|
|
|
|
const commitDate = event.commits.find(x => x.id === sha)?.timestamp;
|
|
|
|
if (commitDate) {
|
|
|
|
return new Date(commitDate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.head_commit) {
|
|
|
|
if (event.head_commit.id === sha) {
|
|
|
|
return new Date(event.head_commit.timestamp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// fallback to github api for commit date
|
2024-11-19 16:57:38 +01:00
|
|
|
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();
|
|
|
|
}
|
2022-12-15 14:46:34 +01:00
|
|
|
}
|