Add feature to pass extra args when building image

If a user wants to pass few extra args to build
command when building image, then he can pass
using 'extra-args' input parameter.

Signed-off-by: divyansh42 <diagrawa@redhat.com>
This commit is contained in:
divyansh42 2021-01-28 19:06:12 +05:30
parent 75dab40354
commit 2b7e117c22
7 changed files with 24 additions and 9 deletions

View file

@ -45,11 +45,11 @@ jobs:
image: ${{ env.IMAGE_NAME }}
# To avoid hardcoding a particular version of the binary.
content: |
./spring-petclinic/target/spring-petclinic-*.BUILD-SNAPSHOT.jar
./spring-petclinic/target/spring-petclinic-*.jar
entrypoint: |
java
-jar
spring-petclinic-*.BUILD-SNAPSHOT.jar
spring-petclinic-*.jar
port: 8080
# Check if image is build

View file

@ -116,6 +116,13 @@ envs: |
GOPATH=/root/buildah/go</pre>
</td>
</tr>
<tr>
<td>extra-args</td>
<td>No</td>
<td> Extra args to be passed to build command when building image.
</td>
</tr>
</table>
## Build Types
@ -165,6 +172,7 @@ Do not set `dockerfiles` if you are doing a build from scratch. Otherwise those
- `entrypoint` so the container knows what command to run.
- In a Dockerfile, this would be the `ENTRYPOINT`.
- All other optional configuration inputs, such as `port`, `envs`, and `workdir`.
- `extra-args`
Example of building a Spring Boot Java app image:
```yaml

View file

@ -44,6 +44,9 @@ inputs:
description: 'Set to true to build using the OCI image format instead of the Docker image format.'
default: 'false'
required: false
extra-args:
description: 'Extra args to be passed to build command when building image'
required: false
runs:
using: 'node12'
main: 'dist/index.js'

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,7 +3,7 @@ import * as exec from "@actions/exec";
import * as path from "path";
interface Buildah {
buildUsingDocker(image: string, context: string, dockerFiles: string[], buildArgs: string[], useOCI: boolean): Promise<CommandResult>;
buildUsingDocker(image: string, context: string, dockerFiles: string[], buildArgs: string[], useOCI: boolean, extraArgs: string): Promise<CommandResult>;
from(baseImage: string): Promise<CommandResult>;
copy(container: string, contentToCopy: string[]): Promise<CommandResult>;
config(container: string, setting: {}): Promise<CommandResult>;
@ -29,7 +29,7 @@ export class BuildahCli implements Buildah {
return [ '--format', useOCI ? 'oci' : 'docker' ];
}
async buildUsingDocker(image: string, context: string, dockerFiles: string[], buildArgs: string[], useOCI: boolean): Promise<CommandResult> {
async buildUsingDocker(image: string, context: string, dockerFiles: string[], buildArgs: string[], useOCI: boolean, extraArgs: string): Promise<CommandResult> {
const args: string[] = ['bud'];
dockerFiles.forEach(file => {
args.push('-f');
@ -43,6 +43,9 @@ export class BuildahCli implements Buildah {
args.push('-t');
args.push(image);
args.push(context);
if (extraArgs) {
args.push(extraArgs);
}
return this.execute(args);
}

View file

@ -21,9 +21,10 @@ export async function run(): Promise<void> {
const newImage = `${core.getInput('image', { required: true })}:${core.getInput('tag', { required: true })}`;
const useOCI = core.getInput("oci") == "true";
const extraArgs = core.getInput("extra-args");
if (dockerFiles.length !== 0) {
await doBuildUsingDockerFiles(cli, newImage, workspace, dockerFiles, useOCI);
await doBuildUsingDockerFiles(cli, newImage, workspace, dockerFiles, useOCI, extraArgs);
} else {
await doBuildFromScratch(cli, newImage, workspace, useOCI);
}
@ -31,7 +32,7 @@ export async function run(): Promise<void> {
core.setOutput("image", newImage);
}
async function doBuildUsingDockerFiles(cli: BuildahCli, newImage: string, workspace: string, dockerFiles: string[], useOCI: boolean): Promise<void> {
async function doBuildUsingDockerFiles(cli: BuildahCli, newImage: string, workspace: string, dockerFiles: string[], useOCI: boolean, extraArgs: string): Promise<void> {
if (dockerFiles.length === 1) {
core.info(`Performing build from Dockerfile`);
}
@ -42,7 +43,7 @@ async function doBuildUsingDockerFiles(cli: BuildahCli, newImage: string, worksp
const context = path.join(workspace, core.getInput('context'));
const buildArgs = getInputList('build-args');
dockerFiles = dockerFiles.map(file => path.join(workspace, file));
await cli.buildUsingDocker(newImage, context, dockerFiles, buildArgs, useOCI);
await cli.buildUsingDocker(newImage, context, dockerFiles, buildArgs, useOCI, extraArgs);
}
async function doBuildFromScratch(cli: BuildahCli, newImage: string, workspace: string, useOCI: boolean): Promise<void> {