Add 'labels' input (#80)

This commit is contained in:
なつき 2021-10-19 11:41:41 -07:00 committed by GitHub
parent 88ef72ac21
commit c06a2c4759
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 167 additions and 14 deletions

View file

@ -9,18 +9,18 @@ on:
schedule: schedule:
- cron: '0 0 * * *' # every day at midnight - cron: '0 0 * * *' # every day at midnight
env:
IMAGE_NAME: "hello-world"
jobs: jobs:
build: build-containerfile:
name: Build image using Buildah name: Build image with Containerfile
runs-on: ubuntu-20.04 runs-on: ubuntu-20.04
strategy: strategy:
fail-fast: false fail-fast: false
matrix: matrix:
install_latest: [ true, false ] install_latest: [ true, false ]
env:
IMAGE_NAME: "hello-world"
steps: steps:
# Checkout buildah action github repository # Checkout buildah action github repository
@ -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: |
@ -76,3 +77,130 @@ jobs:
# Check if image is build # Check if image is build
- name: Check images created - name: Check images created
run: buildah images | grep '${{ env.IMAGE_NAME }}' run: buildah images | grep '${{ env.IMAGE_NAME }}'
- name: Check image metadata
run: |
set -x
buildah inspect ${{ steps.build_image.outputs.image-with-tag }} | jq '.OCIv1.config.Labels."org.opencontainers.image.title"'
buildah inspect ${{ steps.build_image.outputs.image-with-tag }} | jq '.OCIv1.config.Labels."org.opencontainers.image.description"'
buildah inspect ${{ steps.build_image.outputs.image-with-tag }} | jq '.Docker.config.Labels."org.opencontainers.image.title"'
buildah inspect ${{ steps.build_image.outputs.image-with-tag }} | jq '.Docker.config.Labels."org.opencontainers.image.description"'
build-scratch:
name: Build image without Containerfile
runs-on: ubuntu-20.04
strategy:
fail-fast: false
matrix:
install_latest: [ true, false ]
env:
PROJECT_DIR: spring-petclinic
IMAGE_NAME: spring-petclinic
MVN_REPO_DIR: ~/.m2/repository
steps:
# Checkout buildah action github repository
- name: Checkout Buildah action
uses: actions/checkout@v2
- name: Docker Metadata
id: docker-metadata
uses: docker/metadata-action@v3
with:
images: |
${{ env.IMAGE_NAME }}
tags: |
type=edge
type=sha
type=ref,event=branch
type=ref,event=pr
type=schedule
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}},enable=${{ !startsWith(github.ref, 'refs/tags/v0.') }}
- name: Install latest buildah
if: matrix.install_latest
run: |
bash .github/install_latest_buildah.sh
# Checkout spring-petclinic github repository
- name: Checkout spring-petclinic project
uses: actions/checkout@v2
with:
repository: "spring-projects/spring-petclinic"
path: ${{ env.PROJECT_DIR }}
# If none of these files has changed, we assume that the contents of
# .m2/repository can be fetched from the cache.
- name: Hash Maven files
working-directory: ${{ env.PROJECT_DIR }}
run: |
echo "MVN_HASH=${{ hashFiles('**/pom.xml', '.mvn/**/*', 'mvnw*') }}" >> $GITHUB_ENV
# Download the m2 repository from the cache to speed up the build.
- name: Check for Maven cache
id: check-mvn-cache
uses: actions/cache@v2
with:
path: ${{ env.MVN_REPO_DIR }}
key: ${{ env.MVN_HASH }}
# Setup java.
- name: Setup Java
uses: actions/setup-java@v1
with:
java-version: 11
# Run maven to build the project
- name: Maven
working-directory: ${{ env.PROJECT_DIR }}
run: |
mvn package -ntp -B
# If there was no cache hit above, store the output into the cache now.
- name: Save Maven repo into cache
if: ${{ steps.check-mvn-cache.outputs.cache-hit }} != 'true'
uses: actions/cache@v2
with:
path: ${{ env.MVN_REPO_DIR }}
key: ${{ env.MVN_HASH }}
# Build image using Buildah action
- name: Build Image
id: build_image
uses: ./
with:
tags: ${{ steps.docker-metadata.outputs.tags }}
labels: ${{ steps.docker-metadata.outputs.labels }}
base-image: 'registry.access.redhat.com/openjdk/openjdk-11-rhel7'
# To avoid hardcoding a particular version of the binary.
content: |
./spring-petclinic/target/spring-petclinic-*.jar
entrypoint: |
java
-jar
spring-petclinic-*.jar
port: 8080
arch: amd64
workdir: "."
- name: Echo Outputs
run: |
echo "Image: ${{ steps.build_image.outputs.image }}"
echo "Tags: ${{ steps.build_image.outputs.tags }}"
echo "Tagged Image: ${{ steps.build_image.outputs.image-with-tag }}"
# Check if image is build
- name: Check images created
run: buildah images | grep '${{ env.IMAGE_NAME }}'
- name: Check image metadata
run: |
set -x
buildah inspect ${{ steps.build_image.outputs.image-with-tag }} | jq '.OCIv1.config.Labels."org.opencontainers.image.title"'
buildah inspect ${{ steps.build_image.outputs.image-with-tag }} | jq '.OCIv1.config.Labels."org.opencontainers.image.description"'
buildah inspect ${{ steps.build_image.outputs.image-with-tag }} | jq '.Docker.config.Labels."org.opencontainers.image.title"'
buildah inspect ${{ steps.build_image.outputs.image-with-tag }} | jq '.Docker.config.Labels."org.opencontainers.image.description"'

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);