add support for building images with docker files (#4) (#5)

* add support for building images with docker files (#4)

Signed-off-by: Luca Stocchi <lstocchi@redhat.com>

* fix issues when building image with dockerfile

Signed-off-by: Luca Stocchi <lstocchi@redhat.com>

* update readme

Signed-off-by: Luca Stocchi <lstocchi@redhat.com>

* Update README.md

Co-authored-by: Divyanshu Agrawal <diagrawa@redhat.com>

Co-authored-by: Divyanshu Agrawal <diagrawa@redhat.com>
This commit is contained in:
Luca Stocchi 2020-11-19 09:19:57 +01:00 committed by GitHub
parent 329be0a470
commit deaddbe502
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 137 additions and 43 deletions

View file

@ -15,48 +15,103 @@ Note that GitHub's [Ubuntu Environments](https://github.com/actions/virtual-envi
<table> <table>
<thead> <thead>
<tr> <tr>
<th>Action input</th> <th>Input</th>
<th>Required</th>
<th>Description</th> <th>Description</th>
</tr> </tr>
</thead> </thead>
<tr> <tr>
<td>new-image-name</td> <td>image</td>
<td>(Required) Name to give to the image that will be eventually created.</td> <td>Yes</td>
<td>Name to give to the image that will be eventually created.</td>
</tr> </tr>
<tr> <tr>
<td>base-name</td> <td>base-name</td>
<td>(Optional) The base image to use to create the initial container. If not specified, the action will try to pick one automatically. Only Java language is supported at this time.</td> <td>No</td>
<td>The base image to use to create the initial container. If not specified, the action will try to pick one automatically. (N.B: At this time the action is only able to auto select Java base image)</td>
</tr>
<tr>
<td>dockerfiles</td>
<td>No</td>
<td>The list of Dockerfile paths to perform a build using docker instructions. This is a multiline input to add multiple values.</td>
</tr>
<tr>
<td>context</td>
<td>No</td>
<td>The path of the directory to use as context (default: .)</td>
</tr> </tr>
<tr> <tr>
<td>content</td> <td>content</td>
<td>(Required) The content to copy inside the container to create the final image. This is a multiline input to allow you to copy more than one file/directory. For example - <br> content: | <br> target/spring-petclinic-2.3.0.BUILD-SNAPSHOT.jar</td> <td>No</td>
<td>The content to copy inside the container to create the final image. This is a multiline input to allow you to copy more than one file/directory. For example - <br> content: | <br> target/spring-petclinic-2.3.0.BUILD-SNAPSHOT.jar</td>
</tr> </tr>
<tr> <tr>
<td>entrypoint</td> <td>entrypoint</td>
<td>(Required) The entry point to set for the container. This is a multiline input to add multiple values. For example - <br> entrypoint: | <br> java <br> -jar <br> spring-petclinic-2.3.0.BUILD-SNAPSHOT.jar</td> <td>No</td>
<td>The entry point to set for the container. This is a multiline input to add multiple values. For example - <br> entrypoint: | <br> java <br> -jar <br> spring-petclinic-2.3.0.BUILD-SNAPSHOT.jar</td>
</tr> </tr>
<tr> <tr>
<td>port</td> <td>port</td>
<td>(Required) The port to expose when running the container.</td> <td>No</td>
<td>The port to expose when running the container.</td>
</tr> </tr>
<tr> <tr>
<td>working-dir</td> <td>working-dir</td>
<td>(Optional) The working directory to use within the container.</td> <td>No</td>
<td>The working directory to use within the container.</td>
</tr> </tr>
<tr> <tr>
<td>envs</td> <td>envs</td>
<td>(Optional) The environment variables to be set when running the container. This is a multiline input to add multiple environment variables.For example - <br> envs: | <br> GOPATH=/root/buildah</td> <td>No</td>
<td>The environment variables to be set when running the container. This is a multiline input to add multiple environment variables.For example - <br> envs: | <br> GOPATH=/root/buildah</td>
</tr> </tr>
</table> </table>
## Examples ## Build an image using Dockerfile or from scratch
One of the advantages of using the `buildah` action is that you can decide the way you want to build your image.
If you have been using Docker and have some existing Dockerfiles, `buildah` is able to build images by using them.
In this case the inputs needed are just `image`, `dockerfiles` and `content`.
An example below
```yaml
name: Build Image using Dockerfile
on: [push]
jobs:
build:
name: Build image
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Buildah Action
uses: redhat-actions/buildah-action@v1
with:
image: awesome-name:v1
dockerfiles: |
./Dockerfile
```
On the other hand, a build from scratch may require more inputs as it needs to execute a series of steps that can be summarized in:
- Create a new container by using the base image (input: `base-image`)
- Copy all files/directories inside the newly-created container (input: `content`)
- Set up the image configuration values (inputs: `entrypoint`,`port`,`envs`)
- Build an optimized image
Example of building a Spring Boot Java app image below
```yaml ```yaml
name: Build Image name: Build Image
@ -72,13 +127,11 @@ jobs:
uses: actions/checkout@v2 uses: actions/checkout@v2
- name: Maven - name: Maven
run: | run: mvn package
cd ${GITHUB_WORKSPACE}
mvn package
- name: Build Action - name: Build Action
uses: redhat-actions/buildah-action@0.0.1 uses: redhat-actions/buildah-action@v1
with: with:
new-image-name: petclinic image: awesome-name:v1
content: | content: |
target/spring-petclinic-2.3.0.BUILD-SNAPSHOT.jar target/spring-petclinic-2.3.0.BUILD-SNAPSHOT.jar
entrypoint: | entrypoint: |

View file

@ -5,26 +5,33 @@ branding:
icon: circle icon: circle
color: red color: red
inputs: inputs:
new-image-name: image:
description: 'Name of the new image that will be created' description: 'The name (reference) of the image to build'
required: true required: true
base-image: base-image:
description: 'Base image to use' description: 'The base image to use to create a new container image'
required: false required: false
dockerfiles:
description: 'List of Dockerfile paths (eg: ./Dockerfile)'
required: false
context:
description: 'Path of the directory to use as context (default: .)'
required: false
default: '.'
content: content:
description: 'The content to copy inside the base image' description: 'List of files/directories to copy inside the base image'
required: true required: false
entrypoint: entrypoint:
description: 'Entrypoint' description: 'The entry point to set for containers based on image'
required: true required: false
port: port:
description: 'Port' description: 'The port to expose when running containers based on image'
required: true required: false
working-dir: working-dir:
description: 'Working directory' description: 'The working directory to use within the container'
required: false required: false
envs: envs:
description: 'envs' description: 'List of environment variables to be set when running containers based on image'
required: false required: false
runs: runs:
using: 'node12' using: 'node12'

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

@ -3,6 +3,7 @@ import * as exec from "@actions/exec";
import { CommandResult } from "./types"; import { CommandResult } from "./types";
interface Buildah { interface Buildah {
buildUsingDocker(image: string, context: string, dockerFiles: string[]): Promise<CommandResult>;
from(baseImage: string): Promise<CommandResult>; from(baseImage: string): Promise<CommandResult>;
copy(container: string, contentToCopy: string[]): Promise<CommandResult>; copy(container: string, contentToCopy: string[]): Promise<CommandResult>;
config(container: string, setting: {}): Promise<CommandResult>; config(container: string, setting: {}): Promise<CommandResult>;
@ -24,6 +25,18 @@ export class BuildahCli implements Buildah {
this.executable = executable; this.executable = executable;
} }
async buildUsingDocker(image: string, context: string, dockerFiles: string[]): Promise<CommandResult> {
const args: string[] = ['bud'];
dockerFiles.forEach(file => {
args.push('-f');
args.push(file);
});
args.push('-t');
args.push(image);
args.push(context);
return await this.execute(args);
}
async from(baseImage: string): Promise<CommandResult> { async from(baseImage: string): Promise<CommandResult> {
return await this.execute(['from', baseImage]); return await this.execute(['from', baseImage]);
} }

View file

@ -7,23 +7,45 @@ import * as path from 'path';
import { Language } from 'language-recognizer/lib/types'; import { Language } from 'language-recognizer/lib/types';
export async function run(): Promise<void> { export async function run(): Promise<void> {
if (process.env.RUNNER_OS !== 'Linux') {
return Promise.reject(new Error('Only linux platform is supported at this time.'));
}
// get buildah cli
const buildahPath = await io.which('buildah', true);
const cli: BuildahCli = new BuildahCli(buildahPath);
const workspace = process.env['GITHUB_WORKSPACE'];
let dockerFiles = getInputList('dockerfiles');
const newImage = core.getInput('image');
if (dockerFiles.length !== 0) {
doBuildUsingDockerFiles(cli, newImage, workspace, dockerFiles);
} else {
doBuildFromScratch(cli, newImage, workspace);
}
}
async function doBuildUsingDockerFiles(cli: BuildahCli, newImage: string, workspace: string, dockerFiles: string[]): Promise<void> {
const context = path.join(workspace, core.getInput('context'));
dockerFiles = dockerFiles.map(file => path.join(workspace, file));
const build = await cli.buildUsingDocker(newImage, context, dockerFiles);
if (build.succeeded === false) {
return Promise.reject(new Error('Failed building an image from docker files.'));
}
}
async function doBuildFromScratch(cli: BuildahCli, newImage: string, workspace: string) {
let baseImage = core.getInput('base-image'); let baseImage = core.getInput('base-image');
const content = getInputList('content'); const content = getInputList('content');
const newImageName = core.getInput('new-image-name');
const entrypoint = getInputList('entrypoint'); const entrypoint = getInputList('entrypoint');
const port = core.getInput('port'); const port = core.getInput('port');
const workingDir = core.getInput('working-dir'); const workingDir = core.getInput('working-dir');
const envs = getInputList('envs'); const envs = getInputList('envs');
if (process.env.RUNNER_OS !== 'Linux') {
return Promise.reject(new Error('Only linux platform is supported at this time.'));
}
// get buildah cli
const buildahPath = await io.which('buildah', true);
// if base-image is not specified by the user we need to pick one automatically // if base-image is not specified by the user we need to pick one automatically
if (!baseImage) { if (!baseImage) {
const workspace = process.env['GITHUB_WORKSPACE'];
if (workspace) { if (workspace) {
// check language/framework used and pick base-image automatically // check language/framework used and pick base-image automatically
const languages = await recognizer.detectLanguages(workspace); const languages = await recognizer.detectLanguages(workspace);
@ -35,9 +57,7 @@ export async function run(): Promise<void> {
return Promise.reject(new Error('No base image found to create a new container')); return Promise.reject(new Error('No base image found to create a new container'));
} }
} }
// create the new image
const cli: BuildahCli = new BuildahCli(buildahPath);
const container = await cli.from(baseImage); const container = await cli.from(baseImage);
if (container.succeeded === false) { if (container.succeeded === false) {
return Promise.reject(new Error(container.reason)); return Promise.reject(new Error(container.reason));
@ -60,7 +80,7 @@ export async function run(): Promise<void> {
return Promise.reject(new Error(configResult.reason)); return Promise.reject(new Error(configResult.reason));
} }
const commit = await cli.commit(containerId, newImageName, ['--squash']); const commit = await cli.commit(containerId, newImage, ['--squash']);
if (commit.succeeded === false) { if (commit.succeeded === false) {
return Promise.reject(new Error(commit.reason)); return Promise.reject(new Error(commit.reason));
} }

View file

@ -9,6 +9,7 @@
"rootDir": ".", "rootDir": ".",
}, },
"exclude": [ "exclude": [
"node_modules" "node_modules",
"scripts"
] ]
} }