Add input labels

This commit is contained in:
なつき 2021-10-15 08:53:20 +00:00
parent 88ef72ac21
commit 60435cd19f
8 changed files with 35 additions and 9 deletions

View file

@ -62,6 +62,7 @@ jobs:
with: with:
layers: false layers: false
tags: ${{ steps.docker-metadata.outputs.tags }} tags: ${{ steps.docker-metadata.outputs.tags }}
labels: ${{ steps.docker-metadata.outputs.labels }}
containerfiles: | containerfiles: |
./Containerfile ./Containerfile
extra-args: | extra-args: |

View file

@ -35,6 +35,7 @@ After building your image, use [push-to-registry](https://github.com/redhat-acti
| layers | Set to true to cache intermediate layers during the build process. | None | layers | Set to true to cache intermediate layers during the build process. | None
| oci | Build the image using the OCI metadata format, instead of the Docker format. | `false` | oci | Build the image using the OCI metadata format, instead of the Docker format. | `false`
| tags | One or more tags to give the new image. Separate by whitespace. Refer to the [Image and Tag Inputs](#image-tag-inputs) section. | `latest` | tags | One or more tags to give the new image. Separate by whitespace. Refer to the [Image and Tag Inputs](#image-tag-inputs) section. | `latest`
| labels | One or more labels to give the new image. Separate by newline. | None
> \* The `containerfiles` input was previously `dockerfiles`. Refer to [this issue](https://github.com/redhat-actions/buildah-build/issues/57). > \* The `containerfiles` input was previously `dockerfiles`. Refer to [this issue](https://github.com/redhat-actions/buildah-build/issues/57).
@ -53,6 +54,7 @@ After building your image, use [push-to-registry](https://github.com/redhat-acti
| oci | Build the image using the OCI metadata format, instead of the Docker format. | `false` | oci | Build the image using the OCI metadata format, instead of the Docker format. | `false`
| port | The port to expose when running the container. | None | port | The port to expose when running the container. | None
| tags | One or more tags to give the new image. Separate by whitespace. Refer to the [Image and Tag Inputs](#image-tag-inputs) section. | `latest` | tags | One or more tags to give the new image. Separate by whitespace. Refer to the [Image and Tag Inputs](#image-tag-inputs) section. | `latest`
| labels | One or more labels to give the new image. Separate by newline. | None
| workdir | The working directory to use within the container. | None | workdir | The working directory to use within the container. | None
<a id="image-tag-inputs"></a> <a id="image-tag-inputs"></a>

View file

@ -12,6 +12,9 @@ inputs:
description: 'The tags of the image to build. For multiple tags, seperate by whitespace. For example, "latest v1".' description: 'The tags of the image to build. For multiple tags, seperate by whitespace. For example, "latest v1".'
required: false required: false
default: latest default: latest
labels:
description: 'The labels of the image to build. Seperate by newline. For example, "io.containers.capabilities=sys_admin,mknod".'
required: false
base-image: base-image:
description: 'The base image to use to create a new container image' description: 'The base image to use to create a new container image'
required: false required: false

2
dist/index.js vendored

File diff suppressed because one or more lines are too long

2
dist/index.js.map vendored

File diff suppressed because one or more lines are too long

View file

@ -15,12 +15,13 @@ export interface BuildahConfigSettings {
port?: string; port?: string;
workingdir?: string; workingdir?: string;
arch?: string; arch?: string;
labels?: string[];
} }
interface Buildah { interface Buildah {
buildUsingDocker( buildUsingDocker(
image: string, context: string, containerFiles: string[], buildArgs: string[], image: string, context: string, containerFiles: string[], buildArgs: string[],
useOCI: boolean, arch: string, platform: string, layers: string, extraArgs: string[] useOCI: boolean, arch: string, platform: string, labels: string[], layers: string, extraArgs: string[]
): Promise<CommandResult>; ): Promise<CommandResult>;
from(baseImage: string): Promise<CommandResult>; from(baseImage: string): Promise<CommandResult>;
config(container: string, setting: BuildahConfigSettings): Promise<CommandResult>; config(container: string, setting: BuildahConfigSettings): Promise<CommandResult>;
@ -63,7 +64,7 @@ export class BuildahCli implements Buildah {
async buildUsingDocker( async buildUsingDocker(
image: string, context: string, containerFiles: string[], buildArgs: string[], image: string, context: string, containerFiles: string[], buildArgs: string[],
useOCI: boolean, arch: string, platform: string, layers: string, extraArgs: string[] useOCI: boolean, arch: string, platform: string, labels: string[], layers: string, extraArgs: string[]
): Promise<CommandResult> { ): Promise<CommandResult> {
const args: string[] = [ "bud" ]; const args: string[] = [ "bud" ];
if (arch) { if (arch) {
@ -78,6 +79,10 @@ export class BuildahCli implements Buildah {
args.push("-f"); args.push("-f");
args.push(file); args.push(file);
}); });
labels.forEach((label) => {
args.push("--label");
args.push(label);
});
buildArgs.forEach((buildArg) => { buildArgs.forEach((buildArg) => {
args.push("--build-arg"); args.push("--build-arg");
args.push(buildArg); args.push(buildArg);
@ -143,6 +148,12 @@ export class BuildahCli implements Buildah {
args.push("--workingdir"); args.push("--workingdir");
args.push(settings.workingdir); args.push(settings.workingdir);
} }
if (settings.labels) {
settings.labels.forEach((label) => {
args.push("--label");
args.push(label);
});
}
args.push(container); args.push(container);
return this.execute(args); return this.execute(args);
} }

View file

@ -73,6 +73,12 @@ export enum Inputs {
* Default: None. * Default: None.
*/ */
IMAGE = "image", IMAGE = "image",
/**
* The labels of the image to build. Seperate by newline. For example, "io.containers.capabilities=sys_admin,mknod".
* Required: false
* Default: None.
*/
LABELS = "labels",
/** /**
* Set to true to cache intermediate layers during build process * Set to true to cache intermediate layers during build process
* Required: false * Required: false

View file

@ -34,6 +34,8 @@ export async function run(): Promise<void> {
const image = core.getInput(Inputs.IMAGE); const image = core.getInput(Inputs.IMAGE);
const tags = core.getInput(Inputs.TAGS); const tags = core.getInput(Inputs.TAGS);
const tagsList: string[] = tags.trim().split(/\s+/); const tagsList: string[] = tags.trim().split(/\s+/);
const labels = core.getInput(Inputs.LABELS);
const labelsList: string[] = labels ? splitByNewline(labels) : [];
// info message if user doesn't provides any tag // info message if user doesn't provides any tag
if (tagsList.length === 0) { if (tagsList.length === 0) {
@ -61,13 +63,13 @@ export async function run(): Promise<void> {
} }
if (containerFiles.length !== 0) { if (containerFiles.length !== 0) {
await doBuildUsingContainerFiles(cli, newImage, workspace, containerFiles, useOCI, arch, platform); await doBuildUsingContainerFiles(cli, newImage, workspace, containerFiles, useOCI, arch, platform, labelsList);
} }
else { else {
if (platform) { if (platform) {
throw new Error("The --platform option is not supported for builds without containerfiles."); throw new Error("The --platform option is not supported for builds without containerfiles.");
} }
await doBuildFromScratch(cli, newImage, useOCI, arch); await doBuildFromScratch(cli, newImage, useOCI, arch, labelsList);
} }
if (tagsList.length > 1) { if (tagsList.length > 1) {
@ -80,7 +82,7 @@ export async function run(): Promise<void> {
async function doBuildUsingContainerFiles( async function doBuildUsingContainerFiles(
cli: BuildahCli, newImage: string, workspace: string, containerFiles: string[], useOCI: boolean, arch: string, cli: BuildahCli, newImage: string, workspace: string, containerFiles: string[], useOCI: boolean, arch: string,
platform: string platform: string, labels: string[],
): Promise<void> { ): Promise<void> {
if (containerFiles.length === 1) { if (containerFiles.length === 1) {
core.info(`Performing build from Containerfile`); core.info(`Performing build from Containerfile`);
@ -103,12 +105,12 @@ async function doBuildUsingContainerFiles(
buildahBudExtraArgs = lines.flatMap((line) => line.split(" ")).map((arg) => arg.trim()); buildahBudExtraArgs = lines.flatMap((line) => line.split(" ")).map((arg) => arg.trim());
} }
await cli.buildUsingDocker( await cli.buildUsingDocker(
newImage, context, containerFileAbsPaths, buildArgs, useOCI, arch, platform, layers, buildahBudExtraArgs newImage, context, containerFileAbsPaths, buildArgs, useOCI, arch, platform, labels, layers, buildahBudExtraArgs
); );
} }
async function doBuildFromScratch( async function doBuildFromScratch(
cli: BuildahCli, newImage: string, useOCI: boolean, arch: string cli: BuildahCli, newImage: string, useOCI: boolean, arch: string, labels: string[],
): Promise<void> { ): Promise<void> {
core.info(`Performing build from scratch`); core.info(`Performing build from scratch`);
@ -128,6 +130,7 @@ async function doBuildFromScratch(
workingdir: workingDir, workingdir: workingDir,
envs, envs,
arch, arch,
labels,
}; };
await cli.config(containerId, newImageConfig); await cli.config(containerId, newImageConfig);
await cli.copy(containerId, content); await cli.copy(containerId, content);