Replace input dockerfiles with containerfiles (#69)

* Replace input dockerfiles with containerfiles

Signed-off-by: divyansh42 <diagrawa@redhat.com>
This commit is contained in:
Divyanshu Agrawal 2021-09-15 00:33:38 +05:30 committed by GitHub
parent ab006ef445
commit f9dfea0413
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 106 additions and 76 deletions

View file

@ -1,7 +1,7 @@
# This workflow will perform a test whenever there
# is some change in code done to ensure that the changes
# are not buggy and we are getting the desired output.
name: Build from dockerfile
name: Build from containerfile
on:
push:
pull_request:
@ -36,7 +36,7 @@ jobs:
- name: Create Dockerfile
run: |
cat > Dockerfile<<EOF
cat > Containerfile<<EOF
FROM busybox
RUN echo "hello world"
EOF
@ -49,8 +49,8 @@ jobs:
image: ${{ env.IMAGE_NAME }}
layers: false
tags: 'latest ${{ github.sha }}'
dockerfiles: |
./Dockerfile
containerfiles: |
./Containerfile
extra-args: |
--pull

View file

@ -38,9 +38,9 @@ jobs:
sudo apt-get update
sudo apt-get install -y qemu-user-static
- name: Create Dockerfile
- name: Create Containerfile
run: |
cat > Dockerfile<<EOF
cat > Containerfile<<EOF
ARG ARCH
FROM docker.io/\${ARCH}/alpine:3.14
@ -58,8 +58,8 @@ jobs:
tags: ${{ env.IMAGE_TAG }}
arch: ${{ matrix.arch }}
build-args: ARCH=${{ matrix.arch }}
dockerfiles: |
./Dockerfile
containerfiles: |
./Containerfile
- name: Echo Outputs
run: |

View file

@ -1,7 +1,7 @@
# buildah-build
[![CI checks](https://github.com/redhat-actions/buildah-build/workflows/CI%20checks/badge.svg)](https://github.com/redhat-actions/buildah-build/actions?query=workflow%3A%22CI+checks%22)
[![Build](https://github.com/redhat-actions/buildah-build/workflows/Build/badge.svg)](https://github.com/redhat-actions/buildah-build/actions?query=workflow%3ABuild)
[![Build from dockerfile](https://github.com/redhat-actions/buildah-build/workflows/Build%20from%20dockerfile/badge.svg)](https://github.com/redhat-actions/buildah-build/actions?query=workflow%3A%22Build+from+dockerfile%22)
[![Build from containerfile](https://github.com/redhat-actions/buildah-build/workflows/Build%20from%20containerfile/badge.svg)](https://github.com/redhat-actions/buildah-build/actions?query=workflow%3A%22Build+from+containerfile%22)
[![Link checker](https://github.com/redhat-actions/buildah-build/workflows/Link%20checker/badge.svg)](https://github.com/redhat-actions/buildah-build/actions?query=workflow%3A%22Link+checker%22)
<br>
<br>
@ -21,23 +21,24 @@ After building your image, use [push-to-registry](https://github.com/redhat-acti
<a id="dockerfile-build-inputs"></a>
### [Inputs for build from dockerfile](https://github.com/containers/buildah/blob/main/docs/buildah-build.md)
### [Inputs for build from containerfile](https://github.com/containers/buildah/blob/main/docs/buildah-build.md)
| Input Name | Description | Default |
| ---------- | ----------- | ------- |
| arch | Label the image with this architecture, instead of defaulting to the host architecture. Refer to [Multi arch builds](#multi-arch-builds) for more information. | None (host architecture)
| 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 Containerfile 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**
| containerfiles* | The list of Containerfile paths to perform a build using docker instructions. This is a multiline input to allow multiple Containerfiles. | **Must be provided**
| extra-args | Extra args to be passed to buildah bud. Separate arguments by newline. Do not use quotes. | None
| image | Name to give to the output image. | **Must be provided**
| layers | Set to true to cache intermediate layers during the build process. | None
| oci | Build the image using the OCI format, instead of the Docker format. By default, this is `false`, because images built using the OCI format have issues when published to Dockerhub. | `false`
| tags | The tags of the image to build. For multiple tags, separate by a space. For example, `latest ${{ github.sha }}` | `latest`
> *The `containerfiles` input was previously `dockerfiles`. Now `dockerfiles` is an alias for `containerfiles`. For details see [the issue](https://github.com/redhat-actions/buildah-build/issues/57).
<a id="scratch-build-inputs"></a>
### [Inputs for build without dockerfile](https://github.com/containers/buildah/blob/master/docs/buildah-config.md)
### [Inputs for build without containerfile](https://github.com/containers/buildah/blob/master/docs/buildah-config.md)
| Input Name | Description | Default |
| ---------- | ----------- | ------- |
@ -69,18 +70,18 @@ For example, `spring-image:latest`
## Build Types
You can configure the `buildah` action to build your image using one or more Dockerfiles, or none at all.
You can configure the `buildah` action to build your image using one or more Containerfiles, or none at all.
<a id="build-using-dockerfile"></a>
### Building using Dockerfiles
### Building using Containerfiles
If you have been building your images with an existing Dockerfile, `buildah` can reuse your Dockerfile.
If you have been building your images with an existing Containerfile, `buildah` can reuse your Containerfile.
In this case the inputs needed are `image` and `dockerfiles`. `tag` is also recommended. If your Dockerfile requires ARGs, these can be passed using `build-arg`.
In this case the inputs needed are `image` and `containerfiles`. `tag` is also recommended. If your Containerfile requires ARGs, these can be passed using `build-arg`.
```yaml
name: Build Image using Dockerfile
name: Build Image using Containerfile
on: [push]
jobs:
@ -96,26 +97,26 @@ jobs:
with:
image: my-new-image
tags: v1 ${{ github.sha }}
dockerfiles: |
./Dockerfile
containerfiles: |
./Containerfile
build-args: |
some_arg=some_value
```
<a id="scratch-build"></a>
### Building without a Dockerfile
### Building without a Containerfile
Building without a Dockerfile requires additional inputs, that would normally be specified in the Dockerfile.
Building without a Containerfile requires additional inputs, that would normally be specified in the Containerfile.
Do not set `dockerfiles` if you are doing a build from scratch. Otherwise those Dockerfiles will be used, and the inputs below will be ignored.
Do not set `containerfiles` if you are doing a build from scratch. Otherwise those Containerfiles will be used, and the inputs below will be ignored.
- An output `image` name and usually a `tag`.
- `base-image`
- In a Dockerfile, this would be the `FROM` directive.
- In a Containerfile, this would be the `FROM` directive.
- `content` to copy into the new image
- In a Dockerfile, this would be `COPY` directives.
- In a Containerfile, this would be `COPY` directives.
- `entrypoint` so the container knows what command to run.
- In a Dockerfile, this would be the `ENTRYPOINT`.
- In a Containerfile, this would be the `ENTRYPOINT`.
- All other optional configuration inputs, such as `port`, `envs`, and `workdir`.
Example of building a Spring Boot Java app image:
@ -125,7 +126,7 @@ on: [push]
jobs:
build-image:
name: Build image without Dockerfile
name: Build image without Containerfile
runs-on: ubuntu-latest
steps:
@ -153,7 +154,7 @@ Refer to the [multi-arch example](./.github/workflows/multiarch.yml).
### Emulating RUN instructions
Cross-architecture builds from dockerfiles containing `RUN` instructions require `qemu-user-static` emulation registered in the Linux kernel.
Cross-architecture builds from containerfiles containing `RUN` instructions require `qemu-user-static` emulation registered in the Linux kernel.
For example, run `sudo apt install qemu-user-static` on Debian hosts, or `sudo dnf install qemu-user-static` on Fedora.

View file

@ -1,5 +1,5 @@
name: 'Buildah Build'
description: 'Build a container image, with or without a Dockerfile'
description: 'Build a container image, with or without a Containerfile'
author: 'Red Hat'
branding:
icon: circle
@ -15,8 +15,11 @@ inputs:
base-image:
description: 'The base image to use to create a new container image'
required: false
containerfiles:
description: 'List of Containerfile paths (eg: ./Containerfile)'
required: false
dockerfiles:
description: 'List of Dockerfile paths (eg: ./Dockerfile)'
description: 'Alias for "containerfiles". "containerfiles" takes precedence if both are set.'
required: false
context:
description: 'Path of the directory to use as context (default: .)'

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

@ -19,7 +19,7 @@ export interface BuildahConfigSettings {
interface Buildah {
buildUsingDocker(
image: string, context: string, dockerFiles: string[], buildArgs: string[],
image: string, context: string, containerFiles: string[], buildArgs: string[],
useOCI: boolean, arch: string, layers: string, extraArgs: string[]
): Promise<CommandResult>;
from(baseImage: string): Promise<CommandResult>;
@ -62,7 +62,7 @@ export class BuildahCli implements Buildah {
}
async buildUsingDocker(
image: string, context: string, dockerFiles: string[], buildArgs: string[],
image: string, context: string, containerFiles: string[], buildArgs: string[],
useOCI: boolean, arch: string, layers: string, extraArgs: string[]
): Promise<CommandResult> {
const args: string[] = [ "bud" ];
@ -70,7 +70,7 @@ export class BuildahCli implements Buildah {
args.push("--arch");
args.push(arch);
}
dockerFiles.forEach((file) => {
containerFiles.forEach((file) => {
args.push("-f");
args.push(file);
});

View file

@ -24,6 +24,12 @@ export enum Inputs {
* Default: None.
*/
BUILD_ARGS = "build-args",
/**
* List of Containerfile paths (eg: ./Containerfile)
* Required: false
* Default: None.
*/
CONTAINERFILES = "containerfiles",
/**
* List of files/directories to copy inside the base image
* Required: false
@ -37,7 +43,7 @@ export enum Inputs {
*/
CONTEXT = "context",
/**
* List of Dockerfile paths (eg: ./Dockerfile)
* Alias for "containerfiles". "containerfiles" takes precedence if both are set.
* Required: false
* Default: None.
*/

View file

@ -8,7 +8,9 @@ import * as io from "@actions/io";
import * as path from "path";
import { Inputs, Outputs } from "./generated/inputs-outputs";
import { BuildahCli, BuildahConfigSettings } from "./buildah";
import { splitByNewline } from "./utils";
import {
getArch, getContainerfiles, getInputList, splitByNewline,
} from "./utils";
export async function run(): Promise<void> {
if (process.env.RUNNER_OS !== "Linux") {
@ -27,7 +29,7 @@ export async function run(): Promise<void> {
const DEFAULT_TAG = "latest";
const workspace = process.env.GITHUB_WORKSPACE || process.cwd();
const dockerFiles = getInputList(Inputs.DOCKERFILES);
const containerFiles = getContainerfiles();
const image = core.getInput(Inputs.IMAGE, { required: true });
const tags = core.getInput(Inputs.TAGS);
const tagsList: string[] = tags.split(" ");
@ -42,8 +44,8 @@ export async function run(): Promise<void> {
const arch = getArch();
if (dockerFiles.length !== 0) {
await doBuildUsingDockerFiles(cli, newImage, workspace, dockerFiles, useOCI, arch);
if (containerFiles.length !== 0) {
await doBuildUsingContainerFiles(cli, newImage, workspace, containerFiles, useOCI, arch);
}
else {
await doBuildFromScratch(cli, newImage, useOCI, arch);
@ -57,19 +59,19 @@ export async function run(): Promise<void> {
core.setOutput(Outputs.IMAGE_WITH_TAG, `${image}:${tagsList[0]}`);
}
async function doBuildUsingDockerFiles(
cli: BuildahCli, newImage: string, workspace: string, dockerFiles: string[], useOCI: boolean, arch: string
async function doBuildUsingContainerFiles(
cli: BuildahCli, newImage: string, workspace: string, containerFiles: string[], useOCI: boolean, arch: string
): Promise<void> {
if (dockerFiles.length === 1) {
core.info(`Performing build from Dockerfile`);
if (containerFiles.length === 1) {
core.info(`Performing build from Containerfile`);
}
else {
core.info(`Performing build from ${dockerFiles.length} Dockerfiles`);
core.info(`Performing build from ${containerFiles.length} Containerfiles`);
}
const context = path.join(workspace, core.getInput(Inputs.CONTEXT));
const buildArgs = getInputList(Inputs.BUILD_ARGS);
const dockerFileAbsPaths = dockerFiles.map((file) => path.join(workspace, file));
const containerFileAbsPaths = containerFiles.map((file) => path.join(workspace, file));
const layers = core.getInput(Inputs.LAYERS);
const inputExtraArgsStr = core.getInput(Inputs.EXTRA_ARGS);
@ -81,7 +83,7 @@ async function doBuildUsingDockerFiles(
buildahBudExtraArgs = lines.flatMap((line) => line.split(" ")).map((arg) => arg.trim());
}
await cli.buildUsingDocker(
newImage, context, dockerFileAbsPaths, buildArgs, useOCI, arch, layers, buildahBudExtraArgs
newImage, context, containerFileAbsPaths, buildArgs, useOCI, arch, layers, buildahBudExtraArgs
);
}
@ -112,33 +114,4 @@ async function doBuildFromScratch(
await cli.commit(containerId, newImage, useOCI);
}
function getInputList(name: string): string[] {
const items = core.getInput(name);
if (!items) {
return [];
}
return items
.split(/\r?\n/)
.filter((x) => x)
.reduce<string[]>(
(acc, line) => acc.concat(line).map((pat) => pat.trim()),
[],
);
}
function getArch(): string {
// 'arch' should be used over 'archs', see https://github.com/redhat-actions/buildah-build/issues/60
const archs = core.getInput(Inputs.ARCHS);
const arch = core.getInput(Inputs.ARCH);
if (arch && archs) {
core.warning(
`Please use only one input of "${Inputs.ARCH}" and "${Inputs.ARCHS}". "${Inputs.ARCH}" takes precedence, `
+ `so --arch argument will be "${arch}".`
);
}
return arch || archs;
}
run().catch(core.setFailed);

View file

@ -9,6 +9,7 @@ import * as core from "@actions/core";
import * as path from "path";
import * as io from "@actions/io";
import * as os from "os";
import { Inputs } from "./generated/inputs-outputs";
async function findStorageDriver(filePaths: string[]): Promise<string> {
let storageDriver = "";
@ -63,3 +64,49 @@ export async function findFuseOverlayfsPath(): Promise<string | undefined> {
export function splitByNewline(s: string): string[] {
return s.split(/\r?\n/);
}
export function getArch(): string {
// 'arch' should be used over 'archs', see https://github.com/redhat-actions/buildah-build/issues/60
const archs = core.getInput(Inputs.ARCHS);
const arch = core.getInput(Inputs.ARCH);
if (arch && archs) {
core.warning(
`Both "${Inputs.ARCH}" and "${Inputs.ARCHS}" inputs are set. `
+ `Please use only one of these two inputs, as they are aliases of one another. `
+ `"${Inputs.ARCH}" takes precedence.`
);
}
return arch || archs;
}
export function getContainerfiles(): string[] {
// 'containerfile' should be used over 'dockerfile',
// see https://github.com/redhat-actions/buildah-build/issues/57
const containerfiles = getInputList(Inputs.CONTAINERFILES);
const dockerfiles = getInputList(Inputs.DOCKERFILES);
if (containerfiles.length !== 0 && dockerfiles.length !== 0) {
core.warning(
`Both "${Inputs.CONTAINERFILES}" and "${Inputs.DOCKERFILES}" inputs are set. `
+ `Please use only one of these two inputs, as they are aliases of one another. `
+ `"${Inputs.CONTAINERFILES}" takes precedence.`
);
}
return containerfiles.length !== 0 ? containerfiles : dockerfiles;
}
export function getInputList(name: string): string[] {
const items = core.getInput(name);
if (!items) {
return [];
}
const splitItems = splitByNewline(items);
return splitItems
.reduce<string[]>(
(acc, line) => acc.concat(line).map((item) => item.trim()),
[],
);
}