Skip to content

Commit

Permalink
Add feature to build multi-arch image (#27)
Browse files Browse the repository at this point in the history
Since buildah now supports multi arch image
support (ref: containers/buildah#1590)
This feature will allow building images for multiple
architectures.

Signed-off-by: divyansh42 <[email protected]>
  • Loading branch information
divyansh42 authored Feb 17, 2021
1 parent 21e07c3 commit 803a141
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/scratch_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ jobs:
-jar
spring-petclinic-*.jar
port: 8080

archs: amd64,arm64

- name: Echo Outputs
run: |
echo "Image: ${{ steps.build_image.outputs.image }}"
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ After building your image, use [push-to-registry](https://github.com/redhat-acti

| Input Name | Description | Default |
| ---------- | ----------- | ------- |
| archs | Architecture(s) to build the image(s) for. For multiple architectures, separate by a comma. If the workflow is running on a self-hosted runner, [qemu-user-static](https://github.com/marketplace/actions/docker-setup-qemu) can be used to install the `qemu-user-static` dependency. | `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
| 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**
Expand All @@ -32,6 +33,7 @@ After building your image, use [push-to-registry](https://github.com/redhat-acti

| Input Name | Description | Default |
| ---------- | ----------- | ------- |
| archs | Architecture(s) to build the image(s) for. For multiple architectures, separate by a comma. If the workflow is running on a self-hosted runner, [qemu-user-static](https://github.com/marketplace/actions/docker-setup-qemu) can be used to install the `qemu-user-static` dependency. | `amd64`
| 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
| context | Path to directory to use as the build context. | `.`
Expand Down
6 changes: 6 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ inputs:
description: 'Set to true to build using the OCI image format instead of the Docker image format'
default: 'false'
required: false
archs:
description: |
Architecture(s) to build the image(s) for. For multiple architectures,
separate by a comma.
default: 'amd64'
required: false
outputs:
image:
description: 'Name of the image built'
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions git-hooks/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
echo "----- Pre-commit -----"
set -ex
npx action-io-generator -o src/generated/inputs-outputs.ts
npm run lint
npm run bundle
git add -v dist/ src/generated
13 changes: 11 additions & 2 deletions src/buildah.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ export interface BuildahConfigSettings {
envs?: string[];
port?: string;
workingdir?: string;
archs?: string;
}

interface Buildah {
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>;
from(baseImage: string): Promise<CommandResult>;
copy(container: string, contentToCopy: string[]): Promise<CommandResult | undefined>;
Expand All @@ -32,9 +33,13 @@ export class BuildahCli implements Buildah {
}

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> {
const args: string[] = [ "bud" ];
if (archs) {
args.push("--arch");
args.push(archs);
}
dockerFiles.forEach((file) => {
args.push("-f");
args.push(file);
Expand Down Expand Up @@ -90,6 +95,10 @@ export class BuildahCli implements Buildah {
args.push(env);
});
}
if (settings.archs) {
args.push("--arch");
args.push(settings.archs);
}
args.push(container);
return this.execute(args);
}
Expand Down
7 changes: 7 additions & 0 deletions src/generated/inputs-outputs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
// This file was auto-generated by action-io-generator. Do not edit by hand!
export enum Inputs {
/**
* Architecture(s) to build the image(s) for. For multiple architectures,
* separate by a comma.
* Required: false
* Default: "amd64"
*/
ARCHS = "archs",
/**
* The base image to use to create a new container image
* Required: true
Expand Down
14 changes: 9 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@ export async function run(): Promise<void> {
const tagsList: string[] = tags.split(" ");
const newImage = `${image}:${tagsList[0]}`;
const useOCI = core.getInput(Inputs.OCI) === "true";
let archs: string | undefined = core.getInput(Inputs.ARCHS);
// remove white spaces (if any) in archs input
archs = archs.replace(/\s+/g, "");

if (dockerFiles.length !== 0) {
await doBuildUsingDockerFiles(cli, newImage, workspace, dockerFiles, useOCI);
await doBuildUsingDockerFiles(cli, newImage, workspace, dockerFiles, useOCI, archs);
}
else {
await doBuildFromScratch(cli, newImage, useOCI);
await doBuildFromScratch(cli, newImage, useOCI, archs);
}

if (tagsList.length > 1) {
Expand All @@ -36,7 +39,7 @@ export async function run(): Promise<void> {
}

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> {
if (dockerFiles.length === 1) {
core.info(`Performing build from Dockerfile`);
Expand All @@ -48,11 +51,11 @@ async function doBuildUsingDockerFiles(
const context = path.join(workspace, core.getInput(Inputs.CONTEXT));
const buildArgs = getInputList(Inputs.BUILD_ARGS);
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(
cli: BuildahCli, newImage: string, useOCI: boolean,
cli: BuildahCli, newImage: string, useOCI: boolean, archs: string
): Promise<void> {
core.info(`Performing build from scratch`);

Expand All @@ -73,6 +76,7 @@ async function doBuildFromScratch(
port,
workingdir: workingDir,
envs,
archs,
};
await cli.config(containerId, newImageConfig);
await cli.commit(containerId, newImage, useOCI);
Expand Down

0 comments on commit 803a141

Please sign in to comment.