Add feature to build multi arch image

Since buildah now supports multi arch image built
support (ref: https://github.com/containers/buildah/issues/1590)
This feature will allow building images based on multiple
architectures

Signed-off-by: divyansh42 <diagrawa@redhat.com>
This commit is contained in:
divyansh42 2021-02-12 19:59:22 +05:30
parent 21e07c3197
commit 749ffdbf42
9 changed files with 39 additions and 12 deletions

View file

@ -78,6 +78,7 @@ jobs:
-jar -jar
spring-petclinic-*.jar spring-petclinic-*.jar
port: 8080 port: 8080
archs: amd64,arm64
- name: Echo Outputs - name: Echo Outputs
run: | run: |

View file

@ -21,6 +21,7 @@ After building your image, use [push-to-registry](https://github.com/redhat-acti
| Input Name | Description | Default | | Input Name | Description | Default |
| ---------- | ----------- | ------- | | ---------- | ----------- | ------- |
| archs | ARCH of the image to build, for multiple ARCHs seperate by a comma. For example, `"arm64,amd64"`. If running on self hosted runners then suggest running [qemu-user-static](https://github.com/marketplace/actions/docker-setup-qemu) action to install `qemu-user-static`. | `amd64`
| build-args | Build arguments to pass to the Docker build using `--build-arg`, if using a Dockerfile that requires ARGs. Use the form `arg_name=arg_value`, and separate arguments with newlines. | None | build-args | Build arguments to pass to the Docker build using `--build-arg`, if using a Dockerfile that requires ARGs. Use the form `arg_name=arg_value`, and separate arguments with newlines. | None
| context | Path to directory to use as the build context. | `.` | context | Path to directory to use as the build context. | `.`
| dockerfiles | The list of Dockerfile paths to perform a build using docker instructions. This is a multiline input to allow multiple Dockerfiles. | **Must be provided** | dockerfiles | The list of Dockerfile paths to perform a build using docker instructions. This is a multiline input to allow multiple Dockerfiles. | **Must be provided**
@ -32,6 +33,7 @@ After building your image, use [push-to-registry](https://github.com/redhat-acti
| Input Name | Description | Default | | Input Name | Description | Default |
| ---------- | ----------- | ------- | | ---------- | ----------- | ------- |
| archs | ARCH of the image to build, for multiple ARCHs seperate by a comma. For example, `"arm64,amd64"`. If running on self hosted runners then suggest running [qemu-user-static](https://github.com/marketplace/actions/docker-setup-qemu) action to install `qemu-user-static`. | `amd64`
| base-image | The base image to use for the container. | **Must be provided** | base-image | The base image to use for the container. | **Must be provided**
| content | Paths to files or directories to copy inside the container to create the file image. This is a multiline input to allow you to copy multiple files/directories.| None | content | Paths to files or directories to copy inside the container to create the file image. This is a multiline input to allow you to copy multiple files/directories.| None
| context | Path to directory to use as the build context. | `.` | context | Path to directory to use as the build context. | `.`
@ -74,7 +76,7 @@ jobs:
- uses: actions/checkout@v2 - uses: actions/checkout@v2
- name: Buildah Action - name: Buildah Action
uses: redhat-actions/buildah-build@v2 uses: redhat-actions/buildah-build@v3
with: with:
image: my-new-image image: my-new-image
tags: v1 ${{ github.sha }} tags: v1 ${{ github.sha }}
@ -115,7 +117,7 @@ jobs:
- run: mvn package - run: mvn package
- name: Build Image - name: Build Image
uses: redhat-actions/buildah-build@v2 uses: redhat-actions/buildah-build@v3
with: with:
base-image: docker.io/fabric8/java-alpine-openjdk11-jre base-image: docker.io/fabric8/java-alpine-openjdk11-jre
image: my-new-image image: my-new-image

View file

@ -44,6 +44,10 @@ inputs:
description: 'Set to true to build using the OCI image format instead of the Docker image format' description: 'Set to true to build using the OCI image format instead of the Docker image format'
default: 'false' default: 'false'
required: false required: false
archs:
description: 'ARCH of the image to build, for multiple ARCHs seperate by a comma. For example, "arm64,amd64"'
default: 'amd64'
required: false
outputs: outputs:
image: image:
description: 'Name of the image built' description: 'Name of the image built'

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

@ -6,5 +6,6 @@
echo "----- Pre-commit -----" echo "----- Pre-commit -----"
set -ex set -ex
npx action-io-generator -o src/generated/inputs-outputs.ts npx action-io-generator -o src/generated/inputs-outputs.ts
npm run lint
npm run bundle npm run bundle
git add -v dist/ src/generated git add -v dist/ src/generated

View file

@ -8,11 +8,12 @@ export interface BuildahConfigSettings {
envs?: string[]; envs?: string[];
port?: string; port?: string;
workingdir?: string; workingdir?: string;
archs?: string;
} }
interface Buildah { interface Buildah {
buildUsingDocker( buildUsingDocker(
image: string, context: string, dockerFiles: string[], buildArgs: string[], useOCI: boolean, image: string, context: string, dockerFiles: string[], buildArgs: string[], useOCI: boolean, archs: string,
): Promise<CommandResult>; ): Promise<CommandResult>;
from(baseImage: string): Promise<CommandResult>; from(baseImage: string): Promise<CommandResult>;
copy(container: string, contentToCopy: string[]): Promise<CommandResult | undefined>; copy(container: string, contentToCopy: string[]): Promise<CommandResult | undefined>;
@ -32,9 +33,13 @@ export class BuildahCli implements Buildah {
} }
async buildUsingDocker( async buildUsingDocker(
image: string, context: string, dockerFiles: string[], buildArgs: string[], useOCI: boolean, image: string, context: string, dockerFiles: string[], buildArgs: string[], useOCI: boolean, archs: string
): Promise<CommandResult> { ): Promise<CommandResult> {
const args: string[] = [ "bud" ]; const args: string[] = [ "bud" ];
if (archs) {
args.push("--arch");
args.push(archs);
}
dockerFiles.forEach((file) => { dockerFiles.forEach((file) => {
args.push("-f"); args.push("-f");
args.push(file); args.push(file);
@ -90,6 +95,10 @@ export class BuildahCli implements Buildah {
args.push(env); args.push(env);
}); });
} }
if (settings.archs) {
args.push("--arch");
args.push(settings.archs);
}
args.push(container); args.push(container);
return this.execute(args); return this.execute(args);
} }

View file

@ -1,5 +1,11 @@
// This file was auto-generated by action-io-generator. Do not edit by hand! // This file was auto-generated by action-io-generator. Do not edit by hand!
export enum Inputs { export enum Inputs {
/**
* ARCH of the image to build, for multiple ARCHs seperate by a comma. For example, "arm64,amd64"
* Required: false
* Default: "amd64"
*/
ARCHS = "archs",
/** /**
* The base image to use to create a new container image * The base image to use to create a new container image
* Required: true * Required: true

View file

@ -20,12 +20,15 @@ export async function run(): Promise<void> {
const tagsList: string[] = tags.split(" "); const tagsList: string[] = tags.split(" ");
const newImage = `${image}:${tagsList[0]}`; const newImage = `${image}:${tagsList[0]}`;
const useOCI = core.getInput(Inputs.OCI) === "true"; const useOCI = core.getInput(Inputs.OCI) === "true";
let archs = core.getInput(Inputs.ARCHS);
// remove white spaces (if any) in archs input
archs = archs.replace(/\s+/g, "");
if (dockerFiles.length !== 0) { if (dockerFiles.length !== 0) {
await doBuildUsingDockerFiles(cli, newImage, workspace, dockerFiles, useOCI); await doBuildUsingDockerFiles(cli, newImage, workspace, dockerFiles, useOCI, archs);
} }
else { else {
await doBuildFromScratch(cli, newImage, useOCI); await doBuildFromScratch(cli, newImage, useOCI, archs);
} }
if (tagsList.length > 1) { if (tagsList.length > 1) {
@ -36,7 +39,7 @@ export async function run(): Promise<void> {
} }
async function doBuildUsingDockerFiles( async function doBuildUsingDockerFiles(
cli: BuildahCli, newImage: string, workspace: string, dockerFiles: string[], useOCI: boolean, cli: BuildahCli, newImage: string, workspace: string, dockerFiles: string[], useOCI: boolean, archs: string
): Promise<void> { ): Promise<void> {
if (dockerFiles.length === 1) { if (dockerFiles.length === 1) {
core.info(`Performing build from Dockerfile`); core.info(`Performing build from Dockerfile`);
@ -48,11 +51,11 @@ async function doBuildUsingDockerFiles(
const context = path.join(workspace, core.getInput(Inputs.CONTEXT)); const context = path.join(workspace, core.getInput(Inputs.CONTEXT));
const buildArgs = getInputList(Inputs.BUILD_ARGS); const buildArgs = getInputList(Inputs.BUILD_ARGS);
const dockerFileAbsPaths = dockerFiles.map((file) => path.join(workspace, file)); const dockerFileAbsPaths = dockerFiles.map((file) => path.join(workspace, file));
await cli.buildUsingDocker(newImage, context, dockerFileAbsPaths, buildArgs, useOCI); await cli.buildUsingDocker(newImage, context, dockerFileAbsPaths, buildArgs, useOCI, archs);
} }
async function doBuildFromScratch( async function doBuildFromScratch(
cli: BuildahCli, newImage: string, useOCI: boolean, cli: BuildahCli, newImage: string, useOCI: boolean, archs: string
): Promise<void> { ): Promise<void> {
core.info(`Performing build from scratch`); core.info(`Performing build from scratch`);
@ -73,6 +76,7 @@ async function doBuildFromScratch(
port, port,
workingdir: workingDir, workingdir: workingDir,
envs, envs,
archs,
}; };
await cli.config(containerId, newImageConfig); await cli.config(containerId, newImageConfig);
await cli.commit(containerId, newImage, useOCI); await cli.commit(containerId, newImage, useOCI);