Add feature to tag image with multiple tags

Signed-off-by: divyansh42 <diagrawa@redhat.com>
This commit is contained in:
divyansh42 2021-01-22 18:07:40 +05:30
parent 75dab40354
commit 48975824e7
11 changed files with 1983 additions and 119 deletions

9
.eslintrc.js Normal file
View file

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

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,15 +50,35 @@ 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 }}
# Store the output jar into the env for reuse below.
# It will contain, eg. "spring-petclinic-2.3.0.BUILD-SNAPSHOT.jar"
- name: Get jar filename
id: get-jarname
run: |
export JAR_NAME="$(basename $(ls ${{ env.PROJECT_DIR }}/target/*.jar))"
echo "Petclinic jar is $JAR_NAME"
echo "::set-output name=jarname::$JAR_NAME"
echo "JAR_NAME=$JAR_NAME" >> $GITHUB_ENV
# 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 v2'
# 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-*.BUILD-SNAPSHOT.jar
@ -52,6 +88,11 @@ jobs:
spring-petclinic-*.BUILD-SNAPSHOT.jar spring-petclinic-*.BUILD-SNAPSHOT.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
run: buildah images | grep '${{ env.IMAGE_NAME }}' run: buildah images | grep '${{ env.IMAGE_NAME }}'

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, seperate by a space. For example, <code>v1 v0.1</code>.<br>
Default: <code>latest</code> Default: <code>latest</code>
</td> </td>
</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`: The list of tags delimeted by a space.<br>
For example, `v1 v0.1`.
## 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
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,8 +8,8 @@ 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, "v1 v0.1".'
required: false required: false
default: latest default: latest
base-image: base-image:
@ -44,6 +44,11 @@ inputs:
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 tags delimeted by a space'
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

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.4",
"@redhat-actions/tsconfig": "^1.1.1",
"@types/node": "^12", "@types/node": "^12",
"@typescript-eslint/eslint-plugin": "^4.14.0",
"@typescript-eslint/parser": "^4.14.0",
"@vercel/ncc": "^0.25.1", "@vercel/ncc": "^0.25.1",
"eslint": "^7.18.0",
"typescript": "^4.0.5" "typescript": "^4.0.5"
} }
} }

View file

@ -2,14 +2,6 @@ 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";
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[];
envs?: string[]; envs?: string[];
@ -17,70 +9,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 +94,32 @@ 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 +129,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 +150,7 @@ export class BuildahCli implements Buildah {
} }
return { return {
exitCode, output: stdout, error: stderr exitCode, output: stdout, error: stderr,
}; };
} }
} }

View file

@ -1,37 +1,45 @@
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 recognizer from "language-recognizer";
import * as recognizer from 'language-recognizer'; import { promises as fs } from "fs";
import {promises as fs} from 'fs'; import * as path from "path";
import * as path from 'path'; import { Language } from "language-recognizer/lib/types";
import { Language } from 'language-recognizer/lib/types'; import { BuildahCli, BuildahConfigSettings } from "./buildah";
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 || "";
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");
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 { }
else {
await doBuildFromScratch(cli, newImage, workspace, useOCI); await doBuildFromScratch(cli, newImage, workspace, 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,21 +47,23 @@ 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 inputdockerFiles = dockerFiles.map((file) => path.join(workspace, file));
await cli.buildUsingDocker(newImage, context, dockerFiles, buildArgs, useOCI); await cli.buildUsingDocker(newImage, context, inputdockerFiles, 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, workspace: string, useOCI: boolean,
): Promise<void> {
core.info(`Performing build from scratch`);
let baseImage = core.getInput('base-image'); let baseImage = core.getInput("base-image");
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 base-image is not specified by the user we need to pick one automatically
if (!baseImage) { if (!baseImage) {
@ -62,23 +72,24 @@ async function doBuildFromScratch(cli: BuildahCli, newImage: string, workspace:
const languages = await recognizer.detectLanguages(workspace); const languages = await recognizer.detectLanguages(workspace);
baseImage = await getSuggestedBaseImage(languages); baseImage = await getSuggestedBaseImage(languages);
if (!baseImage) { if (!baseImage) {
throw new Error('No base image found to create a new container'); 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'); 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);
@ -91,30 +102,27 @@ function getInputList(name: string): string[] {
} }
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> { async function getSuggestedBaseImage(languages: Language[]): Promise<string> {
if (!languages || languages.length === 0) { let baseImage = "";
return undefined;
}
for (const language of languages) { for (const language of languages) {
const baseImage = await getBaseImageByLanguage(language); baseImage = await getBaseImageByLanguage(language);
if (baseImage) { if (baseImage) {
return baseImage; return baseImage;
} }
} }
return undefined; return baseImage;
} }
async function getBaseImageByLanguage(language: Language): Promise<string> { async function getBaseImageByLanguage(language: Language): Promise<string> {
const rawData = await fs.readFile(path.join(__dirname, '..', 'language-image.json'), 'utf-8'); const rawData = await fs.readFile(path.join(__dirname, "..", "language-image.json"), "utf-8");
const languageImageJSON = JSON.parse(rawData); const languageImageJSON = JSON.parse(rawData);
return languageImageJSON[language.name]; return languageImageJSON[language.name];
} }

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" ],
]
} }