Add feature to output image with multiple tags (#21)

Signed-off-by: divyansh42 <diagrawa@redhat.com>
This commit is contained in:
Divyanshu Agrawal 2021-02-01 23:24:50 +05:30 committed by GitHub
parent 75dab40354
commit 88e0085544
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 1971 additions and 154 deletions

5
.eslintrc.js Normal file
View file

@ -0,0 +1,5 @@
module.exports = {
extends: [
"@redhat-actions/eslint-config",
],
};

View file

@ -4,8 +4,9 @@
name: Test Build
on: [push, pull_request, workflow_dispatch]
env:
TEST_REPO: spring-petclinic
PROJECT_DIR: spring-petclinic
IMAGE_NAME: spring-petclinic
MVN_REPO_DIR: ~/.m2/repository
jobs:
build:
@ -24,7 +25,22 @@ jobs:
uses: actions/checkout@v2
with:
repository: "spring-projects/spring-petclinic"
path: ${{ env.TEST_REPO }}
path: ${{ env.PROJECT_DIR }}
# If none of these files has changed, we assume that the contents of
# .m2/repository can be fetched from the cache.
- name: Hash Maven files
working-directory: ${{ env.PROJECT_DIR }}
run: |
echo "MVN_HASH=${{ hashFiles('**/pom.xml', '.mvn/**/*', 'mvnw*') }}" >> $GITHUB_ENV
# Download the m2 repository from the cache to speed up the build.
- name: Check for Maven cache
id: check-mvn-cache
uses: actions/cache@v2
with:
path: ${{ env.MVN_REPO_DIR }}
key: ${{ env.MVN_HASH }}
# Setup java.
- name: Setup Java
@ -34,23 +50,39 @@ jobs:
# Run maven to build the project
- name: Maven
working-directory: ${{ env.TEST_REPO }}
working-directory: ${{ env.PROJECT_DIR }}
run: |
mvn package -ntp -B
# If there was no cache hit above, store the output into the cache now.
- name: Save Maven repo into cache
if: ${{ steps.check-mvn-cache.outputs.cache-hit }} != 'true'
uses: actions/cache@v2
with:
path: ${{ env.MVN_REPO_DIR }}
key: ${{ env.MVN_HASH }}
# Build image using Buildah action
- name: Build Image
id: build_image
uses: ./buildah-build/
with:
image: ${{ env.IMAGE_NAME }}
tags: 'latest ${{ github.sha }}'
base-image: 'registry.access.redhat.com/openjdk/openjdk-11-rhel7'
# 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
- name: Echo Outputs
run: |
echo "Image: ${{ steps.build_image.outputs.image }}"
echo "Tags: ${{ steps.build_image.outputs.tags }}"
# Check if image is build
- name: Check images created

View file

@ -31,10 +31,10 @@ After building your image, use [push-to-registry](https://github.com/redhat-acti
</tr>
<tr>
<td>tag</td>
<td>tags</td>
<td>No</td>
<td>
Tag to give to the output image.<br>
The tags of the image to build. For multiple tags, separate by a space. For example, <code>latest ${{ github.sha }}</code>.<br>
Default: <code>latest</code>
</td>
</tr>
@ -42,7 +42,7 @@ After building your image, use [push-to-registry](https://github.com/redhat-acti
<tr>
<td>base-image</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>
<td>The base image to use for the container.</td>
</tr>
<tr>
@ -118,6 +118,14 @@ envs: |
</tr>
</table>
## Action Outputs
`image`: The name of the built image.<br>
For example, `spring-image`.
`tags`: A list of the tags that were created, separated by spaces.<br>
For example, `latest ${{ github.sha }}`.
## Build Types
You can configure the `buildah` action to build your image using one or more Dockerfiles, or none at all.
@ -144,7 +152,7 @@ jobs:
uses: redhat-actions/buildah-build@v1
with:
image: my-new-image
tag: v1
tags: v1 ${{ github.sha }}
dockerfiles: |
./Dockerfile
build-args: |
@ -186,7 +194,7 @@ jobs:
with:
base-image: docker.io/fabric8/java-alpine-openjdk11-jre
image: my-new-image
tag: v1
tags: v1
content: |
target/spring-petclinic-2.3.0.BUILD-SNAPSHOT.jar
entrypoint: java -jar spring-petclinic-2.3.0.BUILD-SNAPSHOT.jar

View file

@ -8,13 +8,13 @@ inputs:
image:
description: 'The name (reference) of the image to build'
required: true
tag:
description: 'The tag of the image to build'
tags:
description: 'The tags of the image to build. For multiple tags, seperate by a space. For example, "latest v1".'
required: false
default: latest
base-image:
description: 'The base image to use to create a new container image'
required: false
required: true
dockerfiles:
description: 'List of Dockerfile paths (eg: ./Dockerfile)'
required: false
@ -38,12 +38,17 @@ inputs:
description: 'List of environment variables to be set when running containers based on image'
required: false
build-args:
description: 'List of --build-args to pass to buildah.'
description: 'List of --build-args to pass to buildah'
required: false
oci:
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'
required: false
outputs:
image:
description: 'Name of the image built'
tags:
description: 'List of the tags that were created, separated by spaces'
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

@ -1,3 +0,0 @@
{
"java": "docker.io/fabric8/java-alpine-openjdk11-jre"
}

1776
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -1,6 +1,9 @@
{
"name": "buildah-build",
"version": "0.0.1",
"version": "1.0.0",
"engines": {
"node": "12"
},
"description": "Action for building OCI-compatible images using buildah",
"repository": {
"type": "git",
@ -10,7 +13,8 @@
"scripts": {
"compile": "tsc -p .",
"bundle": "ncc build src/index.ts --source-map --minify",
"clean": "rm -rf out/ dist/"
"clean": "rm -rf out/ dist/",
"lint": "eslint . --max-warnings=0"
},
"keywords": [],
"author": "Red Hat",
@ -22,8 +26,13 @@
"language-recognizer": "0.0.1"
},
"devDependencies": {
"@redhat-actions/eslint-config": "^1.2.11",
"@redhat-actions/tsconfig": "^1.1.1",
"@types/node": "^12",
"@typescript-eslint/eslint-plugin": "^4.14.1",
"@typescript-eslint/parser": "^4.14.1",
"@vercel/ncc": "^0.25.1",
"eslint": "^7.18.0",
"typescript": "^4.0.5"
}
}

View file

@ -1,14 +1,7 @@
import * as core from "@actions/core";
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>;
from(baseImage: string): Promise<CommandResult>;
copy(container: string, contentToCopy: string[]): Promise<CommandResult>;
config(container: string, setting: {}): Promise<CommandResult>;
commit(container: string, newImageName: string, useOCI: boolean): Promise<CommandResult>;
}
import CommandResult from "./types";
export interface BuildahConfigSettings {
entrypoint?: string[];
@ -17,70 +10,83 @@ export interface BuildahConfigSettings {
workingdir?: string;
}
export class BuildahCli implements Buildah {
interface Buildah {
buildUsingDocker(
image: string, context: string, dockerFiles: string[], buildArgs: string[], useOCI: boolean,
): Promise<CommandResult>;
from(baseImage: string): Promise<CommandResult>;
copy(container: string, contentToCopy: string[]): Promise<CommandResult | undefined>;
config(container: string, setting: BuildahConfigSettings): Promise<CommandResult>;
commit(container: string, newImageName: string, useOCI: boolean): Promise<CommandResult>;
}
private executable: string;
export class BuildahCli implements Buildah {
private readonly executable: string;
constructor(executable: string) {
this.executable = executable;
}
private getImageFormatOption(useOCI: boolean): string[] {
return [ '--format', useOCI ? 'oci' : 'docker' ];
private static getImageFormatOption(useOCI: boolean): string[] {
return [ "--format", useOCI ? "oci" : "docker" ];
}
async buildUsingDocker(image: string, context: string, dockerFiles: string[], buildArgs: string[], useOCI: boolean): Promise<CommandResult> {
const args: string[] = ['bud'];
dockerFiles.forEach(file => {
args.push('-f');
async buildUsingDocker(
image: string, context: string, dockerFiles: string[], buildArgs: string[], useOCI: boolean,
): Promise<CommandResult> {
const args: string[] = [ "bud" ];
dockerFiles.forEach((file) => {
args.push("-f");
args.push(file);
});
buildArgs.forEach((buildArg) => {
args.push('--build-arg');
args.push("--build-arg");
args.push(buildArg);
});
args.push(...this.getImageFormatOption(useOCI));
args.push('-t');
args.push(...BuildahCli.getImageFormatOption(useOCI));
args.push("-t");
args.push(image);
args.push(context);
return this.execute(args);
}
async from(baseImage: string): Promise<CommandResult> {
return this.execute(['from', baseImage]);
return this.execute([ "from", baseImage ]);
}
async copy(container: string, contentToCopy: string[], path?: string): Promise<CommandResult | undefined> {
async copy(container: string, contentToCopy: string[], contentPath?: string): Promise<CommandResult | undefined> {
if (contentToCopy.length === 0) {
return undefined;
}
core.debug('copy');
core.debug("copy");
core.debug(container);
for (const content of contentToCopy) {
const args: string[] = ["copy", container, content];
if (path) {
args.push(path);
const args: string[] = [ "copy", container, content ];
if (contentPath) {
args.push(contentPath);
}
return this.execute(args);
}
return undefined;
}
async config(container: string, settings: BuildahConfigSettings): Promise<CommandResult> {
core.debug('config');
core.debug("config");
core.debug(container);
const args: string[] = ['config'];
const args: string[] = [ "config" ];
if (settings.entrypoint) {
args.push('--entrypoint');
args.push(this.convertArrayToStringArg(settings.entrypoint));
args.push("--entrypoint");
args.push(BuildahCli.convertArrayToStringArg(settings.entrypoint));
}
if (settings.port) {
args.push('--port');
args.push("--port");
args.push(settings.port);
}
if (settings.envs) {
settings.envs.forEach((env) => {
args.push('--env');
args.push("--env");
args.push(env);
});
}
@ -89,23 +95,34 @@ export class BuildahCli implements Buildah {
}
async commit(container: string, newImageName: string, useOCI: boolean): Promise<CommandResult> {
core.debug('commit');
core.debug("commit");
core.debug(container);
core.debug(newImageName);
const args: string[] = [ 'commit', ...this.getImageFormatOption(useOCI), '--squash', container, newImageName ];
const args: string[] = [
"commit", ...BuildahCli.getImageFormatOption(useOCI),
"--squash", container, newImageName,
];
return this.execute(args);
}
private convertArrayToStringArg(args: string[]): string {
let arrayAsString = '[';
args.forEach(arg => {
async tag(imageName: string, tags: string[]): Promise<CommandResult> {
const args: string[] = [ "tag" ];
for (const tag of tags) {
args.push(`${imageName}:${tag}`);
}
core.info(`Tagging the built image with tags ${tags.toString()}`);
return this.execute(args);
}
private static convertArrayToStringArg(args: string[]): string {
let arrayAsString = "[";
args.forEach((arg) => {
arrayAsString += `"${arg}",`;
});
return `${arrayAsString.slice(0, -1)}]`;
}
private async execute(args: string[], execOptions: exec.ExecOptions = {}): Promise<CommandResult> {
// ghCore.info(`${EXECUTABLE} ${args.join(" ")}`)
let stdout = "";
@ -115,18 +132,19 @@ export class BuildahCli implements Buildah {
finalExecOptions.ignoreReturnCode = true; // the return code is processed below
finalExecOptions.listeners = {
stdline: (line) => {
stdline: (line): void => {
stdout += line + "\n";
},
errline: (line) => {
stderr += line + "\n"
errline: (line):void => {
stderr += line + "\n";
},
}
};
const exitCode = await exec.exec(this.executable, args, finalExecOptions);
if (execOptions.ignoreReturnCode !== true && exitCode !== 0) {
// Throwing the stderr as part of the Error makes the stderr show up in the action outline, which saves some clicking when debugging.
// Throwing the stderr as part of the Error makes the stderr
// show up in the action outline, which saves some clicking when debugging.
let error = `${path.basename(this.executable)} exited with code ${exitCode}`;
if (stderr) {
error += `\n${stderr}`;
@ -135,7 +153,7 @@ export class BuildahCli implements Buildah {
}
return {
exitCode, output: stdout, error: stderr
exitCode, output: stdout, error: stderr,
};
}
}

View file

@ -1,37 +1,42 @@
import * as core from '@actions/core';
import * as io from '@actions/io';
import { BuildahCli, BuildahConfigSettings } from './buildah';
import * as recognizer from 'language-recognizer';
import {promises as fs} from 'fs';
import * as path from 'path';
import { Language } from 'language-recognizer/lib/types';
import * as core from "@actions/core";
import * as io from "@actions/io";
import * as path from "path";
import { BuildahCli, BuildahConfigSettings } from "./buildah";
export async function run(): Promise<void> {
if (process.env.RUNNER_OS !== 'Linux') {
throw new Error('buildah, and therefore this action, only works on Linux. Please use a Linux runner.');
if (process.env.RUNNER_OS !== "Linux") {
throw new Error("buildah, and therefore this action, only works on Linux. Please use a Linux runner.");
}
// get buildah cli
const buildahPath = await io.which('buildah', true);
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', { required: true })}:${core.getInput('tag', { required: true })}`;
const useOCI = core.getInput("oci") == "true";
const workspace = process.env.GITHUB_WORKSPACE || process.cwd();
const dockerFiles = getInputList("dockerfiles");
const image = core.getInput("image", { required: true });
const tags = core.getInput("tags") || "latest";
const tagsList: string[] = tags.split(" ");
const newImage = `${image}:${tagsList[0]}`;
const useOCI = core.getInput("oci") === "true";
if (dockerFiles.length !== 0) {
await doBuildUsingDockerFiles(cli, newImage, workspace, dockerFiles, useOCI);
} else {
await doBuildFromScratch(cli, newImage, workspace, useOCI);
}
else {
await doBuildFromScratch(cli, newImage, useOCI);
}
core.setOutput("image", newImage);
if (tagsList.length > 1) {
await cli.tag(image, tagsList);
}
core.setOutput("image", image);
core.setOutput("tags", tags);
}
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,
): Promise<void> {
if (dockerFiles.length === 1) {
core.info(`Performing build from Dockerfile`);
}
@ -39,46 +44,34 @@ async function doBuildUsingDockerFiles(cli: BuildahCli, newImage: string, worksp
core.info(`Performing build from ${dockerFiles.length} Dockerfiles`);
}
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);
const context = path.join(workspace, core.getInput("context"));
const buildArgs = getInputList("build-args");
const dockerFileAbsPaths = dockerFiles.map((file) => path.join(workspace, file));
await cli.buildUsingDocker(newImage, context, dockerFileAbsPaths, buildArgs, useOCI);
}
async function doBuildFromScratch(cli: BuildahCli, newImage: string, workspace: string, useOCI: boolean): Promise<void> {
core.info(`Performing build from scratch`)
async function doBuildFromScratch(
cli: BuildahCli, newImage: string, useOCI: boolean,
): Promise<void> {
core.info(`Performing build from scratch`);
let baseImage = core.getInput('base-image');
const content = getInputList('content');
const entrypoint = getInputList('entrypoint');
const port = core.getInput('port');
const workingDir = core.getInput('workdir');
const envs = getInputList('envs');
// if base-image is not specified by the user we need to pick one automatically
if (!baseImage) {
if (workspace) {
// check language/framework used and pick base-image automatically
const languages = await recognizer.detectLanguages(workspace);
baseImage = await getSuggestedBaseImage(languages);
if (!baseImage) {
throw new Error('No base image found to create a new container');
}
} else {
throw new Error('No base image found to create a new container');
}
}
const baseImage = core.getInput("base-image", { required: true });
const content = getInputList("content");
const entrypoint = getInputList("entrypoint");
const port = core.getInput("port");
const workingDir = core.getInput("workdir");
const envs = getInputList("envs");
const container = await cli.from(baseImage);
const containerId = container.output.replace('\n', '');
const containerId = container.output.replace("\n", "");
await cli.copy(containerId, content);
const newImageConfig: BuildahConfigSettings = {
entrypoint: entrypoint,
port: port,
entrypoint,
port,
workingdir: workingDir,
envs: envs
envs,
};
await cli.config(containerId, newImageConfig);
await cli.commit(containerId, newImage, useOCI);
@ -87,36 +80,15 @@ async function doBuildFromScratch(cli: BuildahCli, newImage: string, workspace:
function getInputList(name: string): string[] {
const items = core.getInput(name);
if (!items) {
return [];
return [];
}
return items
.split(/\r?\n/)
.filter(x => x)
.reduce<string[]>(
(acc, line) => acc.concat(line).map(pat => pat.trim()),
[]
.split(/\r?\n/)
.filter((x) => x)
.reduce<string[]>(
(acc, line) => acc.concat(line).map((pat) => pat.trim()),
[],
);
}
async function getSuggestedBaseImage(languages: Language[]): Promise<string> {
if (!languages || languages.length === 0) {
return undefined;
}
for (const language of languages) {
const baseImage = await getBaseImageByLanguage(language);
if (baseImage) {
return baseImage;
}
}
return undefined;
}
async function getBaseImageByLanguage(language: Language): Promise<string> {
const rawData = await fs.readFile(path.join(__dirname, '..', 'language-image.json'), 'utf-8');
const languageImageJSON = JSON.parse(rawData);
return languageImageJSON[language.name];
}
run().catch(core.setFailed);

View file

@ -3,3 +3,5 @@ type CommandResult = {
output: string
error: string
};
export default CommandResult;

View file

@ -1,15 +1,10 @@
{
"extends": "@redhat-actions/tsconfig",
"compilerOptions": {
"target": "ES6",
"module": "commonjs",
"lib": [
"ES2017"
],
"outDir": "out",
"rootDir": ".",
"rootDir": "src/",
"outDir": "out/"
},
"exclude": [
"node_modules",
"scripts"
]
"include": [
"src/"
],
}