mirror of
https://github.com/redhat-actions/buildah-build.git
synced 2025-06-08 01:49:03 +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
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);
|
Loading…
Add table
Add a link
Reference in a new issue