mirror of
https://github.com/redhat-actions/buildah-build.git
synced 2025-06-07 17:31:35 +00:00
add language recognizer
Signed-off-by: Luca Stocchi <lstocchi@redhat.com>
This commit is contained in:
parent
652179bd8c
commit
3043388d5b
12 changed files with 6828 additions and 106 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1 +1,2 @@
|
|||
node_modules/
|
||||
out/
|
|
@ -20,6 +20,12 @@ inputs:
|
|||
port:
|
||||
description: 'Port'
|
||||
required: false
|
||||
working-dir:
|
||||
description: 'Working directory'
|
||||
required: false
|
||||
envs:
|
||||
description: 'envs'
|
||||
required: false
|
||||
runs:
|
||||
using: 'node12'
|
||||
main: 'dist/index.js'
|
2
dist/index.js
vendored
2
dist/index.js
vendored
File diff suppressed because one or more lines are too long
2
dist/index.js.map
vendored
2
dist/index.js.map
vendored
File diff suppressed because one or more lines are too long
6622
dist/languages.yaml
vendored
Normal file
6622
dist/languages.yaml
vendored
Normal file
File diff suppressed because it is too large
Load diff
3
language-image.json
Normal file
3
language-image.json
Normal file
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"java": "docker.io/fabric8/java-alpine-openjdk11-jre"
|
||||
}
|
|
@ -18,22 +18,25 @@ class BuildahCli {
|
|||
}
|
||||
from(baseImage) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
if (!baseImage) {
|
||||
// find correct baseImage based on language project
|
||||
}
|
||||
return yield this.execute(['from', baseImage]);
|
||||
});
|
||||
}
|
||||
copy(container, content, path) {
|
||||
copy(container, contentToCopy, path) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
core.debug('copy');
|
||||
core.debug(container);
|
||||
core.debug(content);
|
||||
const args = ["copy", container, content];
|
||||
if (path) {
|
||||
args.push(path);
|
||||
let result;
|
||||
for (const content of contentToCopy) {
|
||||
const args = ["copy", container, content];
|
||||
if (path) {
|
||||
args.push(path);
|
||||
}
|
||||
result = yield this.execute(args);
|
||||
if (result.succeeded === false) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
return yield this.execute(args);
|
||||
return result;
|
||||
});
|
||||
}
|
||||
config(container, settings) {
|
||||
|
@ -49,6 +52,12 @@ class BuildahCli {
|
|||
args.push('--port');
|
||||
args.push(settings.port);
|
||||
}
|
||||
if (settings.envs) {
|
||||
settings.envs.forEach((env) => {
|
||||
args.push('--env');
|
||||
args.push(env);
|
||||
});
|
||||
}
|
||||
args.push(container);
|
||||
return yield this.execute(args);
|
||||
});
|
||||
|
|
|
@ -9,45 +9,60 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|||
});
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.getInputList = exports.run = void 0;
|
||||
exports.run = void 0;
|
||||
const core = require("@actions/core");
|
||||
const io = require("@actions/io");
|
||||
const buildah_1 = require("./buildah");
|
||||
/**
|
||||
* 1. Buildah only works on Linux as the docker action
|
||||
* 2. Does this action also need to setup buildah?
|
||||
* 3. Does this action also have the ability to push to a registry?
|
||||
*
|
||||
*/
|
||||
const recognizer = require("language-recognizer");
|
||||
const fs_1 = require("fs");
|
||||
const path = require("path");
|
||||
function run() {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
const baseImage = core.getInput('base-image');
|
||||
const content = core.getInput('content');
|
||||
const entrypoint = yield getInputList('entrypoint');
|
||||
const port = core.getInput('port');
|
||||
let baseImage = core.getInput('base-image');
|
||||
const content = getInputList('content');
|
||||
const newImageName = core.getInput('new-image-name');
|
||||
const runnerOS = process.env.RUNNER_OS;
|
||||
if (runnerOS !== 'Linux') {
|
||||
throw new Error(`Only supported on linux platform`);
|
||||
const entrypoint = getInputList('entrypoint');
|
||||
const port = core.getInput('port');
|
||||
const workingDir = core.getInput('working-dir');
|
||||
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 = yield io.which('buildah', true);
|
||||
// create image
|
||||
const cli = new buildah_1.BuildahCli(buildahPath);
|
||||
const creationResult = yield cli.from(baseImage);
|
||||
if (creationResult.succeeded === false) {
|
||||
return Promise.reject(new Error(creationResult.reason));
|
||||
// if base-image is not specified by the user we need to pick one automatically
|
||||
if (!baseImage) {
|
||||
const workspace = process.env['GITHUB_WORKSPACE'];
|
||||
if (workspace) {
|
||||
// check language/framework used and pick base-image automatically
|
||||
const languages = yield recognizer.detectLanguages(workspace);
|
||||
baseImage = yield getSuggestedBaseImage(languages);
|
||||
if (!baseImage) {
|
||||
return Promise.reject(new Error('No base image found to create a new container'));
|
||||
}
|
||||
}
|
||||
else {
|
||||
return Promise.reject(new Error('No base image found to create a new container'));
|
||||
}
|
||||
}
|
||||
const containerId = creationResult.output.replace('\n', '');
|
||||
// create the new image
|
||||
const cli = new buildah_1.BuildahCli(buildahPath);
|
||||
const container = yield cli.from(baseImage);
|
||||
if (container.succeeded === false) {
|
||||
return Promise.reject(new Error(container.reason));
|
||||
}
|
||||
const containerId = container.output.replace('\n', '');
|
||||
const copyResult = yield cli.copy(containerId, content);
|
||||
if (copyResult.succeeded === false) {
|
||||
return Promise.reject(new Error(copyResult.reason));
|
||||
}
|
||||
const configuration = {
|
||||
const newImageConfig = {
|
||||
entrypoint: entrypoint,
|
||||
port: port
|
||||
port: port,
|
||||
workingdir: workingDir,
|
||||
envs: envs
|
||||
};
|
||||
const configResult = yield cli.config(containerId, configuration);
|
||||
const configResult = yield cli.config(containerId, newImageConfig);
|
||||
if (configResult.succeeded === false) {
|
||||
return Promise.reject(new Error(configResult.reason));
|
||||
}
|
||||
|
@ -55,21 +70,39 @@ function run() {
|
|||
if (commit.succeeded === false) {
|
||||
return Promise.reject(new Error(commit.reason));
|
||||
}
|
||||
core.setOutput('image', newImageName);
|
||||
});
|
||||
}
|
||||
exports.run = run;
|
||||
function getInputList(name, ignoreComma) {
|
||||
function getInputList(name) {
|
||||
const items = core.getInput(name);
|
||||
if (!items) {
|
||||
return [];
|
||||
}
|
||||
return items
|
||||
.split(/\r?\n/)
|
||||
.filter(x => x)
|
||||
.reduce((acc, line) => acc.concat(line).map(pat => pat.trim()), []);
|
||||
}
|
||||
function getSuggestedBaseImage(languages) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
const items = core.getInput(name);
|
||||
if (items == '') {
|
||||
return [];
|
||||
if (!languages || languages.length === 0) {
|
||||
return undefined;
|
||||
}
|
||||
return items
|
||||
.split(/\r?\n/)
|
||||
.filter(x => x)
|
||||
.reduce((acc, line) => acc.concat(!ignoreComma ? line.split(',').filter(x => x) : line).map(pat => pat.trim()), []);
|
||||
for (const language of languages) {
|
||||
const baseImage = yield getBaseImageByLanguage(language);
|
||||
if (baseImage) {
|
||||
return baseImage;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
});
|
||||
}
|
||||
function getBaseImageByLanguage(language) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
// eslint-disable-next-line no-undef
|
||||
const rawData = yield fs_1.promises.readFile(path.join(__dirname, 'language-image.json'), 'utf-8');
|
||||
const languageImageJSON = JSON.parse(rawData);
|
||||
return languageImageJSON[language.name];
|
||||
});
|
||||
}
|
||||
exports.getInputList = getInputList;
|
||||
run().catch(core.setFailed);
|
||||
|
|
|
@ -15,7 +15,8 @@
|
|||
"dependencies": {
|
||||
"@actions/core": "^1.2.6",
|
||||
"@actions/exec": "^1.0.4",
|
||||
"@actions/io": "^1.0.2"
|
||||
"@actions/io": "^1.0.2",
|
||||
"language-recognizer": "0.0.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^12.12.7",
|
||||
|
|
|
@ -1,58 +1,51 @@
|
|||
import * as core from "@actions/core";
|
||||
import * as exec from "@actions/exec";
|
||||
import { CommandResult } from "./types";
|
||||
|
||||
interface Buildah {
|
||||
from(baseImage?: string): Promise<CommandResult>;
|
||||
copy(container: string, content: string): 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, flags?: string[]): Promise<CommandResult>;
|
||||
}
|
||||
|
||||
export interface BuildahConfigSettings {
|
||||
author?: string;
|
||||
annotation?: string;
|
||||
arch?: string;
|
||||
created_by?: string;
|
||||
entrypoint?: string[];
|
||||
labels?: string[];
|
||||
envs?: string[];
|
||||
port?: string;
|
||||
workingdir?: string;
|
||||
}
|
||||
|
||||
export interface CommandSucceeeded {
|
||||
readonly succeeded: true;
|
||||
readonly output?: string;
|
||||
}
|
||||
|
||||
export interface CommandFailed {
|
||||
readonly succeeded: false;
|
||||
readonly reason?: string;
|
||||
}
|
||||
|
||||
export type CommandResult = CommandFailed | CommandSucceeeded;
|
||||
|
||||
export class BuildahCli implements Buildah {
|
||||
|
||||
private executable: string;
|
||||
|
||||
constructor(executable: string) {
|
||||
this.executable = executable;
|
||||
}
|
||||
async from(baseImage?: string): Promise<CommandResult> {
|
||||
if (!baseImage) {
|
||||
// find correct baseImage based on language project
|
||||
}
|
||||
|
||||
async from(baseImage: string): Promise<CommandResult> {
|
||||
return await this.execute(['from', baseImage]);
|
||||
}
|
||||
async copy(container: string, content: string, path?: string): Promise<CommandResult> {
|
||||
|
||||
async copy(container: string, contentToCopy: string[], path?: string): Promise<CommandResult> {
|
||||
core.debug('copy');
|
||||
core.debug(container);
|
||||
core.debug(content);
|
||||
const args: string[] = ["copy", container, content];
|
||||
if (path) {
|
||||
args.push(path);
|
||||
let result: CommandResult;
|
||||
for (const content of contentToCopy) {
|
||||
const args: string[] = ["copy", container, content];
|
||||
if (path) {
|
||||
args.push(path);
|
||||
}
|
||||
result = await this.execute(args);
|
||||
if (result.succeeded === false) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
return await this.execute(args);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
async config(container: string, settings: BuildahConfigSettings): Promise<CommandResult> {
|
||||
core.debug('config');
|
||||
core.debug(container);
|
||||
|
@ -65,9 +58,16 @@ export class BuildahCli implements Buildah {
|
|||
args.push('--port');
|
||||
args.push(settings.port);
|
||||
}
|
||||
if (settings.envs) {
|
||||
settings.envs.forEach((env) => {
|
||||
args.push('--env');
|
||||
args.push(env);
|
||||
});
|
||||
}
|
||||
args.push(container);
|
||||
return await this.execute(args);
|
||||
}
|
||||
|
||||
async commit(container: string, newImageName: string, flags: string[] = []): Promise<CommandResult> {
|
||||
core.debug('commit');
|
||||
core.debug(container);
|
||||
|
|
98
src/index.ts
98
src/index.ts
|
@ -1,45 +1,61 @@
|
|||
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';
|
||||
|
||||
/**
|
||||
* 1. Buildah only works on Linux as the docker action
|
||||
* 2. Does this action also need to setup buildah?
|
||||
* 3. Does this action also have the ability to push to a registry?
|
||||
*
|
||||
*/
|
||||
export async function run(): Promise<void> {
|
||||
const baseImage = core.getInput('base-image');
|
||||
const content = core.getInput('content');
|
||||
const entrypoint = await getInputList('entrypoint');
|
||||
const port = core.getInput('port');
|
||||
let baseImage = core.getInput('base-image');
|
||||
const content = getInputList('content');
|
||||
const newImageName = core.getInput('new-image-name');
|
||||
const runnerOS = process.env.RUNNER_OS;
|
||||
const entrypoint = getInputList('entrypoint');
|
||||
const port = core.getInput('port');
|
||||
const workingDir = core.getInput('working-dir');
|
||||
const envs = getInputList('envs');
|
||||
|
||||
if (runnerOS !== 'Linux') {
|
||||
throw new Error(`Only supported on linux platform`);
|
||||
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);
|
||||
|
||||
// create image
|
||||
const cli: BuildahCli = new BuildahCli(buildahPath);
|
||||
const creationResult = await cli.from(baseImage);
|
||||
if (creationResult.succeeded === false) {
|
||||
return Promise.reject(new Error(creationResult.reason));
|
||||
// if base-image is not specified by the user we need to pick one automatically
|
||||
if (!baseImage) {
|
||||
const workspace = process.env['GITHUB_WORKSPACE'];
|
||||
if (workspace) {
|
||||
// check language/framework used and pick base-image automatically
|
||||
const languages = await recognizer.detectLanguages(workspace);
|
||||
baseImage = await getSuggestedBaseImage(languages);
|
||||
if (!baseImage) {
|
||||
return Promise.reject(new Error('No base image found to create a new container'));
|
||||
}
|
||||
} else {
|
||||
return Promise.reject(new Error('No base image found to create a new container'));
|
||||
}
|
||||
}
|
||||
const containerId = creationResult.output.replace('\n', '');
|
||||
|
||||
// create the new image
|
||||
const cli: BuildahCli = new BuildahCli(buildahPath);
|
||||
const container = await cli.from(baseImage);
|
||||
if (container.succeeded === false) {
|
||||
return Promise.reject(new Error(container.reason));
|
||||
}
|
||||
const containerId = container.output.replace('\n', '');
|
||||
|
||||
const copyResult = await cli.copy(containerId, content);
|
||||
if (copyResult.succeeded === false) {
|
||||
return Promise.reject(new Error(copyResult.reason));
|
||||
}
|
||||
|
||||
const configuration: BuildahConfigSettings = {
|
||||
const newImageConfig: BuildahConfigSettings = {
|
||||
entrypoint: entrypoint,
|
||||
port: port
|
||||
}
|
||||
const configResult = await cli.config(containerId, configuration);
|
||||
port: port,
|
||||
workingdir: workingDir,
|
||||
envs: envs
|
||||
};
|
||||
const configResult = await cli.config(containerId, newImageConfig);
|
||||
if (configResult.succeeded === false) {
|
||||
return Promise.reject(new Error(configResult.reason));
|
||||
}
|
||||
|
@ -48,22 +64,42 @@ export async function run(): Promise<void> {
|
|||
if (commit.succeeded === false) {
|
||||
return Promise.reject(new Error(commit.reason));
|
||||
}
|
||||
|
||||
core.setOutput('image', newImageName);
|
||||
}
|
||||
|
||||
export async function getInputList(name: string, ignoreComma?: boolean): Promise<string[]> {
|
||||
function getInputList(name: string): string[] {
|
||||
const items = core.getInput(name);
|
||||
if (items == '') {
|
||||
if (!items) {
|
||||
return [];
|
||||
}
|
||||
return items
|
||||
.split(/\r?\n/)
|
||||
.filter(x => x)
|
||||
.reduce<string[]>(
|
||||
(acc, line) => acc.concat(!ignoreComma ? line.split(',').filter(x => x) : 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> {
|
||||
// eslint-disable-next-line no-undef
|
||||
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);
|
11
src/types.ts
Normal file
11
src/types.ts
Normal file
|
@ -0,0 +1,11 @@
|
|||
export interface CommandSucceeeded {
|
||||
readonly succeeded: true;
|
||||
readonly output?: string;
|
||||
}
|
||||
|
||||
export interface CommandFailed {
|
||||
readonly succeeded: false;
|
||||
readonly reason?: string;
|
||||
}
|
||||
|
||||
export type CommandResult = CommandFailed | CommandSucceeeded;
|
Loading…
Add table
Add a link
Reference in a new issue