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 name: Test Build
on: [push, pull_request, workflow_dispatch] on: [push, pull_request, workflow_dispatch]
env: env:
TEST_REPO: spring-petclinic PROJECT_DIR: spring-petclinic
IMAGE_NAME: spring-petclinic IMAGE_NAME: spring-petclinic
MVN_REPO_DIR: ~/.m2/repository
jobs: jobs:
build: build:
@ -24,7 +25,22 @@ jobs:
uses: actions/checkout@v2 uses: actions/checkout@v2
with: with:
repository: "spring-projects/spring-petclinic" 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. # Setup java.
- name: Setup Java - name: Setup Java
@ -34,23 +50,39 @@ jobs:
# Run maven to build the project # Run maven to build the project
- name: Maven - name: Maven
working-directory: ${{ env.TEST_REPO }} working-directory: ${{ env.PROJECT_DIR }}
run: | run: |
mvn package -ntp -B 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 # Build image using Buildah action
- name: Build Image - name: Build Image
id: build_image
uses: ./buildah-build/ uses: ./buildah-build/
with: with:
image: ${{ env.IMAGE_NAME }} 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. # To avoid hardcoding a particular version of the binary.
content: | content: |
./spring-petclinic/target/spring-petclinic-*.BUILD-SNAPSHOT.jar ./spring-petclinic/target/spring-petclinic-*.jar
entrypoint: | entrypoint: |
java java
-jar -jar
spring-petclinic-*.BUILD-SNAPSHOT.jar spring-petclinic-*.jar
port: 8080 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 # Check if image is build
- name: Check images created - 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>
<tr> <tr>
<td>tag</td> <td>tags</td>
<td>No</td> <td>No</td>
<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> Default: <code>latest</code>
</td> </td>
</tr> </tr>
@ -42,7 +42,7 @@ After building your image, use [push-to-registry](https://github.com/redhat-acti
<tr> <tr>
<td>base-image</td> <td>base-image</td>
<td>No</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>
<tr> <tr>
@ -118,6 +118,14 @@ envs: |
</tr> </tr>
</table> </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 ## 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 Dockerfiles, or none at all.
@ -144,7 +152,7 @@ jobs:
uses: redhat-actions/buildah-build@v1 uses: redhat-actions/buildah-build@v1
with: with:
image: my-new-image image: my-new-image
tag: v1 tags: v1 ${{ github.sha }}
dockerfiles: | dockerfiles: |
./Dockerfile ./Dockerfile
build-args: | build-args: |
@ -186,7 +194,7 @@ jobs:
with: with:
base-image: docker.io/fabric8/java-alpine-openjdk11-jre base-image: docker.io/fabric8/java-alpine-openjdk11-jre
image: my-new-image image: my-new-image
tag: v1 tags: v1
content: | content: |
target/spring-petclinic-2.3.0.BUILD-SNAPSHOT.jar target/spring-petclinic-2.3.0.BUILD-SNAPSHOT.jar
entrypoint: java -jar 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: image:
description: 'The name (reference) of the image to build' description: 'The name (reference) of the image to build'
required: true required: true
tag: tags:
description: 'The tag of the image to build' description: 'The tags of the image to build. For multiple tags, seperate by a space. For example, "latest v1".'
required: false required: false
default: latest default: latest
base-image: base-image:
description: 'The base image to use to create a new container image' description: 'The base image to use to create a new container image'
required: false required: true
dockerfiles: dockerfiles:
description: 'List of Dockerfile paths (eg: ./Dockerfile)' description: 'List of Dockerfile paths (eg: ./Dockerfile)'
required: false required: false
@ -38,12 +38,17 @@ inputs:
description: 'List of environment variables to be set when running containers based on image' description: 'List of environment variables to be set when running containers based on image'
required: false required: false
build-args: build-args:
description: 'List of --build-args to pass to buildah.' description: 'List of --build-args to pass to buildah'
required: false required: false
oci: 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' default: 'false'
required: false required: false
outputs:
image:
description: 'Name of the image built'
tags:
description: 'List of the tags that were created, separated by spaces'
runs: runs:
using: 'node12' using: 'node12'
main: 'dist/index.js' 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", "name": "buildah-build",
"version": "0.0.1", "version": "1.0.0",
"engines": {
"node": "12"
},
"description": "Action for building OCI-compatible images using buildah", "description": "Action for building OCI-compatible images using buildah",
"repository": { "repository": {
"type": "git", "type": "git",
@ -10,7 +13,8 @@
"scripts": { "scripts": {
"compile": "tsc -p .", "compile": "tsc -p .",
"bundle": "ncc build src/index.ts --source-map --minify", "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": [], "keywords": [],
"author": "Red Hat", "author": "Red Hat",
@ -22,8 +26,13 @@
"language-recognizer": "0.0.1" "language-recognizer": "0.0.1"
}, },
"devDependencies": { "devDependencies": {
"@redhat-actions/eslint-config": "^1.2.11",
"@redhat-actions/tsconfig": "^1.1.1",
"@types/node": "^12", "@types/node": "^12",
"@typescript-eslint/eslint-plugin": "^4.14.1",
"@typescript-eslint/parser": "^4.14.1",
"@vercel/ncc": "^0.25.1", "@vercel/ncc": "^0.25.1",
"eslint": "^7.18.0",
"typescript": "^4.0.5" "typescript": "^4.0.5"
} }
} }

View file

@ -1,14 +1,7 @@
import * as core from "@actions/core"; import * as core from "@actions/core";
import * as exec from "@actions/exec"; import * as exec from "@actions/exec";
import * as path from "path"; import * as path from "path";
import CommandResult from "./types";
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>;
}
export interface BuildahConfigSettings { export interface BuildahConfigSettings {
entrypoint?: string[]; entrypoint?: string[];
@ -17,70 +10,83 @@ export interface BuildahConfigSettings {
workingdir?: string; 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) { constructor(executable: string) {
this.executable = executable; this.executable = executable;
} }
private getImageFormatOption(useOCI: boolean): string[] { private static getImageFormatOption(useOCI: boolean): string[] {
return [ '--format', useOCI ? 'oci' : 'docker' ]; return [ "--format", useOCI ? "oci" : "docker" ];
} }
async buildUsingDocker(image: string, context: string, dockerFiles: string[], buildArgs: string[], useOCI: boolean): Promise<CommandResult> { async buildUsingDocker(
const args: string[] = ['bud']; image: string, context: string, dockerFiles: string[], buildArgs: string[], useOCI: boolean,
dockerFiles.forEach(file => { ): Promise<CommandResult> {
args.push('-f'); const args: string[] = [ "bud" ];
dockerFiles.forEach((file) => {
args.push("-f");
args.push(file); args.push(file);
}); });
buildArgs.forEach((buildArg) => { buildArgs.forEach((buildArg) => {
args.push('--build-arg'); args.push("--build-arg");
args.push(buildArg); args.push(buildArg);
}); });
args.push(...this.getImageFormatOption(useOCI)); args.push(...BuildahCli.getImageFormatOption(useOCI));
args.push('-t'); args.push("-t");
args.push(image); args.push(image);
args.push(context); args.push(context);
return this.execute(args); return this.execute(args);
} }
async from(baseImage: string): Promise<CommandResult> { 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) { if (contentToCopy.length === 0) {
return undefined; return undefined;
} }
core.debug('copy'); core.debug("copy");
core.debug(container); core.debug(container);
for (const content of contentToCopy) { for (const content of contentToCopy) {
const args: string[] = ["copy", container, content]; const args: string[] = [ "copy", container, content ];
if (path) { if (contentPath) {
args.push(path); args.push(contentPath);
} }
return this.execute(args); return this.execute(args);
} }
return undefined;
} }
async config(container: string, settings: BuildahConfigSettings): Promise<CommandResult> { async config(container: string, settings: BuildahConfigSettings): Promise<CommandResult> {
core.debug('config'); core.debug("config");
core.debug(container); core.debug(container);
const args: string[] = ['config']; const args: string[] = [ "config" ];
if (settings.entrypoint) { if (settings.entrypoint) {
args.push('--entrypoint'); args.push("--entrypoint");
args.push(this.convertArrayToStringArg(settings.entrypoint)); args.push(BuildahCli.convertArrayToStringArg(settings.entrypoint));
} }
if (settings.port) { if (settings.port) {
args.push('--port'); args.push("--port");
args.push(settings.port); args.push(settings.port);
} }
if (settings.envs) { if (settings.envs) {
settings.envs.forEach((env) => { settings.envs.forEach((env) => {
args.push('--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> { async commit(container: string, newImageName: string, useOCI: boolean): Promise<CommandResult> {
core.debug('commit'); core.debug("commit");
core.debug(container); core.debug(container);
core.debug(newImageName); 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); return this.execute(args);
} }
private convertArrayToStringArg(args: string[]): string { async tag(imageName: string, tags: string[]): Promise<CommandResult> {
let arrayAsString = '['; const args: string[] = [ "tag" ];
args.forEach(arg => { 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}",`; arrayAsString += `"${arg}",`;
}); });
return `${arrayAsString.slice(0, -1)}]`; return `${arrayAsString.slice(0, -1)}]`;
} }
private async execute(args: string[], execOptions: exec.ExecOptions = {}): Promise<CommandResult> { private async execute(args: string[], execOptions: exec.ExecOptions = {}): Promise<CommandResult> {
// ghCore.info(`${EXECUTABLE} ${args.join(" ")}`) // ghCore.info(`${EXECUTABLE} ${args.join(" ")}`)
let stdout = ""; let stdout = "";
@ -115,18 +132,19 @@ export class BuildahCli implements Buildah {
finalExecOptions.ignoreReturnCode = true; // the return code is processed below finalExecOptions.ignoreReturnCode = true; // the return code is processed below
finalExecOptions.listeners = { finalExecOptions.listeners = {
stdline: (line) => { stdline: (line): void => {
stdout += line + "\n"; stdout += line + "\n";
}, },
errline: (line) => { errline: (line):void => {
stderr += line + "\n" stderr += line + "\n";
}, },
} };
const exitCode = await exec.exec(this.executable, args, finalExecOptions); const exitCode = await exec.exec(this.executable, args, finalExecOptions);
if (execOptions.ignoreReturnCode !== true && exitCode !== 0) { 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}`; let error = `${path.basename(this.executable)} exited with code ${exitCode}`;
if (stderr) { if (stderr) {
error += `\n${stderr}`; error += `\n${stderr}`;
@ -135,7 +153,7 @@ export class BuildahCli implements Buildah {
} }
return { 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 core from "@actions/core";
import * as io from '@actions/io'; import * as io from "@actions/io";
import { BuildahCli, BuildahConfigSettings } from './buildah'; import * as path from "path";
import * as recognizer from 'language-recognizer'; import { BuildahCli, BuildahConfigSettings } from "./buildah";
import {promises as fs} from 'fs';
import * as path from 'path';
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") {
if (process.env.RUNNER_OS !== 'Linux') { throw new Error("buildah, and therefore this action, only works on Linux. Please use a Linux runner.");
throw new Error('buildah, and therefore this action, only works on Linux. Please use a Linux runner.');
} }
// get buildah cli // get buildah cli
const buildahPath = await io.which('buildah', true); const buildahPath = await io.which("buildah", true);
const cli: BuildahCli = new BuildahCli(buildahPath); const cli: BuildahCli = new BuildahCli(buildahPath);
const workspace = process.env['GITHUB_WORKSPACE']; const workspace = process.env.GITHUB_WORKSPACE || process.cwd();
let dockerFiles = getInputList('dockerfiles'); const dockerFiles = getInputList("dockerfiles");
const newImage = `${core.getInput('image', { required: true })}:${core.getInput('tag', { required: true })}`; const image = core.getInput("image", { required: true });
const tags = core.getInput("tags") || "latest";
const useOCI = core.getInput("oci") == "true"; const tagsList: string[] = tags.split(" ");
const newImage = `${image}:${tagsList[0]}`;
const useOCI = core.getInput("oci") === "true";
if (dockerFiles.length !== 0) { if (dockerFiles.length !== 0) {
await doBuildUsingDockerFiles(cli, newImage, workspace, dockerFiles, useOCI); 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) { if (dockerFiles.length === 1) {
core.info(`Performing build from Dockerfile`); 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`); core.info(`Performing build from ${dockerFiles.length} Dockerfiles`);
} }
const context = path.join(workspace, core.getInput('context')); const context = path.join(workspace, core.getInput("context"));
const buildArgs = getInputList('build-args'); const buildArgs = getInputList("build-args");
dockerFiles = dockerFiles.map(file => path.join(workspace, file)); const dockerFileAbsPaths = dockerFiles.map((file) => path.join(workspace, file));
await cli.buildUsingDocker(newImage, context, dockerFiles, buildArgs, useOCI); await cli.buildUsingDocker(newImage, context, dockerFileAbsPaths, buildArgs, useOCI);
} }
async function doBuildFromScratch(cli: BuildahCli, newImage: string, workspace: string, useOCI: boolean): Promise<void> { async function doBuildFromScratch(
core.info(`Performing build from scratch`) cli: BuildahCli, newImage: string, useOCI: boolean,
): Promise<void> {
core.info(`Performing build from scratch`);
let baseImage = core.getInput('base-image'); const baseImage = core.getInput("base-image", { required: true });
const content = getInputList('content'); const content = getInputList("content");
const entrypoint = getInputList('entrypoint'); const entrypoint = getInputList("entrypoint");
const port = core.getInput('port'); const port = core.getInput("port");
const workingDir = core.getInput('workdir'); const workingDir = core.getInput("workdir");
const envs = getInputList('envs'); 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 container = await cli.from(baseImage); const container = await cli.from(baseImage);
const containerId = container.output.replace('\n', ''); const containerId = container.output.replace("\n", "");
await cli.copy(containerId, content); await cli.copy(containerId, content);
const newImageConfig: BuildahConfigSettings = { const newImageConfig: BuildahConfigSettings = {
entrypoint: entrypoint, entrypoint,
port: port, port,
workingdir: workingDir, workingdir: workingDir,
envs: envs envs,
}; };
await cli.config(containerId, newImageConfig); await cli.config(containerId, newImageConfig);
await cli.commit(containerId, newImage, useOCI); await cli.commit(containerId, newImage, useOCI);
@ -87,36 +80,15 @@ async function doBuildFromScratch(cli: BuildahCli, newImage: string, workspace:
function getInputList(name: string): string[] { function getInputList(name: string): string[] {
const items = core.getInput(name); const items = core.getInput(name);
if (!items) { if (!items) {
return []; return [];
} }
return items return items
.split(/\r?\n/) .split(/\r?\n/)
.filter(x => x) .filter((x) => x)
.reduce<string[]>( .reduce<string[]>(
(acc, line) => acc.concat(line).map(pat => pat.trim()), (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); run().catch(core.setFailed);

View file

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

View file

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