2020-11-06 17:42:45 +00:00
|
|
|
import * as core from '@actions/core';
|
|
|
|
import * as io from '@actions/io';
|
|
|
|
import { BuildahCli, BuildahConfigSettings } from './buildah';
|
2020-11-13 11:38:29 +00:00
|
|
|
import * as recognizer from 'language-recognizer';
|
|
|
|
import {promises as fs} from 'fs';
|
|
|
|
import * as path from 'path';
|
|
|
|
import { Language } from 'language-recognizer/lib/types';
|
2020-11-06 17:42:45 +00:00
|
|
|
|
|
|
|
export async function run(): Promise<void> {
|
2020-11-13 11:38:29 +00:00
|
|
|
let baseImage = core.getInput('base-image');
|
|
|
|
const content = getInputList('content');
|
2020-11-06 17:42:45 +00:00
|
|
|
const newImageName = core.getInput('new-image-name');
|
2020-11-13 11:38:29 +00:00
|
|
|
const entrypoint = getInputList('entrypoint');
|
|
|
|
const port = core.getInput('port');
|
|
|
|
const workingDir = core.getInput('working-dir');
|
|
|
|
const envs = getInputList('envs');
|
2020-11-06 17:42:45 +00:00
|
|
|
|
2020-11-13 11:38:29 +00:00
|
|
|
if (process.env.RUNNER_OS !== 'Linux') {
|
|
|
|
return Promise.reject(new Error('Only linux platform is supported at this time.'));
|
2020-11-06 17:42:45 +00:00
|
|
|
}
|
|
|
|
// get buildah cli
|
|
|
|
const buildahPath = await io.which('buildah', true);
|
|
|
|
|
2020-11-13 11:38:29 +00:00
|
|
|
// 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'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// create the new image
|
2020-11-06 17:42:45 +00:00
|
|
|
const cli: BuildahCli = new BuildahCli(buildahPath);
|
2020-11-13 11:38:29 +00:00
|
|
|
const container = await cli.from(baseImage);
|
|
|
|
if (container.succeeded === false) {
|
|
|
|
return Promise.reject(new Error(container.reason));
|
2020-11-06 17:42:45 +00:00
|
|
|
}
|
2020-11-13 11:38:29 +00:00
|
|
|
const containerId = container.output.replace('\n', '');
|
2020-11-06 17:42:45 +00:00
|
|
|
|
|
|
|
const copyResult = await cli.copy(containerId, content);
|
|
|
|
if (copyResult.succeeded === false) {
|
|
|
|
return Promise.reject(new Error(copyResult.reason));
|
|
|
|
}
|
|
|
|
|
2020-11-13 11:38:29 +00:00
|
|
|
const newImageConfig: BuildahConfigSettings = {
|
2020-11-06 17:42:45 +00:00
|
|
|
entrypoint: entrypoint,
|
2020-11-13 11:38:29 +00:00
|
|
|
port: port,
|
|
|
|
workingdir: workingDir,
|
|
|
|
envs: envs
|
|
|
|
};
|
|
|
|
const configResult = await cli.config(containerId, newImageConfig);
|
2020-11-06 17:42:45 +00:00
|
|
|
if (configResult.succeeded === false) {
|
|
|
|
return Promise.reject(new Error(configResult.reason));
|
|
|
|
}
|
|
|
|
|
|
|
|
const commit = await cli.commit(containerId, newImageName, ['--squash']);
|
|
|
|
if (commit.succeeded === false) {
|
|
|
|
return Promise.reject(new Error(commit.reason));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-13 11:38:29 +00:00
|
|
|
function getInputList(name: string): string[] {
|
2020-11-06 17:42:45 +00:00
|
|
|
const items = core.getInput(name);
|
2020-11-13 11:38:29 +00:00
|
|
|
if (!items) {
|
2020-11-06 17:42:45 +00:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
return items
|
|
|
|
.split(/\r?\n/)
|
|
|
|
.filter(x => x)
|
|
|
|
.reduce<string[]>(
|
2020-11-13 11:38:29 +00:00
|
|
|
(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];
|
|
|
|
}
|
2020-11-06 17:42:45 +00:00
|
|
|
|
|
|
|
run().catch(core.setFailed);
|